Thread: Data type problems, float, int, etc with averages

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    10

    Data type problems, float, int, etc with averages

    Hi guys, i'm having a little trouble getting my GPA average calculator to work as one of the challenges from the book. The sample data is:

    GPA = 3.5, 2.8, 3.0, 2.5, 4.0, 3.7
    avg GPA = 3.25

    i can't seem to get the sample data above to work. For me i think there something wrong with the data types. Shouldn't i be using float for decimal type values? Correct me if i'm wrong.

    Code:
    #include <stdio.h>
    
    float iGPA[29];
    
    int main(void) {
    
    int x;
    float countGPA = 0;
    int iSelection;
    float eGPA;
    float gpaTOTAL;
    float avgGPA = 0;;
    
    printf("\n\tGPA Average Calculator\n");
    
    
    for (x = 0; x < 29; x++) {
    
    
        printf("\nEnter gpa: ");
        scanf("%f", &eGPA);
    
        iGPA[x] = eGPA;
    
        countGPA++;
    
        printf("\n1\tAdd GPA: \n");
        printf("2\tCalculate average GPA: ");
        printf("\nEnter selection: ");
        scanf("%d", &iSelection);
    
    
        if (iSelection == 2) {
            break;
        }
    
    
    }
    
    int y;
    
    for (y = 0; y < countGPA; y++) {
    
    gpaTOTAL = iGPA[y];
    gpaTOTAL += gpaTOTAL;
    
    
    
    }
    avgGPA = gpaTOTAL / countGPA;
                printf("\nAverage GPA is: %f", avgGPA);
    
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look at the following snippet:
    Code:
    for (y = 0; y < countGPA; y++) {
    gpaTOTAL = iGPA[y];
    gpaTOTAL += gpaTOTAL;
    }
    Each time through your loop you reset gpaTOTAL to iGPA[y], erasing anything in the total. The following should get you closer.
    Code:
    gpaTOTAL = 0.0;
    for (y = 0; y < countGPA; y++) {
       gpaTOTAL += iGPA[y]; 
    }
    Jim

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Each time through the loop, this will overwrite the previous value of gpaTOTAL:
    Code:
    gpaTOTAL = iGPA[y];
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Yep - the problem is in the summing loop, as described in the last post.

    You data types are mostly ok. Everything that needs to be a float is a float. I wouldn't use a float for countGPA though. There's no need for it, it's just a counter, it can be an int.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    10

    Thanks

    Hi guys thanks,

    I originally had that, but i think i forgot to change all the floats that were originally ints, which kept giving me zero average. I understand now. I appreciate it guys.

    Sincerely,
    Jomoka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-14-2011, 09:26 PM
  2. Float Data Type Ranges
    By GokhanK in forum C Programming
    Replies: 1
    Last Post: 02-26-2011, 03:17 PM
  3. Float data type issue
    By Figen in forum C Programming
    Replies: 2
    Last Post: 01-31-2011, 01:42 PM
  4. C - float and double data-type question..
    By ShadeS_07 in forum C Programming
    Replies: 10
    Last Post: 07-14-2008, 06:06 AM
  5. float data type question
    By Mark S. in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2006, 09:34 AM