gpt4 book ai didi

使用文件指针创建二维数组

转载 作者:行者123 更新时间:2023-11-30 18:15:19 24 4
gpt4 key购买 nike

我编写了这个程序,它从输入文件创建一个二维数组,然后对数字执行各种操作,例如计算平均值、获取最大值等......输入文件有 96 个字符,12 行,每行 8 个字符。这正是我想要设置数组的方式。

我觉得除了创建数组之外,一切都是正确的。当我运行函数来打印数组时,会弹出一些非常奇怪的数字,包括负数。与输入文件中的内容相差甚远。

任何人都可以看到 makeArray 函数或我在 main 中调用该函数的方式有问题吗?

#include <stdio.h>        
void makeArray(FILE*ptr, int array[12][8]) {
int i,j;
ptr = fopen("scores.txt", "r");
for (i = 0 ; i < 12 ; i++) {
for (j = 0 ; j < 8 ; j++) {
fscanf(ptr, "%d", &array[i][j]);
}
}
}

int getScore(int array[12][8], int mon, int trn) {
return array[mon][trn];
}

int getMonthMax(int array[12][8], int mon) {
int max = 0, i;
for (i = 0 ; i < 8 ; i++) {
if (array[mon][i]>max)
max = array[mon][i];
}
return max;
}

int getYearMax(int array[12][8]) {
int max = 0, i, j;
for (i = 0 ; i < 12 ; i++) {
for (j = 0 ; j < 8 ; j++) {
if (array[i][j] > max)
max = array[i][j];
}
}
return max;
}

int getMonthAvg(int array[12][8], int mon) {
int avg, sum = 0, i;
for (i = 0 ; i < 8 ; i++) {
sum = array[mon][i] + sum;
}
avg = sum / 8;
return avg;
}

int getYearAvg(int array[12][8]) {
int avg, sum=0, i, j;
for (i = 0 ; i < 12 ; i++) {
for (j = 0 ; j < 8 ; j++) {
sum = array[i][j] + sum;
}
}
avg = sum / (12 * 8);
return avg;
}

int toursMissed(int array[12][8]) {
int count = 0, i, j;
for (i = 0 ; i < 12 ; i++) {
for (j = 0 ; j < 8 ; j++) {
if (array[i][j] == 0)
count++;
}
}
return count;
}


void printArray (int array[12][8]) {
int i, j;
printf("The scores for the year are:\n");
for (i = 0 ; i < 12 ; i++) {
for (j = 0 ; j < 8 ; j++) {
printf("%d\t", array[i][j]);
}
printf("\n");
}
}

void displayMenu() {
printf("What would you like to do?\n");
printf("------------------------------------\n");
printf("Select from options 1-7 or 0 to stop\n");
printf("Select 1 to get the score for a specific game\n");
printf("Select 2 to get the max score for a specific month\n");
printf("Select 3 to get the average score for a specific month\n");
printf("Select 4 to get the max score for the year\n");
printf("Select 5 to get the average score for the year\n");
printf("Select 6 to get the number of tournaments missed for the year\n");
printf("Select 7 to print all scores for the year\n");
printf("Select 0 to stop\n");
printf("------------------------------------\n");
}

void processRequest(int array[12][8], int num) {
int scores[12][8], answ, mon, game;

if (num == 0) {
printf("Thank you! Goodbye.");
}
else if (num == 1) {
printf("Please enter the month and the game\n");
scanf("%d%d", &mon, &game);
answ= getScore(scores, mon, game);
printf("The score for Tournament %d is %d\n\n\n\n", game, answ);
}
else if (num == 2) {
printf("Please enter the month\n");
scanf("%d", &mon);
answ=getMonthMax(scores, mon);
printf("The maximum score for month %d is %d\n\n\n\n", mon, answ);
}
else if (num == 3) {
printf("Please enter the month\n");
scanf("%d", &mon);
answ=getMonthAvg(scores, mon);
printf("The average score for month %d is %d\n\n\n\n", mon, answ);
}
else if (num == 4) {
answ= getYearMax(scores);
printf("The max score for the year is %d\n\n\n\n", answ);
}
else if (num == 5) {
answ= getYearAvg(scores);
printf("The average score for the year is %d\n\n\n\n", answ);
}
else if (num == 6) {
answ= toursMissed(scores);
printf("The number of tournaments missed for the year is %d\n\n\n\n", answ);
}
else if (num == 7) {
printArray(scores);
printf("\n\n\n\n");
}
else
printf("Please make a valid selection\n\n\n\n");
}

int main() {
int choice;
int scores[12][8];

FILE *input;
makeArray(input, scores);

do {
displayMenu();
scanf("%d", &choice);

processRequest(scores, choice);
} while (choice != 0);

return 0;
}

最佳答案

首先。您应该为数组中的每个原始数据分配内存

int ii;
typedef FILE * FILE_PTR;
FILE_PTR ** lossArr = (FILE_PTR**)malloc( sizeof( FILE_PTR* ) * i );
for ( ii = 0; ii < i; ii++ ) {
lossArr[ ii ] = (FILE_PTR*)malloc( sizeof( FILE_PTR ) * j );
}
// now you can use lossArr[ x ][ y ], where x = 0..i-1, y = 0..j-1

关于使用文件指针创建二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33817502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com