Thread: Grades calculating program help please!

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    Grades calculating program help please!

    Hello everyone,
    I am a programming beginner taking intermediate C. Our teacher assigned a program for us to write and I've been going crazy trying to figure out how to put it together. I attached the two things that we need for the assignment and the instructions.
    I did get one of the functions to work. I'm able to open the file that I want successfully. My issue is that I have no logical idea about how to go about scanning the information from the file in. I mean, the first number is the student ID, and it repeats every 6 lines and that needs to be scanned into an array. I will probably figure out how to do that part. However, how to scan the actual data and calculate it, is killing me.... The second line has 9 homework scores for example, so does this mean that I have to make int HW1, int HW2, int HW3.... int HW9?! and then add those two up and divide to get the average? I seems a little insane and like there should be an easier way... And how would I put the results of everything back? please help me out! (If you could try to keep the explanation as simple as possible, I would really appreciate it. I tend to get lost with all of the fancy terminology and such)

    Thank you for your time!
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Announcements - C Programming
    Announcements - General Programming Boards
    No code => no help

    > My issue is that I have no logical idea about how to go about scanning the information from the file in
    You should try reading some other peoples posts then.
    How to read a file posts appear several times a week.

    Plus, .doc files are a horrid file format for sharing information.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by hotshotennis View Post
    so does this mean that I have to make int HW1, int HW2, int HW3.... int HW9?! and then add those two up and divide to get the average? I seems a little insane and like there should be an easier way...
    Assuming that you don't know about arrays yet, and since you don't need to save all of the scores:
    Add each score in a total sum and keep track of the number of scores. When you're done retrieving them just do a division. Now, it's not that hard, is it?
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    We've learned about 1D arrays and I'm supposed to make 2 arrays. One with the student IDs and one with the final grades.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    The idea is to have a loop. Keep reading the ID, the P/N letter and the scores until there's nothing else to read, saving the ID and the final grade as you go. What are you confused about?
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by GReaper View Post
    The idea is to have a loop. Keep reading the ID, the P/N letter and the scores until there's nothing else to read, saving the ID and the final grade as you go. What are you confused about?
    Just that! How do I go about doing that? I can even get my program to scan the ID's and put them into an array properly. Let alone scanning the rest of the file and getting a final grades calculated and put into another array.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I suggest you read the tutorial we have here on arrays and such. The C Tutorials should appear under "Popular pages" at the bottom of this page.
    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. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by claudiu View Post
    I suggest you read the tutorial we have here on arrays and such. The C Tutorials should appear under "Popular pages" at the bottom of this page.
    Ok, I'll give it a try, thanks.

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    I read through all of it, even took the quizzes. There wasn't really anything in the tutorial that I didn't already know. This still gives me no help with the program and its quite frustrating. I feel like in class, we learn basic stuff like 1+1 and then for homework, we have to calculate the mass of the sun. This is all just a figure of speech of course, and doesn't apply to C language. But the idea is the same, and its annoying to feel this way. Any help?

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I am sorry you found it frustrating, but the learning process often tends to be like that. Homework is meant to make you think beyond what you learn in class, so don't get discouraged, look at it as an opportunity.

    As for your program, do you have any code produced so far? If so post it here.
    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. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    One tends to build a program like a house, you have to start somewhere with very basic stuff. Once you have a solid base, you add more to it, and so 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.

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    I have done stuff. But its very messy, and barely anything works... Some parts are commented out cause I was experimenting with different methods. Anyway, here it is:
    Code:
    #include <stdio.h>
    #define MAX_SIZE 50
    int openFile(FILE* spGrades);
    int readData(FILE* spGrades, int* stuID[MAX_SIZE], int i, int* ID);
    int readHWScores(FILE* spGrades, int i, int HWaverage);
    
    
    int main (void)
    {
    FILE* spGrades;
    
    int* stuID[MAX_SIZE];
    int HW1;
    int HW2;
    int HW3;
    int HW4;
    int HW5;
    int HW6;
    int HW7;
    int HW8;
    int HW9;
    int exam1;
    int exam2;
    int final;
    int i;
    char grade;
    int* ID;
    int HWaverage;
    
    openFile(spGrades);
    readData(spGrades, stuID, i, ID);
    
    
    /*readHWScores(spGrades, i, HWaverage);*/
    
    return 0;
    }
    
    int openFile(FILE* spGrades)
    {
    if (!(spGrades = fopen ("BG_Scores.txt", "r")))
           {
            printf("\aError opening student grade file\n");
           return 100;
           }// if open input
    
    else
           printf("Open success\n");
    }
    
    int readData(FILE* spGrades, int* stuID[MAX_SIZE], int i, int* ID)
    {
    
    /*if(spGrades){
           while(fscanf(spGrades, "%d", &ID)!=EOF);
    }*/
    
    for (i=1; i <=EOF; i+=6)
    {
    fscanf( spGrades,"%d\n", &ID);
    
    stuID[i] = ID;
    }
    for (i=0; i<=MAX_SIZE; i++)
    {
    printf("%d\n", stuID);
    }
    return 0;
    
    
    }
    int readHWScores(FILE* spGrades, int i, int HWaverage)
    {
    int HW1;
    int HW2;
    int HW3;
    int HW4;
    int HW5;
    int HW6;
    int HW7;
    int HW8;
    int HW9;
    
    char x;
    
    if(spGrades){
           while(fscanf(spGrades, "%d %d %d %d %d %d %d %d %d", &HW1, &HW2, &HW3, &HW4, &HW5, &HW6, &HW7, &HW8, &HW9)!=EOF);
    }
    printf("%d", HW1);
    
    
    for (i=3; i <=EOF; i+6)
    fscanf( spGrades, "%d %d %d %d %d %d %d %d %d\n", &HW1, &HW2, &HW3, &HW4, &HW5, &HW6, &HW7, &HW8, &HW9);
    HWaverage = ((HW1 + HW2 + HW3 + HW4 + HW5 + HW6 + HW7 + HW8 + HW9)/9);
    printf("%d\n", HWaverage);
    printf("%d\n", HW1);
    
    for(i=2; i!=EOF; i+=6)
    {
    while ((x = fgetc(spGrades))!=EOF)
    {
    printf("%c", x);
    }
    }
    
    return 0;
    }
    -Hope your eyes don't bleed. Thanks for the help!

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, you are right this is very messy, but you have the right ideas. Keep that copy of the code just in case and start a new file, so we can clean this up. In the new file, declare your main and see if you can open the file (move the code from your open file function in main). Don't read anything yet, just remember to close the file before returning from main with fclose(). Compile and make sure everything is fine, then post again.
    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. #14
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    @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?

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I have to go to bed soon, since it is very late here but anyway: after you do what I said previously, and the code is CLEAN, INDENTED and CORRECT (as in it compiles). Try to read ONE student and his scores from your input file. JUST ONE. Get that to be CLEAN, INDENTED and CORRECT (make sure you can printf the values of the variables you read into, to make sure you have done so successfully).

    After that is done, I will probably be able to post again to help you in the morning. Take this easy, methodically, and slowly and don't get bogged down by all the complexities that arise when you first read the problem. Start small scale, build up.
    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