Code:
#include <stdio.h>
#define MAX_STUDENTS 50
int stuID[MAX_STUDENTS];
int finalScores[MAX_STUDENTS];
int getScores();
int calculateScores(char P_N, int homeworkTotal, int midtermTotal, int final, int extraCredit);
int sortScores();
int printToFile();
int main (void)
{
sortScores();
printToFile();
return 0;
}
/*scan the students ID's into an array and scan in each students scores*/
int getScores()
{
FILE* spGrades;
int final;
int extraCredit;
int ID;
int ret;
char P_N;
int stu_num = 0;
if (!(spGrades = fopen ("BG_Scores.txt", "r")))
{
printf("\aError opening student grade file\n");
}
for(stu_num=0;stu_num<MAX_STUDENTS;stu_num++){ // no more MAX_STUDENTS
//processing student ID's
ret=fscanf(spGrades, "%d\n", &ID);
if(ret!=1){//until no more student ID's
printf("Grades calculated successfully.\n");
return stu_num;
}
//process pass or no pass grade
fscanf(spGrades, "%c\n", &P_N);
//processing student homework scores
int homeworkTotal = 0;
int i;
int hw;
for (i = 0; i < 8; i++)
{
fscanf(spGrades, "%d ", &hw);
homeworkTotal = homeworkTotal + hw;
}
fscanf(spGrades, "%d\n", &hw);
homeworkTotal = homeworkTotal + hw;
homeworkTotal /= 9;
// processing midterms
int midtermTotal = 0;
int midterm;
{
fscanf(spGrades, "%d %d", &midterm, &midtermTotal);
midtermTotal = midtermTotal + midterm;
}
midtermTotal /= 2;
//processing final grade
fscanf(spGrades, "%d\n", &final);
//process extra credit
fscanf(spGrades, "%d\n", &extraCredit);
stuID[stu_num] = ID;
finalScores[stu_num] = calculateScores(P_N, homeworkTotal, midtermTotal, final, extraCredit);
}
fclose(spGrades);
}
int calculateScores(char P_N, int homeworkTotal, int midtermTotal, int final, int extraCredit)
{
float final_grade;
if(P_N != 'P')
return 0;
if(homeworkTotal <= 70)
return 0;
if(midtermTotal <= 60)
return 0;
if(final <= 60)
return 0;
final_grade = ((homeworkTotal + midtermTotal + final + extraCredit) / 3);
return(final_grade);
}
int sortScores()
{
}
int printToFile()
{
FILE* spSorted;
if (!(spSorted = fopen ("BG_Sorted.txt", "w")))
{
printf("\aError opening student grade file\n");
}
int i;
int stu_num;
fprintf(spSorted, "Spring 2012\n");
fprintf(spSorted, "CIS 15 BG\n");
fprintf(spSorted, "====\t======\t====\n");
fprintf(spSorted, "ID\tSCORE\tGRADE\n");
fprintf(spSorted, "====\t======\t====\n");
stu_num = getScores();
for (i =0; i < stu_num; i++){
fprintf(spSorted, "%d\t", stuID[i]);
fprintf(spSorted, "%d\t", finalScores[i]);
int finalScore=finalScores[i];
if(finalScore >= 97)
fprintf(spSorted, "A+");
else if(finalScore >=93 && finalScore <= 96.9)
fprintf(spSorted, "A");
else if(finalScore >= 90 && finalScore <= 92.9)
fprintf(spSorted, "A-");
else if(finalScore >= 87 && finalScore <=89.9)
fprintf(spSorted, "B+");
else if(finalScore >=83 && finalScore <=86.9)
fprintf(spSorted, "B");
else if(finalScore >=80 && finalScore <= 82.9)
fprintf(spSorted, "B-");
else if(finalScore >=77 && finalScore <=79.9)
fprintf(spSorted, "C+");
else if(finalScore >= 70 && finalScore <= 76.9)
fprintf(spSorted, "C");
else if(finalScore >= 67 && finalScore <= 69.9)
fprintf(spSorted, "D+");
else if(finalScore >= 63 && finalScore <= 66.9)
fprintf(spSorted, "D");
else if(finalScore >= 60 && finalScore <= 62.9)
fprintf(spSorted, "D-");
else
fprintf(spSorted, "F");
fprintf(spSorted,"\n");
}
}
-I got one of my moms friends to sit with me through a brutal 3 hours to get it done. Only thing that we were both stumped at was that we need to do a selection sort from highest to lowest of the grades. I don't know how to do this and didn't want to bother him any further. Also, the problem is that even if I sort the scores, they will be miss matched with the ID's... How do I go about doing this?