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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

1 week ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

3 weeks ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

6 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

6 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

7 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

7 months ago