我寫了這個程序,從輸入文件創建一個二維數組,然後對數字做各種事情,如計算平均值,獲取最大值等... 輸入文件有96個字符, 12行,每行8個字符。這正是我想要設置陣列的方式。使用文件指針創建二維數組
我覺得一切都是正確的,除了創建數組。當我運行函數來打印數組時,會彈出一些非常奇怪的數字,包括底片。遠離輸入文件中的內容。
任何人都可以在makeArray函數或我如何調用主函數中看到錯誤嗎?
#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;
}
您是否可以編輯問題以包含輸入文件的示例? – m69