我遇到了修復這個無限循環的問題。 我已經做了一些測試,所以我很確定循環不是在讀取文件時。 緊跟在「printReportHeading();」之後是一個for循環。我很確定這是導致問題的原因。 我將如何去解決這個問題。 這與2維數組的初始化有關嗎?無限循環與二維數組
代碼:
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;
}
如果問題是一個無限循環(而不是說通過'printFile'是NULL導致崩潰),循環必須在代碼中你沒有向我們展示。你可以編輯你的問題,包括'loadData','calcAverage','determineLetter'和'printLine'的代碼嗎? – simonc 2013-04-30 13:27:52
我添加了其餘的代碼,感謝您的興趣@simonc – 2013-04-30 13:46:24
除非MAX_STUDENTS正在更改值或無法通過'int row'訪問的數字,否則主循環中無法解決問題。請顯示您的其他功能,以及如何定義MAX_STUDENTS。一定要通過調試器逐步完成代碼,無限循環應該很容易找到。 – Mike 2013-04-30 13:47:01