Main未能正确调用函数" makeArray"

时间:2016-07-26 20:54:37

标签: c arrays function 2d

我不太确定问题是什么,但我的函数makeArray在读取文件时不会存储值,因此数组只是吐出垃圾而不是我需要的值。

这是我的功能

#include <stdio.h>
#include <stdlib.h>
#define ROW 12
#define COL 8

void makeArray(FILE *infile, int array[][8]) {
    int i, j;
    infile = fopen("scores.txt", "r");

    for (i = 0; i < ROW; i++) {
        for (j = 0; j < COL; j++) {
            fscanf(infile, "%d", &array[i][j]);
        }
    }
    fclose(infile);
}

主要是:

int main() {
    int choice, array[ROW][COL] = { 0 };
    FILE *infile;

    makeArray(infile, array);

    do {
        displayMenu();
        scanf("%d", &choice);
        processRequest(array, choice);
    } while (choice != 0);

    return 0;
}

整个代码如下:

#include <stdio.h>
#include <stdlib.h>

#define ROW 12
#define COL 8

void makeArray(FILE *infile, int array[][8]) {
    int i, j;

    infile = fopen("scores.txt", "r");

    for (i = 0; i < ROW; i++) {
        for (j = 0; j < COL; j++) {
            fscanf(infile, "%d", &array[i][j]);
        }
    }
    fclose(infile);
}

int getScore(int array[][8], int month, int game) {

    int score;

    array[month-1][game-1] = score;

    return score;
}

int getMonthMax(int array[][8], int month) {

    int i, max;

    for (i = 0; i < COL; i++) {
        if (array[month - 1][i] > max) {
            max = array[month - 1][i];
        }
    }   
    return max;     
}

int getYearMax(int array[][8]) {

    int i, j, max;

    for (i = 0; i < ROW; i++) {
        for (j = 0; j < COL; j++) {
            if (array[i][j] > max) {
                max = array[i][j];
            }
        }
    }
    return max;
}

float getMonthAvg(int array[][8], int month) {

    int i, sum = 0, num = 0, j = 0;
    float avg;

    for (i = 0; i < COL; i++) {
        array[month - 1][i] = num;
        sum += num;
        j++;    
    }
    avg = (sum / j);
    return avg;
}

float getYearAvg(int array[][8]) {

    int i, j, k, sum = 0, num;
    float avg;

    for (i = 0; i < ROW; i++) {
        for (j = 0; j < COL; j++) {
            array[i][j] = num;
            sum += num;
            k++;
        }
    }
    avg = (sum / k);
    return avg;
}

int toursMissed(int array[][8]) {

    int i, j, k;

    for (i = 0; i < ROW; i++) {
        for (j = 0; j < COL; j++){
            if (array[i][j] == 0)
                k++;
        }
    }
    return k;
}

void displayMenu() {

    int i, com;

    printf("What would you like to do?\n");
    for (i = 0; i < 40; i++) {
        printf("-");
    }
    printf("\nSelect from option 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 tournamnets missed for the year\n");
    printf("Select 7 to print all scores for the year\n");
    printf("Select 0 to stop\n");
    for (i = 0; i < 40; i++) {
        printf("-");
    }
    printf("\n");

}

void printArray(int array[][8]) {

    int i, j;

    for (i = 0; i < ROW; i++) {
        for (j = 0; j < COL; j++) {
            printf("%d\t", &array[i][j]);   
        }
        printf("\n");
    }
}

void processRequest(int array[][8], int integer) {

    int f1, f2, f3, f4, f5, f6, f7, f8;
    int mont, gam;


    if (integer == 0) {
        printf("\nThank you!  Goodbye\n");
    }
    if (integer == 1) {
        printf("\nPlease enter the month and the game\n");
        scanf("%d%d", &mont, &gam);
        f1 = getScore(array, mont, gam);
        printf("\nThe score for Tournament %d is %d", gam, f1);
    }
    if (integer == 2) {
        printf("\nPlease enter the month\n");
        scanf("%d", &mont);
        f2 = getMonthMax(array, mont);
        printf("\nThe max score for month %d was %d\n", mont, f2);
    }
    if (integer == 3) {
        printf("\nPlease enter the month\n");
        scanf("%d", &mont);
        f3 = getMonthAvg(array, mont);
        printf("\nThe average score for month %d is %4.2f\n", mont, f3);
    }
    if (integer == 4) {
        f4 = getYearMax(array);
        printf("\nThe max score for the year is %d\n", f4);
    }
    if (integer == 5) {
        f5 = getYearAvg(array);
        printf("\nThe average score for the year is %4.2f\n", f5);
    }
    if (integer == 6) {
        f6 = toursMissed(array);
        printf("\nThe number of tournaments missed for the year is %d\n", f6);
    }
    if (integer == 7) {
        printf("\nThe scores for the year are:\n");
        printArray(array);
    }
}

int main() {

    int choice, array[ROW][COL] = { 0 };
    FILE *infile;

    makeArray(infile, array);

    do {
        displayMenu();
        scanf("%d", &choice);
        processRequest(array, choice);
    } while (choice != 0);

    return 0;
}

不确定是否有必要提供所有这些信息,但现在已经可以使用了。

1 个答案:

答案 0 :(得分:0)

您的代码中存在多个问题:

  • 主要问题是你无法判断数组是否被正确读取,因为函数printArray()中的转储代码不正确:printf("%d\t", &array[i][j]);应该是printf("%d\t", array[i][j]);

    < / LI>
  • function makeArray接受一个FILE *infile参数,其值被忽略。您应该将此参数设置为局部变量,或者在调用者中打开文件并传递FILE*或者可能将文件名作为参数传递。

  • 您没有测试任何返回值是否正确完成:如果无法打开文件,fopen可以返回NULL。使用NULL流指针将调用大多数标准I / O函数中的未定义行为。

  • 您不会测试scanf()而不是fscanf()的返回值。如果提供了错误的输入,程序将调用未定义的行为,并且用户将无从找到解释的位置。检查所有返回值并生成明确的文档错误消息。

  • 功能getScore不正确。它应该是:

    int getScore(int array[][8], int month, int game) {
        return array[month - 1][game - 1];
    }
    
  • 如果在函数中将COL的值硬编码为8,则
  • 定义COL是无用的:数组是使用显式常量而不是COL宏定义的。 / p>

  • 函数getYearAvg()getMonthAvg()num存储到数组中,而不是从数组中读取它。此外,avg未初始化为0.0

  • k未在0 toursMissed()getYearAvg()投放array[0][0]

  • max应在getYearMax()getMonthMax()

  • 中初始化为makeArray()

最后,如果输入正确,from itertools import chain packed_pairs = zip(*[[(k, v) for v in vs] for ds in iter_list for k, vs in ds.items()]) pairs = chain(*packed_pairs) for pair in pairs: print pair[0], pair[1] 是唯一不会失败的功能。