In this tutorial you will learn about the C Program to Calculate Average Using Arrays and its application with practical example.
C Program to Find Average an Array
In this tutorial, we will learn to create a C program that will average the elements of Array using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C Programming.
Average of the array:-
As we all know array is a collection of similar data type elements. In an array, only one variable is declared which can store multiple values. First will take the number of elements of an array from the user. Then will take the elements from the user for the array. And at last, will total the values and divide them from the number of elements of the array to find the average using C Programming Language.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
STEP 1: START STEP 2: INITIALIZE arr[] = {25, 11, 7, 75, 56} STEP 3: length= sizeof(arr)/sizeof(arr[0]) STEP 4: min = arr[0] STEP 5: SET i=0. PRINT ARRAY i<lenght NORMAL ORDER STEP 6: SET i=lenght -1 THEN PRINT STEP 7: i=i-1. STEP 8: PRINT "REVERSE ORDER OF ARRAY IS AS FOLLOWS" STEP 9: RETURN 0. STEP 10: END. |
Program:-
To find the Average value element from the array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include<stdio.h> int main() { //fill the code; int n; //Input form user number of elements of array printf("Enter the number of elements of array :-\n"); scanf("%d",&n); int arr[n]; int i, avg=0, sum=0; //Input form user elements of array printf("Enter the of elements of array :-\n"); for(i = 0; i < n; i++) { scanf("%d",&arr[i]); } //Sum of array elements for(i = 0; i < n; i++) { sum = sum+arr[i]; } //Average of array elements avg = sum/n; //Output Average of elements of array printf("Average of array is %d\n", avg); return 0; } |
Output:-
In the above program we have first initialized the required variable
- arr = it will hold the elements in an array.
- n = it will hold the number of elements in an array.
- i = it will hold the integer value to control the array.
- sum= it will hold the sum of the array.
- avg= it will hold the average of the array.
Taking input from the user in an array number of elements in the array.
Taking input from the user in array elements in the array.
Sum of the array.
Average of Array.