Program to Calculate Average Using Arrays - (C Programs)
Example:
#include <stdio.h>
int main()
{
int i, n;
float sum = 0, arr[10], avg;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
{
scanf("%f",&arr[i]);
//adding elements to sum
sum = sum + arr[i];
}
//calculating average
avg = sum / n;
//displaying result
printf("Average: %f", avg);
return 0;
}
Output:
Enter the number of elements: 5
Enter 5 elements:
10.0
20.0
30.0
40.0
50.0
Average: 30.000000
Explanation:
This program calculates the average of a given set of numbers using arrays. The user is prompted to enter the number of elements to be added to the array followed by the values of these elements. The sum of all the elements is calculated and then this sum is used to calculate the average by dividing it by the number of elements. Finally, the average is displayed to the user.
Use:
This program can be used in any scenario where the average of a set of numbers needs to be calculated. For example, it can be used in statistical analysis, financial calculations, or even in general-purpose programming.
Summary:
This program calculates the average of a given set of numbers using arrays. The user enters the number of elements and the actual elements. The program then calculates the sum of these elements and divides it by the total number of elements to get the average. Finally, the average is displayed to the user.