Thread: Grades calculating program help please!

  1. #61
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    -Sorry, little tweaks after posting the code that I noticed. So before you have an aneurysm. I got rid of the -1 in the second for loop, and changed MAX_STUDENT to MAX_STUDENTS like it originally was.

  2. #62
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    -Never mind about the MAX_STUDENT part. Didn't realize that it was a numeric constant.... I'll change it back.

  3. #63
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok more tweaks:

    1. Move your array declarations from above main into main. We don't want to use global variables, it is generally bad practice.
    2. In our sort function we don't want to sort MAX_STUDENTS, we want to sort the ACTUAL number of students (i.e. the number of student entries you read from the file). MAX_STUDENTS is just our upper margin, but we could have anywhere between 1 and 50 students. We need to change the third parameter in our sort to something that makes more sense like: numStudents. Then replace all entries of MAX_STUDENTS in the sort function with numStudents. This is probably the number you are returning from getScores(). For now just make those changes, then repost the code.
    3. Remove & from our array params when calling the sort function in main(). That will get rid of the warnings.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #64
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, does this make sense so far? Do you understand what we are doing, and if you don't what do you need clarifications on?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #65
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    #include <stdio.h>
    #define MAX_STUDENTS 50
    
    
           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 stuID[MAX_STUDENTS];
           int finalScores[MAX_STUDENTS];
    
           int stu_num;
           sortScores(&finalScores, &stuID, stu_num);
           printToFile();
    
           return 0;
    }
    /*scan the students ID's into an array and scan in each students scores*/
    int getScores()
    {
           FILE* spGrades;
    
           int stuID[MAX_STUDENTS];
           int finalScores[MAX_STUDENTS];
    
           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 stu_num)
    {
    int current;
    int walk;
    int tempData;
    
           for(current = 0; current < stu_num; current++)
           {
                   for (walk = (current + 1); walk < stu_num; walk++)https://mail.google.com/mail/images/cleardot.gif
    
    
    
                   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;
    
    int stuID[MAX_STUDENTS];
    int finalScores[MAX_STUDENTS];
    
    
           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");
           }
    }
    

    -And the compiler is still returning the same two warnings are above.

  6. #66
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by claudiu View Post
    Also, does this make sense so far? Do you understand what we are doing, and if you don't what do you need clarifications on?
    Yeah, it makes sense for the most part. I'll probably have to go back and mull over it some more after I finally finish with this program and get some decent rest (its 6am over here lol)

  7. #67
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You still haven't changed tweak number 3 above. Look at my post again, I said remove the & when calling the sort function in main().
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #68
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by claudiu View Post
    You still haven't changed tweak number 3 above. Look at my post again, I said remove the & when calling the sort function in main().
    Whoops sorry, didn't see that. I have a bigger problem now though... The sort function pretty much destroyed my output file and made almost everything garbage.

  9. #69
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Don't worry about that for now we are getting to the testing part in a bit, still need a few more changes.

    1. Your print function needs to be able to know how many students you have, and so does your sort for that matter. Currently the only way to find that out is to call the getScores() function which I hope, returns correctly the number of students read from the input file. We have to pass our arrays as parameters to the other two functions: printScores and getScores.

    getScores will take only the arrays as parameters. getScores() will return how many students we have read.
    printScores will basically take the same params as the sort function (because we need to know how many students we are printing from the array)

    2. In main, we will have our two arrays declared, and we will declare another variable called stu_num or whatever you call it which will be the number of students read from the file. In order to initialise it, we will call the getScores function with the new prototype(changed as per instructions in 1.) and we will store its return value in this new variable:
    i.e. stu_num = getScores(finalScores, stuIDs) ---- (or whatever you call them)

    3. Then we need to sort the results, so we pass finalScores,stuIDs and stu_num (NOT MAX_STUDENTS) to the sort function.

    4. Then we call the print function and print the results.


    Basically, main orchestrates everything. It tells the getScores function: "Hey function, I want to read two arrays of data, here are two arrays, store everything in them and tell me how many you have read". Then main says to the sortFunction: "Hey function, I have these two arrays with this many students (stu_num), please sort them for me". And then it tells the print function: "Hey print function I have two arrays with this many students, which unbeknownst to you are already sorted by another function, please print them out".
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #70
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    #include <stdio.h>
    #define MAX_STUDENTS 50
    
    
           int getScores(int finalScores[], int stuID[]);
           int calculateScores(char P_N, int homeworkTotal, int midtermTotal, int final, int extraCredit);
    
           void sortScores(int finalScores[], int stuID[], int stu_num);
           int printToFile(int finalScores[], int stuID[], int stu_num);
    int main (void)
    {
           int stuID[MAX_STUDENTS];
           int finalScores[MAX_STUDENTS];
           int stu_num;
    
           stu_num = getScores(finalScores, stuID);
           sortScores(finalScores, stuID, stu_num);
           printToFile(finalScores, stuID, stu_num);
    
           return 0;
    }
    /*scan the students ID's into an array and scan in each students scores*/
    
    int getScores(int stuID[], int finalScores[])
    
    
    
    {
           FILE* spGrades;
           int stuID[MAX_STUDENTS];
           int finalScores[MAX_STUDENTS];
           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 stu_num)
    {
    int current;
    int walk;
    int tempData;
           for(current = 0; current < stu_num; current++)
           {
                   for (walk = (current + 1); walk < stu_num; 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(int finalScores[], int stuID[], int stu_num)https://mail.google.com/mail/images/cleardot.gif
    
    
    
    
    {
    FILE* spSorted;
    int stuID[MAX_STUDENTS];
    int finalScores[MAX_STUDENTS];
           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");
           }
    }
    

    Here are the warnings:
    HW1.c: In function 'getScores':
    HW1.c:23: error: 'stuID' redeclared as different kind of symbol
    HW1.c:20: error: previous definition of 'stuID' was here
    HW1.c:24: error: 'finalScores' redeclared as different kind of symbol
    HW1.c:20: error: previous definition of 'finalScores' was here
    HW1.c: In function 'printToFile':
    HW1.c:126: error: 'stuID' redeclared as different kind of symbol
    HW1.c:123: error: previous definition of 'stuID' was here
    HW1.c:127: error: 'finalScores' redeclared as different kind of symbol
    HW1.c:123: error: previous definition of 'finalScores' was here
    HW1.c:134: error: 'stu_num' redeclared as different kind of symbol
    HW1.c:123: error: previous definition of 'stu_num' was here
    HW1.c:143: error: too few arguments to function 'getScores'
    -My brain can no longer figure out what the compiler wants this time.

  11. #71
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well it should be pretty obvious, you have the arrays redeclared as local variables in all the functions. Remove those, you are now passing them as parameters.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #72
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I also suggest you rest a bit and look at this with fresh eyes when you are feeling better. No need to work yourself to death, this is getting very close to being finished.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  13. #73
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Ok, now all thats left is it tells me "too few variables in getScores"

  14. #74
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by claudiu View Post
    I also suggest you rest a bit and look at this with fresh eyes when you are feeling better. No need to work yourself to death, this is getting very close to being finished.
    Problem is, this assignment is due at 1:30 today. Its 7am. lol Hopefully, I can finish it soon, and take a nap before going to class and turning it in.

  15. #75
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are calling getScores() in the print function. Remove that line.

    Does your read function work? have you tested reading from a file like I said yesterday?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! calculating grades
    By anlk5910 in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2011, 04:03 PM
  2. Having problem with a program for Calculating Grades
    By Magmadragoon in forum C Programming
    Replies: 17
    Last Post: 02-15-2011, 08:21 AM
  3. calculating gpa with only grades?
    By ashlee in forum C Programming
    Replies: 8
    Last Post: 11-03-2010, 10:18 AM
  4. Calculating grades
    By Ducky in forum C++ Programming
    Replies: 0
    Last Post: 03-14-2008, 12:16 AM
  5. Replies: 13
    Last Post: 08-15-2002, 09:20 AM

Tags for this Thread