Thread: calculate average with an unknown number of inputs

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    1

    calculate average with an unknown number of inputs

    I'm supposed to write a program that outputs the average from an unknown amount of inputs. For that I will use a sentinel value (-1) to end the while loop. Also, the program will use a for loop that will call the function four times. Each time I will enter a new set of number and each time the average will be printed.

    Code:
    #include <stdio.h>
    
    
    void calculateAverage (void);
    
    
    int main(void) {
    
    
    int i;
    float average;
    
    
    for (i = 1; i <= 4; ++i) {
         calculateAverage();
    }
    
    
    return 0;
    }
    
    
    void calculateAverage (void) {
    
    
    int sumofGrades = 0;
    int numberofGrades = 0;
    float average = 0.0;
    int grade = 0;
    
    
            while(grade != -1 && grade >= 0 && grade <= 100) {
    
    
                    if(grade != -1) {
    
    
                  numberofGrades++;
                  sumofGrades += grade;
                    }
                  printf("Enter grades and type -1 to stop: \n");
                  scanf("%d", &grade);
    
    
    
    
    
    
            }
    
    
    
    
         average = (float)sumofGrades / numberofGrades;
         printf("The average is %.2f\n", average);
    
    
    }

    The output that I get is this:

    Enter grades and type -1 to stop:
    3 5 6 8 -1
    Enter grades and type -1 to stop:
    Enter grades and type -1 to stop:
    Enter grades and type -1 to stop:
    Enter grades and type -1 to stop:
    The average is 4.40
    Enter grades and type -1 to stop:


  2. #2
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi..

    The scanf is expecting only 1 int at a time....and the counting is off.....the first time round the while loop numberofGrades goes from 0 to 1, while the sumofGrades is set to 0 in the loop......so 0 is added to the average calculation. so.... (0 + 3 + 5 + 6 + 8)/5 = 4.4
    You don't want the printf for each time round the loop, so that maybe should be moved outside the loop....just depends how you deal with changes...
    Last edited by JohnGM; 10-05-2015 at 06:51 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading an Unknown Number of Inputs
    By Aeias in forum C++ Programming
    Replies: 13
    Last Post: 02-25-2012, 08:27 AM
  2. How to calculate the average of scores?
    By david.jones in forum C Programming
    Replies: 4
    Last Post: 05-02-2011, 06:33 AM
  3. Average of user inputs (in a loop)
    By SilentPirate007 in forum C Programming
    Replies: 13
    Last Post: 03-08-2010, 06:46 PM
  4. calculate average
    By archriku in forum C Programming
    Replies: 23
    Last Post: 04-10-2009, 04:27 AM
  5. calculate average from a file
    By mrsirpoopsalot in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2009, 02:25 PM

Tags for this Thread