|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Need help with C program
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
New to Overclock.net
|
I have this program that I am working on using C. The code compiles and runs I am just a little lost with the last portion of it. The purpose of the program is for the user to enter grades for multiple students. When the user has finished with one student they enter -1 at that point the the average is displayed. Then it is suppose to reset and start with student 2 and so on.
***This is the part I am having problems with. When the user is done they enter a -1 in the grade 1 to indicate no more students. Then it should produce the average for all students. Any assistance would be a big help. #include <stdio.h> int main() { int studentquantity = 1; int gradenumber = 1; int grade; int studenttotal = 0; int totalnumgrades; int overallgrade; float average; float classaverage; printf("To change change students enter -1\n"); printf("When completed with list enter -1 as first score\n\n"); do { do { do { printf("Student #%d, grade #%d = ", studentquantity, gradenumber); scanf("%d", &grade ); if (grade > 100) printf("Grade is invalid, try again\n"); else break; } while ( grade != -1 ); if (grade < 0 ) break; gradenumber = gradenumber + 1; studenttotal = studenttotal + grade; } while ( grade != -1 ); gradenumber--; average = ( float ) studenttotal / gradenumber; printf("Student #%d had %d grade(s), average is %.2lf\n", studentquantity, gradenumber, average); totalnumgrades = gradenumber; overallgrade = studenttotal; gradenumber = 1; studentquantity++; } while ( ( gradenumber == 1) && (grade == -1) ); if ( ( gradenumber == 1) && (grade == -1) ) classaverage = (float) overallgrade / totalnumgrades; printf("There were %d student(s) with an average of %.2fl\n", studentquantity, classaverage); system("PAUSE"); return 0; } |
|
|
|
|
|
#2 (permalink) | ||||||||||||
|
Programmer
![]() |
As first I numbered the lines for reference and formatted the code:
__________________Code:
01 #include <stdio.h>
02 int main() {
03 int studentquantity = 1;
04 int gradenumber = 1;
05 int grade;
06 int studenttotal = 0;
07 int totalnumgrades;
08 int overallgrade;
09 float average;
10 float classaverage;
11 printf("To change change students enter -1\n");
12 printf("When completed with list enter -1 as first score\n\n");
13 do {
14 do {
15 do {
16 printf("Student #%d, grade #%d = ", studentquantity, gradenumber);
17 scanf("%d", &grade);
18 if (grade > 100)
19 printf("Grade is invalid, try again\n");
20 else
21 break;
22 } while (grade != -1);
23 if (grade < 0) break;
24 gradenumber = gradenumber + 1;
25 studenttotal = studenttotal + grade;
26 } while (grade != -1);
27 gradenumber--;
28 average = (float) studenttotal / gradenumber;
29 printf("Student #%d had %d grade(s), average is %.2lf\n", studentquantity, gradenumber, average);
30 totalnumgrades = gradenumber;
31 overallgrade = studenttotal;
32 gradenumber = 1;
33 studentquantity++;
34 } while ((gradenumber == 1) && (grade == -1));
35 if ((gradenumber == 1) && (grade == -1))
36 classaverage = (float) overallgrade / totalnumgrades;
37 printf("There were %d student(s) with an average of %.2fl\n", studentquantity, classaverage);
38 system("PAUSE");
39 return 0;
40 }
22) the test is useless. based on the preceding IF statement, you can reach this point only if the IF is true (grade > 100) because if grade <= 100 the loop is exited with the BREAK directive. So, the WHILE test is always false and useless. A thing like this is probably simpler: Code:
do {
printf("Student #%d, grade #%d = ", studentquantity, gradenumber);
scanf("%d", &grade);
if (grade <= 100) break,
printf("Grade is invalid, try again\n");
} while (true);
Code:
} while (true); Don't forget to set them to 0 at lines 7 and 8. Code:
totalnumgrades += gradenumber; overallgrade += studenttotal; 35) why the IF ? the row 36 has to be executed. Only if the number of entered students is 0 you can avoid the summary. Now we can take a look at the code flow. Starting from the row 27 you go through the next code when the user enters -1 in two different cases: a) the user entered -1 at the end of the inputs for a single student (gradenumber > 1). b) the user entered -1 at the first grade to indicate the end of the program (gradenumber == 1) In case a) (and correcting the errors at 30 and 31) the execution go to 34) and go to the beginning of the loop (because the test is true). But at the row 33 you are increasing studentquantity. This is ok, but if at the next iteration the user enters -1 at the first grade, the final studentquantity will be wrong. You have to reduce the value of 1 before to reach row 36. But in case b) the following happen: - there is no test and all the code between 27 and 33 is executed. - 27) gradenumber is 1 and after the line is reduced to 0. - 28) you are dividing by 0 and there is a runtime error. - 32) you are setting gradenumber to 1 - 34) since (gradenumber == 1) and (grade == -1) the loop restart. This is a suggestion/example for a correct (I hope, I'm not tried it): Code:
#include <stdio.h>
int main() {
int studentquantity = 1;
int gradenumber = 1;
int grade;
int studenttotal = 0;
int totalnumgrades = 0;
int overallgrade = 0;
float average;
float classaverage;
printf("To change change students enter -1\n");
printf("When completed with list enter -1 as first score\n\n");
do {
do {
while (true) {
printf("Student #%d, grade #%d = ", studentquantity, gradenumber);
scanf("%d", &grade);
if (grade <= 100) break;
printf("Grade is invalid, try again\n");
};
if (grade < 0) break;
gradenumber++;
studenttotal += grade;
} while (true);
if (gradenumber > 1) {
gradenumber--;
average = (float) studenttotal / gradenumber;
printf("Student #%d had %d grade(s), average is %.2lf\n", studentquantity, gradenumber, average);
totalnumgrades += gradenumber;
overallgrade += studenttotal;
gradenumber = 1;
studentquantity++;
}
} while (gradenumber == 1);
--studentquantity;
if (studentquantity > 0) {
classaverage = (float) overallgrade / totalnumgrades;
printf("There were %d student(s) with an average of %.2fl\n", studentquantity, classaverage);
}
system("PAUSE");
return 0;
}
|
||||||||||||
|
|
|
|
|
#3 (permalink) |
|
New to Overclock.net
|
Thanks but some of the suggestions you make don't help for what the program is suppose to be doing. As I stated in my original post the code I posted worked as expected except when it went to change student I have to problem and also when I am trying to configure the average for all students.
|
|
|
|
|
|
#4 (permalink) |
|
New to Overclock.net
|
when I use the above suggestions in my code I get the correct results for the first students set of grades just like I did with my original program. I am able now to enter grades for the next student and continue doing so but when I enter -1 as the first grade the program is suppose to break and give me the average for the class. This is not happening. How can I fix this?
|
|
|
|
|
|
#5 (permalink) | |
|
New to Overclock.net
|
This is what my code looks like now.
Quote:
|
|
|
|
|
|
|
#8 (permalink) |
|
Case Modder
![]() |
Code:
totalnumgrades = gradenumber; overallgrade = studenttotal; Code:
totalnumgrades = totalnumgrades + gradenumber; overallgrade = overallgrade + studenttotal; Code:
totalnumgrades += gradenumber; overallgrade += studenttotal;
__________________
Rich Custom Wooden Case Builder
|
|
|
|
|
|
#10 (permalink) | |
|
Case Modder
![]() |
Quote:
__________________
Rich Custom Wooden Case Builder
|
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|