# Quesion problem

Printable View

• 06-30-2002
datainjector
Quesion problem
Hi, well i just need alil help with this question just a few tips and what the question is all about..

Q4.12).Modify the program of fig 4.7 so that it calculates the average grade for the class.

Well the problem is that it says to calculate the average grade well i can do it when the grades were integers but they are Characters how can i find the average of a character????

/* Fig. 4.7: fig04_07.c
Counting letter grades */
#include <stdio.h>

int main()
{
int grade;
int aCount = 0, bCount = 0, cCount = 0,
dCount = 0, fCount = 0;

printf( "Enter the letter grades.\n" );
printf( "Enter the EOF character to end input.\n" );

while ( ( grade = getchar() ) != EOF ) {

switch ( grade ) { /* switch nested in while */

case 'A': case 'a': /* grade was uppercase A */
++aCount; /* or lowercase a */
break;

case 'B': case 'b': /* grade was uppercase B */
++bCount; /* or lowercase b */
break;

case 'C': case 'c': /* grade was uppercase C */
++cCount; /* or lowercase c */
break;

case 'D': case 'd': /* grade was uppercase D */
++dCount; /* or lowercase d */
break;

case 'F': case 'f': /* grade was uppercase F */
++fCount; /* or lowercase f */
break;

case '\n': case' ': /* ignore these in input */
break;

default: /* catch all other characters */
printf( "Incorrect letter grade entered." );
printf( " Enter a new grade.\n" );
break;
}
}

printf( "\nTotals for each letter grade are:\n" );
printf( "A: %d\n", aCount );
printf( "B: %d\n", bCount );
printf( "C: %d\n", cCount );
printf( "D: %d\n", dCount );
printf( "F: %d\n", fCount );

return 0;
}
• 06-30-2002
Hammer
Re: Quesion problem
Quote:

Originally posted by datainjector
Well the problem is that it says to calculate the average grade well i can do it when the grades were integers but they are Characters how can i find the average of a character????

Easy, you know how many people got each grade, you already have that info in aCount, bCount etc. You know the total number of grades.... it's simple math to get the average. :)
• 07-01-2002
master5001
Your code looks good thus far :) You may consider using floats or doubles instead of ints (unless you wanted to do fixed point math) so you can have some decimal precision.
• 07-01-2002
datainjector
HAve u read my question???
Well man i guess u better read the question properly ...or did u post it at the wrong place
MASTER:confused:
• 07-01-2002
quzah
Re: HAve u read my question???
Quote:

Originally posted by datainjector
Well man i guess u better read the question properly ...or did u post it at the wrong place
MASTER:confused:

You calculate it the same way you normally would:

A = 4
B = 3
C = 2
D = 1
F = 0

Assign a numerical value for the entered letter grade to the total score, and increment the "number of grades" variable.

When you're done, divide the total score by the number of grades.

Quzah.
• 07-02-2002
Foffo Spearjig
Characters are stored as ints anyway, A = decimal 65, B = dec' 66 etc. You should be able to typecast to ints, find the average, probably typecast the answer to float, then round up or down and cast back to char. Though to be fair this is probably more work and less accurate than the other methods!

foffo