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 They will match the grades, because you will sort BOTH the grades and the ids. They ...

  1. #31
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by claudiu View Post
    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.
    I looked at the wiki. It seems to use variables everywhere, and I don't know where to put in my two arrays... is that what I use for index i and index j(like you used in the psuedocode above). Sorry if my questions are getting dumber by the minute, but I've been at this freaking program for 15 hours now. (and thats just including today)

  2. #32
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The indexes i and j are what you use to navigate an array, presumably in a loop:

    Like: for(i = 1....)
    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. #33
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I am not going to feed you the direct answer, despite your admirable struggle to get this finished, you are very close and you can finish it by yourself . You have no idea how many people come here asking for free handouts. You actually put through the effort and did the work and when you get the sort done, it will be that much more rewarding. It will also teach you an important life lesson: that with enough effort and discipline you can get anything done, even something completely new and unfamiliar.
    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. #34
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by claudiu View Post
    I am not going to feed you the direct answer, despite your admirable struggle to get this finished, you are very close and you can finish it by yourself . You have no idea how many people come here asking for free handouts. You actually put through the effort and did the work and when you get the sort done, it will be that much more rewarding. It will also teach you an important life lesson: that with enough effort and discipline you can get anything done, even something completely new and unfamiliar.
    Ok, give me a few minutes. Ill try to battle it out. Any suggestion on how to get the scores to be printed as floats though?

  5. #35
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Sorry, the pseudocode I gave you was for insertion sort not selection sort. I got confused between the two, because they are essentially very similar and I don't use any of them on a regular basis.

    But feel free to use the pseudocode I gave you, it is probably simpler than selection sort anyway.
    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. #36
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by claudiu View Post
    Sorry, the pseudocode I gave you was for insertion sort not selection sort. I got confused between the two, because they are essentially very similar and I don't use any of them on a regular basis.

    But feel free to use the pseudocode I gave you, it is probably simpler than selection sort anyway.
    Ok, I just started it. Not sure how the who student ID matching thing is going to work out. But ill post what I have in a little bit.

  7. #37
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Printing floats is done with %f instead of %d.

    See this page for details:

    printf - C++ Reference
    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. #38
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by claudiu View Post
    Printing floats is done with %f instead of %d.

    See this page for details:

    printf - C++ Reference
    Yeah I printed with %f, it won't make a difference... I think I have to change a lot of things to float. Anyway, I decided to leave them as ints. I'll post my selection sort attempt soon.

  9. #39
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    int sortScores(int finalScores[MAX_STUDENTS], int first)
    {
    int largest;
    int tempData;
    int current;
    int walk;
            for(current = 0; current > first; current++)
            {
            largest = current;
            for (walk = current + 1;
                    walk >= first;
                    walk++)
            if (finalScores[walk] > finalScores[largest])
                    largest = walk;
            tempData = finalScores[current];
            finalScores[current] = finalScores[largest];
            finalScores[largest] = tempData;
            }
    return;
    }
    -Here is my probably failed attempt. My textbook has a selection sort from lowest to highest, so I tried to reverse it to make it highest to lowest. Let me know what you think.

  10. #40
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, this is partly my fault because I confused you between selection and insertion sort. Let's quickly change this to insertion since it is much simpler.

    You need that sort function to take your grades and your students arrays as parameters and the total number of students. So three params: float[], int[] and int (for num_students).

    Then change your first for(current) to iterate the entire grades array, from 0 to num_student-1. Your second for should iterate from current+1 to the same end num_student-1. Then if finalScores[current]<finalScores[walk] swap finalScores, and ALSO SWAP students[current], students[walk].

    Give that a try and post back.
    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.

  11. #41
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    void sortScores(int finalScores[], int stuID[], int(for(MAX_STUDENT))
    {
    int current;
    int walk;
            for(current = 0; current > MAX_STUDENT - 1; current++)
            {
                    for (current + 1; current> MAX_STUDENT; walk++)
                    if(finalScores[current] < finalScores[walk]
                            finalScores = current;
                            stuID = current;
    
    
            }
    It looks horrible and is probably far from right(hopefully not!)

  12. #42
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It is actually surprisingly close. Your for conditions are wrong. You are asking for current to be larger than MAX_STUDENT when it should be up to MAX_STUDENT for obvious reasons. You are not using walk in your second for, but current again, you need two separate iterators. Also, everything in your if statement must be in brackets and you must have an actual swap using a temporary variable like you did for the previous version you attempted.

    What the hell is int(for(MAX_STUDENT))???

    Compile your code BEFORE posting here and make sure you have no errors.
    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. #43
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    void sortScores(int finalScores[], int stuID[], int MAX_STUDENT)
    {
    int current;
    int walk;
    int temp;
    int largest;
    int tempData;
            for(current = 0; current <= MAX_STUDENT - 1; current++)
            {
                    for (walk + 1; walk <=  MAX_STUDENT; walk++)
                    if(finalScores[current] < finalScores[walk])
                    {
                    largest = walk;
                    tempData = finalScores[current];
                    finalScores[current] = finalScores[largest];
                    finalScores[largest] = tempData;
            }
                    }
    
    
    
    
    return;
    }
    -I can't believe I was close! haha Anyway, heres the update.

  14. #44
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok. You need to stop for a minute and think. Don't just barf something in your editor and think it is correct. Think about what it is you are doing.

    You have two loops, going through the same array. The array has positions starting at 0 and ending at num_student-1, where num_student IS THE NUMBER OF STUDENTS YOU READ FROM THE FILE.

    The first loop looks fine, although the indentation of the brackets is confusing as hell. You are matching the first for opening bracket with the if clause closing bracket and vice versa.

    What the hell is walk+1? Walk is an iterator to navigate through the array so you can say, "hey finalScores[walk] what the hell is your value"? Walk needs to start at 1 position after current so that it iterates OVER the REMAINING OF THE ARRAY THAT COMES AFTER POSITION current.

    Get rid of largest. We don't need it here, jsut use walk. All you need is tempData, walk and current (and obviously the parameters you are passing in, the arrays and the number of students).

    Get rid of the return at the end of the function, this function does not return anything hence the word void at the beginning of its prototype.

    I'm off on my lunch break. I expect a better version soon.
    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. #45
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    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);
    
    
    
    
    }
    -Sorry, I'm not trying to put up crap code. And I don't mean to be making excuses, but I am super tired and a bit brain dead, so sorry about the retarded code. Anyway, heres another version(hopefully better). Also, I can't check it with my editor because it tells me that I have an incompatible pointer type.(might be something wrong with the way that I'm calling the function). Anyway, I figured it would be better to get the sorting to work before dealing with the warnings my compiler is giving me.

Page 3 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