// id scores
2000 62 40
3199 92 97
4012 75 65
6547 89 81
1017 95 95
7714 85 83
1234 91 76//AG_midterm.txt
任务:从文件中读取数据,丢弃学生ID,计算两次期中考试的平均值,并将其存储到数组中 并将平均值写入另一个文件(AG_sorted.txt)
我面临错误,我似乎无法弄清楚问题。我尽力做到尽可能整洁。
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 50
// Function declarations
void printInfo();
int readresults(FILE *AG_Midterm, int *score1, int *score2);
void sort(float* avgScore);
void calcAvg (FILE *AG_Midterm, float* avgScore, int score1, int score2);
void writeSortedResults (FILE* AG_Sorted, float* avgScore);
void printdone();
int main (void)
{
// Local Declarations
FILE* AG_Midterm;
FILE* AG_Sorted;
int score1[MAX_SIZE];
int score2[MAX_SIZE];
float avgScore[MAX_SIZE];
// Statements
printInfo();
if(!(AG_Midterm = fopen ("/Users/r3spectak/Desktop/AG_Midterm.txt", "r")))
{
printf("\aError opening Results File\n");
return 100;
} // if open input
if(!(AG_Sorted = fopen ("/Users/r3spectak/Desktop/AG_Sorted.txt","w")))
{
printf("\aError opening Average Results file\n");
return 102;
}// if open input
while(readresults(AG_Midterm, score1, score1))
{
calcAvg(AG_Midterm,&avgScore,score1,score2);
sort(avgScore);
writeSortedResults(AG_Sorted, avgScore);
} //while
fclose (AG_Midterm);
fclose (AG_Sorted);
printdone();
return 0;
} // main
/*==================printInfo==================================
Reads data from Midterm file
Pre : Nothing
Post: Prints introduction message
*/
void printInfo()
{
// Statements
printf("Begin Calculation of Scores\n");
return ;
} // printInfo
/*===================readResults==================================
Reads data from employee file
Pre : spEmp is an open file.
empID,dept,payrate,exempt,hours worked
Post: reads Employee ID and Pay rate
if data read -- returns 1
if EOF or error--returns 0
*/
int readresults(FILE *AG_Midterm, int *score1, int *score2)
{
// Statements
int i;
int items;
for (i = 0; i < MAX_SIZE && (items = fscanf(AG_Midterm, "%*d%d%d", score1 + i, score2 + i)) != EOF;
i++) {
if (items != 2) {
printf( "Error reading data\n");
return -1;
}
}
return 0;
} //readresults
void sort(float*avgScore)
{
int i = 0;
int j = 0;
for(i = 0; i< MAX_SIZE; i++);
{
for(j = i + 1; j < 10; j++)
{
if(avgScore[i] < avgScore[j])
{
//Exchange them
int temp = avgScore[i];
avgScore[i] = avgScore[j];
avgScore[j] = temp;
}
}
}
return;
} // sort
/*===================calcAvg==================================
Determines the Average of the two midterm scores
Pre : score1, score2
Post: avgScore copied to addresses
*/
void calcAvg (FILE *AG_Midterm, float* avgScore, int score1 , int score2)
{
int i=0;
// Statements
while (i<MAX_SIZE && fscanf(AG_Midterm,"%*d %d %d", &score1, &score2)!=EOF)
{
avgScore[i] = (score1 + score2)/2.0;
i++;
}
} // calcAvg
/*===================writeSortedResults==================================
Writes Average Scores after Sorting
Pre : AG_Sorted is an open file
avgScore
Post: Data written to file
*/
void writeSortedResults (FILE* AG_Sorted, float* avgScore)
{
// Statements
fprintf(AG_Sorted, "%f\n", avgScore);
return;
} // writeSortedResults
/*==================printDone==================================
Reads data from Midterm Score File
Pre : Nothing
Post: Prints end message
*/
void printdone()
{
// Statements
printf("End of Average Score\n");
return;
} // printDone
答案 0 :(得分:1)
您的代码通常会反复将数据与数据地址混淆。例如,行
while(readresults(AG_Midterm, &score1, &score1))
应该阅读
while(readresults(AG_Midterm, score1, score1))
所以,问题似乎是一个普遍的概念问题,而不是一个错误,而你可能还没有能够解决它。您可以先尝试一些更简单的方法,然后再回到此代码。 (如果它是早上到期的家庭作业,那么时间已经很晚了:你可能无法在一个晚上学到你需要知道的东西。希望这个答案至少指向你正确的部分你的教科书。祝你好运。)