Find Average in C++

Find Average in C++. Write a program to find the average of given numbers in the C++ language. Use an array to store the entered numbers, find the sum, calculate the average value, and display the result.

In this problem, we will use the array concept. The array is used to store the multiple values of the same time using a single variable name. The elements of an array are accessed through the index, i.e. array[0] will give the first element of the array, array[1] will give the second element of the array, and so on. To access each element we use loops like while loop, do-while loop, for loop, and e.t.c.

C++ Program to Find Average

#include<iostream>
using namespace std;
// main function
int main()
{
  int size; // size of array
  double sum; // to store sum
  double avg; // to store average

  // ask size of array
  cout << "How many numbers you want to enter: ";
  cin >> size;
  
  // declare an array of double data type
  // with the given size
  double arr[size];

  // take input for array
  cout << "Enter " << size << " numbers: ";
  for (int i = 0; i < size; ++i)
  {
    cin >> arr[i];
  }

  // calculate sum value
  sum = 0; // initialize sum with 0
  for (int i = 0; i < size; ++i)
  {
    sum += arr[i];
  }

  // calculate average value
  avg = sum/size;
  
  // display result
  cout << "Sum = " << sum << endl;
  cout << "Average = " << avg << endl;

  return 0;
}

Output:-

How many numbers you want to enter: 5
Enter 5 numbers: 10 20 30 40 50
Sum = 150
Average = 30

How many numbers you want to enter: 3
Enter 3 numbers: 15.6 45.9 63.8
Sum = 125.3
Average = 41.7667

In this program, we asked to enter the size of the array (i.e. total number of elements) then we created an array of that size. This array is of double data type so that we can store both integer and floating-point numbers. Now let us develop the program to find the average in C++ with the help of function.

Find Average of Array using Function

Program to find average in C++ for the array of numbers using function.

#include<iostream>
using namespace std;
// function to calculate the average of array
double average(double arr[], int size) 
{
  // variables
  double sum;
  double avg;

  // calculate sum value
  sum = 0; // initialize sum with 0
  for (int i = 0; i < size; ++i)
  {
    sum += arr[i];
  }

  // calculate average value
  avg = sum/size;

  // return result
  return avg;
}

// main function
int main()
{
  int size; // size of array
  double avg; // to store average

  // ask size of array
  cout << "Enter size: ";
  cin >> size;
  
  // declare an array of double data type
  // with the given size
  double arr[size];

  // take input for array
  cout << "Enter " << size << " numbers: ";
  for (int i = 0; i < size; ++i)
  {
    cin >> arr[i];
  }

  // call function
  avg = average(arr, size);
  
  // display result
  cout << "Average = " << avg << endl;

  return 0;
}

Output:-

Enter size: 5
Enter 5 numbers: 51 20 32 45 65
Average = 42.6

Enter size: 3
Enter 3 numbers: 454.584 845.45 656.45
Average = 652.161

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *