Program to Average of array elements in C Language

Spread the love
average-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.

average-array-elements-in-c-program-output

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.

average-array-elements-using-functions-in-c-program-output

  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

Venkat

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

You may also like...

Leave a Reply