-Another adjustment: got rid of int temp. I wasn't using it.
Printable View
-Another adjustment: got rid of int temp. I wasn't using it.
Whoops, just realized that I posted the wrong code up there (like I said, I'm brain dead now lol)
Heres the correct one:
Code:void sortScores(int finalScores[], int stuID[], int MAX_STUDENT)
{
int current;
int walk;
int tempData;
for(current = 0; current <= MAX_STUDENT - 1; current++)
{
for (walk = current + 1; walk <= MAX_STUDENT -1; walk++)
if(finalScores[current] < finalScores[walk])
{
tempData = finalScores[current];
finalScores[current] = finalScores[walk];
finalScores[walk] = tempData;
}
}
}
Ok this is almost correct now. However, you need to change the conditions a bit:
Make both conditions < MAX_STUDENT rather than <= MAX_STUDENT - 1. Although it is the same thing, we prefer strict conditions whenever possible (i.e. no equality). You will learn that over time though, it is just how programmers write code.
Indent the code inside your if clause with a tab, so it looks nice.
Now inside the if clause you are currently swapping finalGrades in order to sort them appropriately. Whenever you do that you must also swap stuID[current] with stuID[walk], so that when you print your arrays at the end, student[i] will have the correct initial grade[i] rather than someone else's grade. You do that the same way you swap your scores just after it.
Something tells me your program wouldn't work if I entered "62.99" or any similar value.
Hold your horses memcpy, we are getting to that.
-Do I need to set tempData equal to stuID?Code:void sortScores(int finalScores[], int stuID[], int MAX_STUDENT)
{
int current;
int walk;
int tempData;
for(current = 0; current < MAX_STUDENT - 1; current++)
{
for (walk = (current + 1); walk < MAX_STUDENT - 1; walk++)
if(finalScores[current] < finalScores[walk])
{
tempData = finalScores[current];
finalScores[current] = finalScores[walk];
stuID[current] = stuID[walk];
finalScores[walk] = tempData;
}
}
}
You got it wrong. Don't change what was previously there in terms of the swap of grades. You just need to do the same thing for the student array. To swap two variables you use a third one tempData. You did that once for finalScores. Do the exact same (same lines) but for the other array after (but still within the if clause, because we only want to swap if that condition is met).
I'm a little bit confused by what you mean...
-Is this kind of what its supposed to look like?Code:void sortScores(int finalScores[], int stuID[], int MAX_STUDENT)
{
int current;
int walk;
int tempData;
for(current = 0; current < MAX_STUDENT - 1; current++)
{
for (walk = (current + 1); walk < MAX_STUDENT - 1; walk++)
if(finalScores[current] < finalScores[walk])
{
tempData = finalScores[current];
tempData = stuID[current];
finalScores[current] = finalScores[walk];
stuID[current] = stuID[walk];
finalScores[walk] = tempData;
studID[walk] = tempData;
}
}
}
Or should I make a tempData2 for the stuID or something?
This is what you originally had:
This swaps two numbers, finalScores[current] and finalScores[walk] using an intermediary third, tempData.Code:tempData = finalScores[current];
finalScores[current] = finalScores[walk];
finalScores[walk] = tempData;
Swap stuID[current] and stuID[walk] using the same strategy right after swapping the scores.
More clear?
Like this?
Code:void sortScores(int finalScores[], int stuID[], int MAX_STUDENT)
{
int current;
int walk;
int tempData;
for(current = 0; current < MAX_STUDENT - 1; current++)
{
for (walk = (current + 1); walk < MAX_STUDENT - 1; walk++)
if(finalScores[current] < finalScores[walk])
{
tempData = finalScores[current];
finalScores[current] = finalScores[walk];
finalScores[walk] = tempData;
tempData = stuID[current];
stuID[current] = stuID[walk]
stuID[walk] = tempData;
}
}
}
I noticed that I missed the ;
I fixed it in my code.
Good. You still don't have the conditions for the for correct though. I said change them to be < MAX_STUDENT not < MAX_STUDENT - 1.
Other than that this function looks good for now, post your entire code with the new changes. And make sure it's well indented before posting.
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);
void sortScores(int finalScores[], int stuID[], int num_student);
int printToFile();
int main (void)
{
int num_student;
sortScores(&finalScores, &stuID, num_student);
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;
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 written to file 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)
{
int 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);
}
void sortScores(int finalScores[], int stuID[], int MAX_STUDENT)
{
int current;
int walk;
int tempData;
for(current = 0; current < MAX_STUDENT; current++)
{
for (walk = (current + 1); walk < MAX_STUDENT - 1; walk++)
if(finalScores[current] < finalScores[walk])
{
tempData = finalScores[current];
finalScores[current] = finalScores[walk];
finalScores[walk] = tempData;
tempData = stuID[current];
stuID[current] = stuID[walk];
stuID[walk] = tempData;
}
}
}
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];https://mail.google.com/mail/images/cleardot.gif
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");
}
}
Something must be wrong with my declaration for sortScores... the compiler gives me two warnings:
13: warning: passing argument 1 of 'sortScores' from incompatible pointer type
13: warning: passing argument 2 of 'sortScores' from incompatible pointer type