使用文件指针创建2D数组

时间:2015-11-20 01:31:15

标签: c arrays 2d

我编写了这个程序,它从输入文件创建一个2D数组,然后对数字做各种事情,比如计算平均值,得到最大值等等...... 输入文件有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;
}

1 个答案:

答案 0 :(得分:-2)

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

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