Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 10-03-09   #1 (permalink)
New to Overclock.net
 
Join Date: Sep 2009
Posts: 18

Rep: 0 hsmith1976 Unknown
Unique Rep: 0
Trader Rating: 0
Default Need help with C program

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;
}
hsmith1976 is offline   Reply With Quote
Old 10-03-09   #2 (permalink)
Programmer
 
intel nvidia

Join Date: Jun 2009
Location: Italy
Posts: 231

Rep: 50 pippolo is acknowledged by some
Unique Rep: 46
Trader Rating: 0
Default

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 }
I think that you have a problem at the end... the program does a division by zero. But start from the beginning (the numbers are a reference to the rows):

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);
26) also this test is useless. At the row 23 you exit the loop if grade < 0, so the test is always false. You can write this:

Code:
} while (true);
30) and 31) is this an error ? you have to add the student's values to the total, but you are setting the total to the last student's values.
Don't forget to set them to 0 at lines 7 and 8.

Code:
totalnumgrades += gradenumber;
overallgrade += studenttotal;
34) the second part of the test (grade < 0) is always true at this point.

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;
}
You can make other optimizations/cleaning of the code, but this can be a start.
__________________
System: My System
CPU
2xQuadCore Intel Xeon E5420
Motherboard
Tyan Tempest i5400XT
Memory
16GB 8xSamsung M395T5750 FBDIMM
Graphics Card
XFX 9800GTX
Hard Drive
16 Internal, 8 external. Mainly Seagate
Sound Card
Sound Blaster X-FI Platinum Fatal1ty Champion
Power Supply
Enermax Galaxy 1000W EGA1000EWL
Case
Chieftec (modified)
CPU cooling
Cooler Master 3U-Attiva S3N-7DWHS-L5-GP
GPU cooling
Standard
OS
Vista Ultimate x64
pippolo is offline   Reply With Quote
Old 10-03-09   #3 (permalink)
New to Overclock.net
 
Join Date: Sep 2009
Posts: 18

Rep: 0 hsmith1976 Unknown
Unique Rep: 0
Trader Rating: 0
Default

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.
hsmith1976 is offline   Reply With Quote
Old 10-03-09   #4 (permalink)
New to Overclock.net
 
Join Date: Sep 2009
Posts: 18

Rep: 0 hsmith1976 Unknown
Unique Rep: 0
Trader Rating: 0
Default

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?
hsmith1976 is offline   Reply With Quote
Old 10-03-09   #5 (permalink)
New to Overclock.net
 
Join Date: Sep 2009
Posts: 18

Rep: 0 hsmith1976 Unknown
Unique Rep: 0
Trader Rating: 0
Default

This is what my code looks like now.
Quote:

#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++;
studenttotal = studenttotal + grade;


} while ( grade = -1 );
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;
grade = 0;
studenttotal = 0;
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;
}
hsmith1976 is offline   Reply With Quote
Old 10-04-09   #6 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

You're still not keeping running totals of the number of students and their grades.
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 10-04-09   #7 (permalink)
New to Overclock.net
 
Join Date: Sep 2009
Posts: 18

Rep: 0 hsmith1976 Unknown
Unique Rep: 0
Trader Rating: 0
Default

totalnumgrades = gradenumber;
overallgrade = studenttotal;

This is where the running totals of the number of students and their grades is located unless I am wrong.
hsmith1976 is offline   Reply With Quote
Old 10-04-09   #8 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Code:
totalnumgrades = gradenumber;
overallgrade = studenttotal;
No, that's not a running total. You need to do this:
Code:
totalnumgrades = totalnumgrades  + gradenumber;
overallgrade = overallgrade + studenttotal;
Or more concisely:
Code:
totalnumgrades += gradenumber;
overallgrade += studenttotal;
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 10-04-09   #9 (permalink)
New to Overclock.net
 
Join Date: Sep 2009
Posts: 18

Rep: 0 hsmith1976 Unknown
Unique Rep: 0
Trader Rating: 0
Default

Ok with changing that it doesn't change the program any I still end up with the same result it will not end when -1 is entered as the first grade.
hsmith1976 is offline   Reply With Quote
Old 10-04-09   #10 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Quote:
Originally Posted by hsmith1976 View Post
Ok with changing that it doesn't change the program any I still end up with the same result it will not end when -1 is entered as the first grade.
Yep. You have 3 loops that need to exit when -1 is entered. Obviously, the conditions/expressions to do that aren't all correct.
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 01:24 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.15269 seconds with 8 queries