-2

我在修复这个无限循环时遇到问题。我已经做了一些测试,所以我很确定在读取文件时循环不是。在“printReportHeading();”之后 是一个for循环。我很确定这是导致问题的原因。我将如何解决这个问题。它与二维数组的初始化有关吗?

代码:

  int main(void)
   {
    FILE* fileIn;
    FILE* printFile;

    float average;

    char letterGrade;

    int wholeArray [MAX_STUDENTS][MAX_PROFILE],
            letterFreq[5];

   printInstructions();

    fileIn = fopen("input11.dat", "r");

    if(fileIn == NULL)
    {
            printf("\n\nFILE COULD NOT BE LOCATED\n\n");
    }
    else
    {
            printFile = fopen("upchurch.txt", "w");

            loadData(fileIn, wholeArray);

            printReportHeading();

            for(int row = 0; row < MAX_STUDENTS; row++)
            {
                    average = calcAverage(wholeArray);
                    letterGrade = determineLetter(average);
                    printLine(printFile, wholeArray, average, letterGrade);

                    switch(letterGrade)
                    {
                            case 'A':
                                    letterFreq[0]++;
                                    break;
                            case 'B':
                                    letterFreq[1]++;
                                    break;
                            case 'C':
                                    letterFreq[2]++;
                                    break;
                            case 'D':
                                    letterFreq[3]++;
                                    break;
                            default:
                                    letterFreq[4]++;
                                    break;
                    }
            }

            printHighScores(printFile, wholeArray);
            printLowScores(printFile, wholeArray);
            printAverageScores(printFile, wholeArray);
            fprintf(printFile, "---------------------------------------------\n");

            printHistogram(printFile, letterFreq);
    }

    return;
}


/******************************************************************************
 *Prints the instructions to the user*
******************************************************************************/
void printInstructions(void)
 {
    printf("=================================================================\n");
    printf("= This program takes up to 40 student's ID and five quiz grades =\n");
    printf("= then finds each students letter grade, average of each quiz,  =\n");
    printf("= highest and lowest grade of each quiz, and creates a          =\n");
    printf("= histogram of all of the students letter gradesa as a whole.   =\n");
    printf("=================================================================\n\n");

    return;
}
/******************************************************************************
 *Gets the data from the file for the user*
******************************************************************************/
int loadData(FILE* fileIn, int wholeArray[][MAX_PROFILE])
{
    for(int i = 0; i < MAX_STUDENTS; i++)
            for(int j = 0; j < MAX_PROFILE; j++)
            {
                    fscanf(fileIn, "%d", &wholeArray[i][j]);
            }

    return 0;
}
/******************************************************************************
 *Prints the report heading*
******************************************************************************/
void printReportHeading()
{
    printf("Student   Quiz 1   Quiz 2   Quiz 3   Quiz 4   Quiz 5   Average   Letter\n");

    return;
 }

/******************************************************************************
 *Calculates each student's average and letter grade*
******************************************************************************/
float calcAverage (int wholeArray[][MAX_PROFILE])
{
    int totalGrades = 0;

  for (int i = 1; i < MAX_PROFILE; i++)
  {
     totalGrades += *wholeArray[i];
  }


     return (totalGrades / 5.0f);
}
/******************************************************************************
 *Finds the letter grade*
******************************************************************************/
char determineLetter(float average)
{
    char letterGrade;

    if (average >= 90)
            letterGrade = 'A';
    else if (average >= 80)
            letterGrade = 'B';
    else if (average >= 70)
            letterGrade = 'C';
    else if (average >= 60)
            letterGrade = 'D';
    else
            letterGrade = 'F';

    return letterGrade;
}
/******************************************************************************
 Prints a line of students information
******************************************************************************/
void printLine(FILE* printFile, int wholeArray[][MAX_PROFILE], float average, char letterGrade)
{
    for(int i = 0; i <= MAX_STUDENTS; i++)
       for(int j = 0; j < MAX_PROFILE; j++)
       {
          fprintf(printFile, "%d", wholeArray[i][j]);
             if ((j = MAX_PROFILE))
             {
                printf("%3.2f", average);
                printf("%c", letterGrade);
                j = 0;
             }
       }
    fprintf(printFile, "---------------------------------------------------\n");

    return;
}

/******************************************************************************
 Find the high score of each quiz
******************************************************************************/
void printHighScores(FILE* printFile, int wholeArray[][MAX_PROFILE])
{
    int highestTest = 0;

    fprintf(printFile, "High ");

    for (int i = 1; i < MAX_PROFILE; i++)
    {
            for(int j = 0; j < MAX_STUDENTS; j++)
            {
                    if (highestTest < wholeArray[j][i])
                    {
                            highestTest = wholeArray[j][i];
                    }
            }

            fprintf(printFile, "%3d", highestTest);
    }
    return;
}

/******************************************************************************
 Finds the low score of each quiz
******************************************************************************/
void printLowScores(FILE* printFile, int wholeArray[][MAX_PROFILE])
{
    int lowestTest = 100;

    fprintf(printFile, "Low  ");

    for (int i = 1; i < MAX_PROFILE; i++)
    {
            for(int j = 0; j < MAX_STUDENTS; j++)
            {
                    if (lowestTest > wholeArray[j][i])
                    {
                            lowestTest = wholeArray[j][i];
                    }
            }

            fprintf(printFile, "%3d", lowestTest);
    }
    return;
} 

/******************************************************************************
 Finds the average score of each quiz
******************************************************************************/
void printAverageScores(FILE* printFile, int wholeArray[][MAX_PROFILE])
{
    float averageTest = 0.0;

    int i;

    fprintf(printFile, "Average   ");

    for (i = 1; i < MAX_PROFILE; i++)
    {
            for(int j = 0; j < MAX_STUDENTS; j++)
            {
                    averageTest += wholeArray[j][i];

            }
            averageTest = averageTest / i;

            fprintf(printFile, "%5.2f", averageTest);
    }
    return;
}

 /******************************************************************************
 Prints histogram of letter frequency
 ******************************************************************************/
void printHistogram(FILE* printFile, int letterFreq[5])
{
    for (int i = 0; i < 5; i++)
    {
            for (int j = 1; j <= letterFreq[i]; j++)
                    printf("*");
            printf("\n");
    }
    return;
}
4

2 回答 2

1

这可能不是您的错误的原因,但letterFreq从未初始化,因此printHistogram会调用未定义的行为并最终可能打印数十亿个“*”。解决此问题的方法是将数组成员初始化为 0

letterFreq[5] = {0};
于 2013-04-30T13:58:17.587 回答
1

printLine中,您有:

if ((j = MAX_PROFILE))
{
    …
    j = 0;
}

这有两个问题。首先,j = MAX_PROFILE是分配,而不是比较。它设置jMAX_PROFILE评估为真,导致执行主体if

然后将主体ifj归零。这会导致内部循环无限重复。

我怀疑您在这里的意图是在打印学生个人资料的所有行之后打印平均值和成绩。在这种情况下,只需在循环后打印它们j,仍然在循环内i。不需要测试,也不需要设置j为零。

于 2013-04-30T13:52:22.803 回答