【发布时间】:2015-11-20 01:31:15
【问题描述】:
我编写了这个程序,它从输入文件创建一个二维数组,然后对数字执行各种操作,例如计算平均值、获取最大值等... 输入文件有 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;
}
【问题讨论】:
-
您可以编辑问题以包含输入文件的示例吗?