C Program to Calculate Average of an Array (original) (raw)
Last Updated : 21 Nov, 2024
In this article, we will learn how to calculate the average of all elements of an array using a C program.
The simplest method to calculate the average of all elements of an array is by using a **loop. Let's take a look at an example:
C `
#include <stdio.h>
float getAvg(int arr[], int n) { int sum = 0;
// Find the sum of all elements
for (int i = 0; i < n; i++) {
sum += arr[i];
}
// Return the average
return (float)sum / n;
}
int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]);
// Calculate the average of array arr
float res = getAvg(arr, n);
printf("%.2f\n", res);
return 0;
}
`
**Explanation: This method iterates through the entire array to find the sum of all elements and then divides the sum by the total number of elements to calculate the average.
This approach can also be implemented using recursion as shown below.
Using Recursion
This method computes the average of the first n-1 elements using a recursive call for each element of the array. Then it combines it with the average of last element to find the overall average.
C `
#include <stdio.h>
float getAvg(int arr[], int n) {
// If only one element left, return it
if (n == 1) {
return arr[0];
}
// Add the current element to the average of
// the remaining array
return (arr[n - 1] + (n - 1) *
getAvg(arr, n - 1)) / n;
}
int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]);
// Calculate the average
float res = getAvg(arr, n);
printf("%.2f", res);
return 0;
}
`
Similar Reads
- C Program to Copy an Array to Another Array In this article, we will learn how to copy all the elements of one array to another array in C.The simplest method to copy an array is by using the memcpy() function. Let's take a look at an example:C#include <stdio.h> #include <string.h> int main() { int arr1[] = {1, 2, 3, 4, 5}; int n 3 min read
- C program to count frequency of each element in an array Given an array arr[] of size N, the task is to find the frequency of each distinct element present in the given array. Examples: Input: arr[] = { 1, 100000000, 3, 100000000, 3 } Output: { 1 : 1, 3 : 2, 100000000 : 2 } Explanation: Distinct elements of the given array are { 1, 100000000, 3 } Frequenc 4 min read
- C Program for Equilibrium index of an array Write a function int equilibrium(int[] arr, int n); that given a sequence arr[] of size n, returns an equilibrium index (if any) or -1 if no equilibrium indexes exist. The equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at high 4 min read
- C Program to Traverse an Array Write a C program to traverse the given array that contains N number of elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3} Output: 2 -1 5 6 0 -3Input: arr[] = {4, 0, -2, -9, -7, 1} Output: 4 0 -2 -9 -7 1Different Ways to Traverse an Array in CArrays are versatile data structures and C language pro 3 min read
- Array C/C++ Programs C Program to find sum of elements in a given arrayC program to find largest element in an arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum as xC/C++ Program for Majority ElementC/C++ Program for Find the Number Occurring Odd N 6 min read
- How to Create an Array of Structs in C? In C, a structure is a user-defined data type that can be used to group items of different types into a single entity while an array is a collection of similar data elements. In this article, we will learn how to create an array of structs in C. Creating an Array of Structs in CTo create an array of 2 min read
- size of char datatype and char array in C Given a char variable and a char array, the task is to write a program to find the size of this char variable and char array in C. Examples: Input: ch = 'G', arr[] = {'G', 'F', 'G'} Output: Size of char datatype is: 1 byte Size of char array is: 3 byte Input: ch = 'G', arr[] = {'G', 'F'} Output: Siz 2 min read
- C Program to Find the Maximum and Minimum Element in the Array In this article, we will discuss different ways to find the maximum and minimum elements of the array in C.The simplest method to find the maximum and minimum element of the array is iterates through the array and compare each element with the assumed minimum and maximum and update them if the curre 3 min read
- C Program for Counting Sort Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. Algorithm: Step 1: StartStep 2 : Find Larg 3 min read
- How to Iterate Through Array of Structs in C? In C, while working with an array of structs we have to iterate through each struct in an array to perform certain operations or access its members. In this article, we will learn how to iterate through an array of the structs in C. Iterate Over of an Array of StructuresTo iterate through an array o 2 min read