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; -Another adjustment: got rid of int temp. I wasn't using it....

  1. #46
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    -Another adjustment: got rid of int temp. I wasn't using it.

  2. #47
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    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;
                    }
            }
    }

  3. #48
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    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. #49
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Something tells me your program wouldn't work if I entered "62.99" or any similar value.

  5. #50
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Hold your horses memcpy, we are getting to that.
    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.

  6. #51
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    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;
                    }
            }
    }
    -Do I need to set tempData equal to stuID?

  7. #52
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by memcpy View Post
    Something tells me your program wouldn't work if I entered "62.99" or any similar value.
    I don't think that I have to worry about that. The user is never prompted to enter anything. Everything is already read from a file and assumed to be correct.

  8. #53
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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).
    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. #54
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    I'm a little bit confused by what you mean...
    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;
                    }
            }
    }
    -Is this kind of what its supposed to look like?

  10. #55
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Or should I make a tempData2 for the stuID or something?

  11. #56
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    This is what you originally had:
    Code:
    tempData = finalScores[current];
    finalScores[current] = finalScores[walk];
    finalScores[walk] = tempData;
    This swaps two numbers, finalScores[current] and finalScores[walk] using an intermediary third, tempData.
    Swap stuID[current] and stuID[walk] using the same strategy right after swapping the scores.

    More clear?
    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. #57
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    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;
    
    
    
    
    
    
                    }
            }
    }

  13. #58
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    I noticed that I missed the ;
    I fixed it in my code.

  14. #59
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    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. #60
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    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



Page 4 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, 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, 07: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


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