Average Calculation Error

This is a discussion on Average Calculation Error within the C Programming forums, part of the General Programming Boards category; I am having trouble with calculating the average of a series of grades entered. Here is my code. Any help ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    16

    Average Calculation Error

    I am having trouble with calculating the average of a series of grades entered. Here is my code. Any help would be appreciated.

    insert
    Code:
    #include<stdio.h>
    
    float calculateAverage (float total, int items)
    {
        return total / items;
    }
    
    int main(void)
    {
    
        float classaverage, runningsum, inputnumber, high, low;
    
        int testamount;
        int counter = 1;
    
        char letter_grade;
    
        printf("enter amount of tests: ");
        scanf("%d", &testamount);
    
        printf("Enter test grade: ");
        scanf("%f", &inputnumber);
        runningsum = inputnumber;
        high = low = inputnumber;
    
        while (testamount > counter)
    {
    
        printf("Enter test grade: ");
        scanf("%f", &inputnumber);
    
        if (inputnumber < 0)
    	printf("The number you entered is negative. It will be ignored in calculations.\n");
        else if (inputnumber > 100)
    	printf("The number you entered is greater than 100. It will be ignored in calculations.\n");
        else if (inputnumber < 60)
    	letter_grade == 70;
        else if (inputnumber < 70)
    	letter_grade == 68;
        else if (inputnumber < 80)
    	letter_grade == 67;
        else if (inputnumber < 90)
    	letter_grade == 66;
        else if (inputnumber <= 100)
    	letter_grade == 65;
    
    
    	runningsum += inputnumber;
    
        counter++;
    
    }
    
        classaverage = calculateAverage(runningsum, testamount);
    
        printf("the class average is %g.\n", &classaverage);
    
    
        printf("\nhigh:\t%f\tlow:\t%f\n", high, low);
    
    
    return 0;
    }

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    The main problem I can see is this line:
    Code:
    printf("the class average is %g.\n", &classaverage);
    You want to get ride of the '&'.

    I don't know if you realise this, but the variables 'high' and 'low' just contain the first in the sequence - They aren't the high and low of the list you enter.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    16
    Thank you so much! That worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 04:55 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  4. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  5. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM

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