C program to calculate the average using arrays

 Here. we will learn how to calculate the average of numbers that are stored in an array.

C program to calculate the average using arrays
C program to calculate the average using arrays


To calculate the average of numbers stored inside the array first we will store the numbers inside the array and compute the sum of all the numbers inside the array and then divide the sum using the number of elements in the array. To understand the working of this program you should have the knowledge of arrays and for loop or while loop.

Learn about Arrays and for-loop or while-loop.

Lets take an example 

if n=5 and arr[5] = {1, 2, 3. 4. 5};

first we will take sum of the 5 elements present inside the array

sum = 1+2+3+4+5 = 15 

After getting the sum will divide the sum by n, avg = sum/n = 15/5 = 3.


Algorithm:-

  1.  Start.
  2.  Input n from the user.
  3.  Create  array of integers of n size.
  4.  Then input n elements from the user and store in the array.
  5.  Run loop from i=0 to n, and inside the loop compute sum.
  6.  After computing sum, compute the average by dividing the sum by n.
  7.  Print the average on the output screen.
  8.  Stop.

Program using for-loop:-

In this program, first we have declare n the size of the element, to take the input from the user and initialized the integer variable sum to zero and also the floating point variable avg to zero. After initializing the variables, take the size of array from the user and store it in variable n. Then create the array of size n and then run the for loop from 0 to n and then take the element from the user and store in the array and then again inside another for loop calculate the sum of all the elements inside the array and finally after the loop will compute the average and then finally print the output on the screen.

After taking the size of array from the user theres a check to ensure that the user has entered an integer greater than or equal to zero. if the user has entered greater than zero than will continue the program to find the average and if the user has entered zero or negative value then will print a message to enter the valid number and the exit from the program.



#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    printf("Enter the size of the array: ");
    scanf("%d", &n);
    
    //checking if the number entered is valid or not
    if(n<=0) {
        printf("Please enter the valid number");
        exit(0);
    }
    
    int arr[n];
    int sum=0;
    float avg=0;
    
    //Taking the input in the array from the user
    printf("Enter the elements one by one in the array: \n"); 
    for (int i = 0; i < n; i++) {
        printf("Enter element: ");
        scanf("%d", &arr[i]);
    }
    
    //computing the sum 
    for (int i = 0; i < n; i++) {
        sum = sum + arr[i];
    }
    
    printf("The sum of all elements is: %d\n", sum);
    
    //computing the average
    avg = sum/n;
    
    printf("\nThe average is: %f", avg);
    
    return 0;
}

Output:-
Enter the size of the array: 0
Please enter the valid number

Output:-
Enter the size of the array: 5
Enter the elements one by one in the array: 
Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
Enter element: 5
The sum of all elements is: 15

The average is: 3.000000

Program using while loop:-

This program is doing the same thing as the above program that is finding the average of the elements in the array by using while loop instead of for loop.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    printf("Enter the size of the array: ");
    scanf("%d", &n);
    
    //checking if the number entered is valid or not
    if(n<=0) {
        printf("Please enter the valid number");
        exit(0);
    }
    
    int arr[n];
    int sum=0;
    float avg=0;
    int i = 0;
    
    //Taking the input in the array from the user
    printf("Enter the elements one by one in the array: \n"); 
    while (i < n) {
        printf("Enter element: ");
        scanf("%d", &arr[i]);
        i++;
    }
    
    i=0;
    //computing the sum 
    while ( i < n) {
        sum = sum + arr[i];
        i++;
    }
    
    printf("The sum of all elements is: %d\n", sum);
    
    //computing the average
    avg = sum/n;
    
    printf("\nThe average is: %f", avg);
    
    return 0;
}

Output:-
Enter the size of the array: 5
Enter the elements one by one in the array: 
Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
Enter element: 5
The sum of all elements is: 15

The average is: 3.000000

Post a Comment

0 Comments