我在修复这个无限循环时遇到问题。我已经做了一些测试,所以我很确定在读取文件时循环不是。在“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;
}