Skip to main content

Section 6.5 Averages

At this point, we have already learned all the tools needed to calculate the average of a set of numbers!

Activity 6.3.

Write a C program that finds the average of integers, entered by the user. First, the user should be asked to enter the number of integers they wish to average, then they should be asked to enter those integers and their average should be calculated.
Here is a sample run of your program (with simulated user input):
How many integers do you wish to average? 3
Please enter an integer: 8
Please enter an integer: 12
Please enter an integer: 101
The average is 40.333333.
Recall that the average needs to be of type float, please store it in the variable ave which has already been declared for your convenience.

Compile and run the code below in your own C environment.

#include <stdio.h>

int main(void)
{
    /* Complete the code below. Remember to declare any 
       additional variables needed. */
    float ave;
    int N, num, i;
    
    printf("How many integers do you wish to average?");
    scanf("%d", &N);
 
    for (i=0; i<N; i++) {
        printf("Please enter an integer: ");
        scanf("%d", &num);
        
    }
    
 
 
 
    printf("The average is %f.\n", ave);

   return(0);   
}