1. ## 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!

2. 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.

3. Originally Posted by hotshotennis
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?

4. 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. 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?

6. Originally Posted by GReaper
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. 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.

8. Originally Posted by claudiu
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. 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. 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.

11. 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.

12. 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 readData(FILE* spGrades, int* stuID[MAX_SIZE], int i, int* ID);
int readHWScores(FILE* spGrades, int i, int HWaverage);

int main (void)
{

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;
int* ID;
int HWaverage;

return 0;
}

{
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)
{

}*/

for (i=1; i <=EOF; i+=6)
{

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;

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. 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.

14. @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. 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.

Popular pages Recent additions