# Program to Average of array elements in C Language

## Program Description:

Write a program to calculate the Average of array elements in C programming language. The program should accept user input and update array elements and calculate the average of the array.

### Example Input and Output:

Input:

Output:

Average of all Array elements is : 45

### Prerequisites:

We are going to use the arrays and functions in this program. Please go through the following articles to learn more about C Arrays and Functions.

## Average of array elements in C Program Explanation:

1. Create a Macro SIZE with value 5. #define SIZE 5
2. In the main() function, Declare an array arr with the SIZE. Also, create variables i, sum, and avg. Initialize sum and avg with zero.
3. Take the user input to update the array. To do that, Use a for loop to iterate over the array ( from i = zero(0) to SIZE-1 or i<SIZE ).
• At each iteration, Prompt the user for input and read the value using scanf function and store it in arr[i]
4. The average of the array is equal to the Sum of all array elements divides by the number of elements in the array. ( avg = sum_of_elements / num_of_elements)
5. First, calculate the sum of the array elements. To do that, Use a for loop to traverse the array at each iteration and add the sum to arr[i]. â€“ sum += arr[i];
6. Once we got the sum of all elements of the array( sum), Divide the sum with SIZE of the array to get the average of all array elements â€“ avg = sum / SIZE
7. Print the result on the standard output i.e console.

## Algorithm of Average of array elements in C:

1. Declare an integer arrayÂ  arrÂ of sizeÂ  5Â and an integer variableÂ i. Initialize the sum and avg with zero(0)
2. Prompt the user to input the values for the arr
3. FORÂ  i&nbsp;=&nbsp;0&nbsp;toÂ  SIZE-1Â ( Loop )
• Read the value forÂ Â  arr[i]
4. FORÂ  i&nbsp;=&nbsp;0Â toÂ  SIZE-1Â ( Loop )
• sum&nbsp;=&nbsp;sum&nbsp;+&nbsp;arr[i];
5. Calculate Average
• avg = sum / SIZE;
6. Print the result on the console

## Average of array elements in C Program:

Letâ€™s convert the above algorithm into the C code.

### Program Output:

Compile the program. We are using the GCC compiler in Linux OS.

gcc avg-arr-elements.c

The above command generates the executable file named a.out. Run the executable.

As we can see from the above program, We are getting the expected results.

## Program to calculate the Average of array elements using the function in C:

Letâ€™s rewrite the above program to use the functions for Reading the numbers from user and Calculating the Average of elements.

The following program uses the read function to take user input and the average function to calculate the average of the array elements.

ðŸ“¢Note, As we already have the SIZE macro, We can use the SIZE macro directly instead of passing the size as a parameter to both functions.

## Average of array elements using the function in C Program Explanation:

1. CreateÂ  arrarray with the sizeÂ  5Â and also create aÂ  avgvariable and initialize withÂ  zero(0).
2. DefineÂ the read()Â function to take the input numbers from the user. TheÂ readÂ function takesÂ  *arr andÂ  sizeÂ as formal arguments
• The readÂ function uses aÂ forÂ loopÂ to prompt the user for input and update the array elements.
3. Create another functionÂ  average to calculate the average of all array elements.
• TheÂ  averagefunctions take integer arrayÂ  arr[]Â and array sizeÂ as the formal arguments and return an integer.
• This function iterates over theÂ  arrÂ and at each iteration adds theÂ  sumÂ andÂ  arr[i]Â â€“Â  sum&nbsp;=&nbsp;sum&nbsp;+&nbsp;arr[i];
• Then calculate the average by dividing the sum by size. Return it. ( return sum / size;)
4. In the main() function, Call theÂ  read()Â function to take user input.
5. Call the averageÂ function to calculate the average of array elements. This function returns theÂ average of elements. store it inÂ  avgÂ variable.
6. Print theÂ  avgÂ variable on the console.

### Program Output:

Letâ€™s compile and run the program.

1. C Program to Perform Arithmetic Operations using functions
2. Add Two Numbers using Functions in C
3. C Program to calculate Fibonacci Series using function
4. C Program to find Factorial of a Number using functions
5. C Program to find Minimum and Maximum numbers using Functions
6. Armstrong Number using Function in C language
7. Prime number program using Function
8. Program to Caclulate Area of Circle using Function
9. Program to Check Even or Odd number using Function
10. Binary to Decimal Conversion using function in C

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.