Quesion problem

This is a discussion on Quesion problem within the C Programming forums, part of the General Programming Boards category; Hi, well i just need alil help with this question just a few tips and what the question is all ...

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Unhappy 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;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Quesion problem

    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  4. #4
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    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

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: HAve u read my question???

    Originally posted by datainjector
    Well man i guess u better read the question properly ...or did u post it at the wrong place
    MASTER
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    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
    'C that?... I felt nowt' - The Original Foffo Spearjig and his hard dog. The Tube TV show, sometime in the 80's

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21