Thread: Grades calculating program help please!

  1. #16
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    #include<stdio.h>
    
    
    int main (void)
    {
    FILE* spGrades;
    if (!(spGrades = fopen ("BG_Scores.txt", "r")))
            {
            printf("\aError opening student grade file\n");
            return 100;
            }// if open input
    fclose(spGrades);
    
    
    return 0;
    }
    -Here it is! Hope thats what you wanted. Sorry again about the mess.

  2. #17
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by camel-man View Post
    @claudiu is it usually better to open files in main? I have noticed many programs choose to do that, rarely do I see a separate function that does it. Is there any reason why that is?
    Well, in my view there is no need to make another function call (which is computationally expensive) just to open a file, when you could do it wherever you are calling it from. In small programs like these it's done in main, in large programs it can be done elsewhere obviously.
    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.

  3. #18
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by hotshotennis View Post
    Code:
    #include<stdio.h>
    
    
    int main (void)
    {
    FILE* spGrades;
    if (!(spGrades = fopen ("BG_Scores.txt", "r")))
            {
            printf("\aError opening student grade file\n");
            return 100;
            }// if open input
    fclose(spGrades);
    
    
    return 0;
    }
    -Here it is! Hope thats what you wanted. Sorry again about the mess.
    Ok, no need to return 100 if the file is opened though, don't return anything but 0 from main at the end. In that branch where your open was successful try to read in the data from ONE STUDENT as I wrote above.
    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. #19
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also indent your code nicely, use spaces and tabs:

    i.e.

    Code:
    /*messy code*/
    int main(void)
    {
    int x=3,y=5;
    if(x!=3)
    {
    if(y!=5)
    {
    printf("yay");
    }
    }
    return 0;
    }
    nicer code

    Code:
    int main(void)
    {
        int x = 3, y= 5;
        if(x != 3)
        {
            if(y != 5)
            {
              printf("yay");
            }
        }
      return 0;
    }
    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. #20
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Ok, I got rid of the 100. Yeah, Ill try to be a little bit more careful with the indentation... I tend to leave it messy till the last minute and then clean up.

  6. #21
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Please repost your code after every change no matter how minor, so we always have the most updated copy. Also, have you been able to read the information for one student like I told you?
    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.

  7. #22
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Almost everything is done! Heres my code:
    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?

  8. #23
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Hey, this is looking like a real program now, well done.

    It looks to me like you have two arrays: students[] and finalscores[]. One way would be to sort both according to what is in finalscores.

    i.e. if finalscore[student_i] > finalscore[student_j]
    swap scores in finalscore array
    swap students in student array

    As for selection sort I recommend wikipedia. They have an excellent article on it including pseudocode and implementation in many languages.
    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.

  9. #24
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Bear in mind though, that while selection sort is one of the most inefficient sorting algorithms out there, (not that this matters too much for your problem) it is by far the easiest to remember and implement.
    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. #25
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Yeah, I'm pretty sure that our teacher wanted us to use selection sort. But I for the life of me can't figure out how to do it properly... and I'm not sure how to sort both of them according to what is in finalScores.

  11. #26
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Have you looked at the wiki page yet?

    The pseudocode is fairly simple:

    Code:
    loop through all students with index i
     loop through all students after index i as index j
       if grades[j] > grades[i]
          swap grades[i],grades[j]
          swap students[i], students[j]
    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. #27
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The idea of selection sort is fairly simple and intuitive: for each element in my array, inspect all the others after it and if any of them are larger (or smaller, depending on whether you are sorting ascending or descending) swap my initial element with the one that is larger(smaller). That's literally it.
    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. #28
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Off topic, but could you check this code:
    Code:
    #include <stdio.h>
    #define MAX_STUDENTS 50
    
           int stuID[MAX_STUDENTS];
    
           float finalScores[MAX_STUDENTS];
           int getScores();
           int calculateScores(char P_N, float homeworkTotal, float 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
    
           float 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
    
           float 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, float homeworkTotal, float midtermTotal, int final, int extraCredit)
    {
           float finalScore;
    
           if(P_N != 'P')
                   return 0;
           if(homeworkTotal <= 70)
                   return 0;
           if(midtermTotal <= 60)
                   return 0;
           if(final <= 60)
                   return 0;
    
    
                   finalScore = ((homeworkTotal + midtermTotal + final + extraCredit) / 3);
           return(finalScore);
    
    
    }
    
    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, "%.2f\t", finalScores[i]);https://mail.google.com/mail/images/cleardot.gif
    
    
    
    
    
                   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 wanted to change the grades to floating point numbers in order to make it more accurate, but now it printing 0.00 to the file for the grades. What did I do wrong?

  14. #29
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by claudiu View Post
    The idea of selection sort is fairly simple and intuitive: for each element in my array, inspect all the others after it and if any of them are larger (or smaller, depending on whether you are sorting ascending or descending) swap my initial element with the one that is larger(smaller). That's literally it.
    -Ill check out the wiki soon. My biggest concern is that once I sort the grades, they won't match the ID's. So how do I link the ID's back to the grades?

  15. #30
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by hotshotennis View Post
    -Ill check out the wiki soon. My biggest concern is that once I sort the grades, they won't match the ID's. So how do I link the ID's back to the grades?
    They will match the grades, because you will sort BOTH the grades and the ids. They are two parallel arrays, so when you swap two elements in one, you must swap the corresponding two in the other. That way you still end up with students having the correct grades.
    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