Like Tree1Likes

Grades calculating program help please!

This is a discussion on Grades calculating program help please! within the C Programming forums, part of the General Programming Boards category; Originally Posted by claudiu You are calling getScores() in the print function. Remove that line. Does your read function work? ...

  1. #76
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by claudiu View Post
    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?
    Yes, I just realized that and removed it.
    And yes, my read function works. I tested it yesterday and today.
    Now when I print to output file, I get student ID's and grades swapped, so everyone has A+ lol

  2. #77
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can you post your latest code?
    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. #78
    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 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)
    {
    FILE* spSorted;
    
    
    
           if (!(spSorted = fopen ("BG_Sorted.txt", "w")))
           {
                   printf("\aError opening student grade file\n");
           }
    
           int i;
    
    
           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");
    
    
    
    
    
           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");
           }
    }
    

  4. #79
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can you provide your sample input that you are testing with in the input file and what the output file looks like?
    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. #80
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Do you want both the input and output files?

  6. #81
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Input:
    1234
    P
    100 100 100 100 100 100 100 100 100
    100 100
    100
    15
    1234
    P
    100 100 100 100 100 100 100 100 100
    100 100
    100
    15
    2381
    P
    100 100 100 100 100 100 100 100 100
    100 100
    100
    15
    3452
    N
    100 100 100 100 100 100 100 100 100
    100 100
    100
    15
    4702
    P
    100 95 99 92 90 96 97 100 100
    89 93
    91
    12
    5488
    P
    100 95 90 82 97 83 70 89 99
    95 52
    80
    10
    6569
    P
    70 70 60 50 40 30 30 20 10
    70 80
    90
    15
    7690
    P
    100 95 100 96 100 97 100 98 99
    50 40
    65
    15
    8789
    P
    99 85 92 78 80 90 70 85 93
    87 79
    82
    13
    9890
    P
    100 100 86 92 76 93 100 89 89
    70 80
    23
    15
    1111
    P
    80 90 75 100 78 89 97 50 95
    80 70
    75
    15

  7. #82
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Output:
    Spring 2012
    CIS 15 BG
    ==== ====== ====
    ID SCORE GRADE
    ==== ====== ====
    0 9890 A+
    87 8789 A+
    0 7690 A+
    0 6569 A+
    84 5488 A+
    96 4702 A+
    0 3452 A+
    105 2381 A+
    105 1234 A+
    105 1234 A+
    82 1111 A+

  8. #83
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, I guess I am pretty tired myself, between this and shouting at some idiots at work, because I can't tell what the bloody hell is going on that is swapping the two arrays.

    Let's take the following approach by making a little experiment:

    In main, after the read function try to printf both arrays so that we can see what is in them. Do this after the read, but before the sort, and again after the sort but before the print. One for loop should do it:

    i.e. for i = 0, i<stu_num i++ printf(ID[i],score[i])
    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. #84
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    
      8 int main (void)
      9 {
     10         int stuID[MAX_STUDENTS];
     11         int finalScores[MAX_STUDENTS];
     12         int stu_num;
     13         int i;
     14         stu_num = getScores(finalScores, stuID);
     15         for(i = 0; i < stu_num; i++)
     16         {
     17         printf("%d\n%d\n", stuID[i], finalScores[i]);
     18         }
     19
     20         sortScores(stuID, finalScores, stu_num);
     21         printToFile(stuID, finalScores, stu_num);
     22
     23         return 0;
     24 }
    -hahahaha its printing it out in reverse!
    Output:
    105
    1234
    105
    1234
    105
    2381
    0
    3452
    96
    4702
    84
    5488
    0
    6569
    0
    7690
    87
    8789
    0
    9890
    82
    1111

  10. #85
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Omg!!!!! I got it to work!!! Thank you so much!!!

  11. #86
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What was the problem?
    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. #87
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by claudiu View Post
    What was the problem?
    I started fixing the problem a while ago, and then when we did the printf, it confirmed the problem and brought my attention to the line above printf. Basically, we were passing the stuID and finalScores to the functions in reverse order. (hope that makes sense). So when I switched the order for each function, it worked.

    -Any other tweaks I should know about, or is this thing finally done?

  13. #88
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I think its done. You could add an fclose() at the end of the print function.
    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.

  14. #89
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Make sure you drink some coffee and don't sleep through your submission and let everything go to waste.
    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.

  15. #90
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    An fclose(spSorted) at the very end of the program, right?

Page 6 of 7 FirstFirst 1234567 LastLast
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, 05: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, 11:18 AM
  4. Calculating grades
    By Ducky in forum C++ Programming
    Replies: 0
    Last Post: 03-14-2008, 01:16 AM
  5. Replies: 13
    Last Post: 08-15-2002, 10:20 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21