【问题标题】:Creating 2D array using file pointer使用文件指针创建二维数组
【发布时间】: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;
}

【问题讨论】:

  • 您可以编辑问题以包含输入文件的示例吗?

标签: c arrays 2d


【解决方案1】:

首先。您应该为数组中的每个 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

【讨论】:

  • 我知道它是如何工作的,但不幸的是这是一个作业,所以我不能使用材料之外的东西。我需要使用我在整个程序中使用的东西
  • 不要转换malloc()的返回值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2010-11-06
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
相关资源
最近更新 更多