Categories: C++

Program To find Average Of Array

In this program, we will learn about taking integer input from the user and storing it in the array. Then we will find the sum of all array elements using for loop and finally calculate the average of N input numbers stored in an array.

Program code:

#include <iostream>
using namespace std;

void func_avg(int *arr, int size){
    float sum = 0,average;
    // Find sum of all array elements
    for(int i = 0; i < size; i++) {
        sum += arr[i];
    }
 
    average = (float)sum / size;
    cout << "Average = " << average;

}
int main() {
    int i, count, sum, inputArray[500];
    float average;
 
    cout << "Enter number of elements\n";
    cin >> count;
     
    cout << "Enter " << count << " elements\n";
    // Read "count" elements from user
    for(i = 0; i < count; i++) {
        cin >> inputArray[i];
    }
    func_avg(inputArray,count);
     
   
    return 0;
}

Output:

Enter number of elements
4
Enter 4 elements
1 2 3 4
Average = 2.5

Note: also read about Python Class

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

5 days ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

5 days ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

6 days ago

Largest Sum of Non-Adjacent Numbers

Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…

1 week ago

Count Unival Subtrees in a Binary Tree

A unival tree (short for "universal value tree") is a tree in which all nodes…

1 week ago

Count Decoding Ways for Encoded Messages

Problem Statement (Asked By Facebook) Given the mapping a = 1, b = 2, ...,…

1 week ago