C++ Program to Calculate Average of Numbers

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The average, or arithmetic mean, condenses vast data into a single value by dividing the sum of observations by their count. Comparing class performance, averages highlight overall achievement. By calculating class-wise marks averages, it becomes evident which class excels. The simplicity of averages makes them a versatile tool in daily life for diverse comparisons.

Program to Calculate Average of Numbers Using Arrays

Algorithm

Here in this program, we will learn to calculate the average of numbers using an array in C++.

Steps:

  • Take the total number of observations as input from the user.
  • Dynamically allocate memory to an array of size equal to the total number of observations entered by the user.
  • Take the value of all n observations as input from the user.
  • Calculate the sum of all the n observations using for loop.
  • Calculate the average using the formula average=sum/n.
  • Print your result.

Example:

Output:

Explanation

The integer array v is used to hold the numbers. We begin by asking the user how many total observations he has. This is saved in the variable n, and memory is allocated to the array dynamically. The numbers are then entered by the user and stored in the array using a for loop. Following that, we start a for loop and add the value of each element to the variable sum.

As a result, at the end of the loop, the sum variable contains the total sum of all observations the user has entered. After storing all the numbers, the average is computed using the formula sum/n, and the result is displayed.

Time Complexity: Since we are traversing all the array elements, the program's time complexity becomes O(n).

Program to Calculate Average of Numbers Using Functions

Here in the below program, we will calculate the average of input numbers with the help of our custom function. We will pass the array and size of the array as parameters to our custom function, which returns the average value of all the observations to the main function.

Example:

Output:

Explanation

We first ask the end-user to input the size of the array or the total number of observations in the program's main method. Then we allocate an array with that length. We utilize for loop to collect the input value for the array elements, and we take n elements from the end-user. The average() method receives this array and the size of the array.

The above program's average() method accepts an array of double values and the array's size. It computes the sum value by iterating through the array of items. Average=sum/numberAverage = sum/number;  is how the average value is determined. The average value is returned to the caller function once it has been calculated.

Time Complexity: Since we traverse array elements only once to calculate the sum of all our observations. Therefore time complexity of our program is O(n).

Program to Calculate Average of 3 Numbers

In this example, we will learn how to use the C++ programming language to compute the sum and average of three numbers.

This program prompts the user to enter three integers, after which it computes the total of the three numbers using the (+) arithmetic operator and the average using the formula: Sum/3Sum / 3.

Example:

Output:

Explanation

We have defined three integer data type variables and two floating data type variables in this program, named a, b, c, sum, and average, respectively. To get the sum and average, the user must provide three numbers. The (+) arithmetic operator is used to calculate the sum of two integers. Similarly, the formula for calculating the average is
Average=TotalSum/TotalNumberofTermsAverage = Total Sum / Total Number of Terms

The cout statement displays the sum and average of two values on the screen.

Time Complexity: Since we are directly doing an arithmetic sum of all the numbers using the '+' operator and then dividing it by 3 using the '/' operator. All of these operations are performed in constant time. Hence time complexity of our program is O(1).

Conclusion

  • Average=SumofAllObservations/NumberofObservationsAverage = Sum of All Observations / Number of Observations.
  • Average is a statistical measure representing a huge quantity of data in a single representation.
  • Average can be calculated with the help of arrays, custom functions, and by using the formula.