C++ Program to Calculate Average of Numbers | Scaler Topics (2024)

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 theformula 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 totalnumber of observationsin 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/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 programprompts 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 / 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 TermsAverage=TotalSum/TotalNumberofTerms

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 ObservationsAverage=SumofAllObservations/NumberofObservations.
  • 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.
C++ Program to Calculate Average of Numbers | Scaler Topics (2024)
Top Articles
Network Monitoring Alerts: 7 Best Practices for Network Alert Management
Jamaica Safety 2024: Is Jamaica Safe to Visit? 
Antisis City/Antisis City Gym
Riverrun Rv Park Middletown Photos
Hannaford Weekly Flyer Manchester Nh
Corpse Bride Soap2Day
Crime Scene Photos West Memphis Three
Truist Drive Through Hours
Tamilblasters 2023
Jscc Jweb
Zendaya Boob Job
Whitley County Ky Mugshots Busted
454 Cu In Liters
Ostateillustrated Com Message Boards
Q Management Inc
Walmart stores in 6 states no longer provide single-use bags at checkout: Which states are next?
Webcentral Cuny
BMW K1600GT (2017-on) Review | Speed, Specs & Prices
Orange Pill 44 291
Barber Gym Quantico Hours
Somewhere In Queens Showtimes Near The Maple Theater
How to Grow and Care for Four O'Clock Plants
Www.patientnotebook/Atic
Valic Eremit
D2L Brightspace Clc
Mineral Wells Skyward
Kabob-House-Spokane Photos
Tuw Academic Calendar
Cornedbeefapproved
Garden Grove Classlink
NV Energy issues outage watch for South Carson City, Genoa and Glenbrook
Superhot Free Online Game Unblocked
Puffin Asmr Leak
Evil Dead Rise - Everything You Need To Know
How to Draw a Bubble Letter M in 5 Easy Steps
PA lawmakers push to restore Medicaid dental benefits for adults
Nancy Pazelt Obituary
2700 Yen To Usd
The Conners Season 5 Wiki
Trivago Anaheim California
Windshield Repair & Auto Glass Replacement in Texas| Safelite
Coffee County Tag Office Douglas Ga
Comanche Or Crow Crossword Clue
Craigslist Houses For Rent Little River Sc
Cch Staffnet
Vci Classified Paducah
Ratchet And Clank Tools Of Destruction Rpcs3 Freeze
Ajpw Sugar Glider Worth
Amourdelavie
Nkey rollover - Hitta bästa priset på Prisjakt
Tweedehands camper te koop - camper occasion kopen
Access One Ummc
Latest Posts
Article information

Author: Fr. Dewey Fisher

Last Updated:

Views: 6128

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Fr. Dewey Fisher

Birthday: 1993-03-26

Address: 917 Hyun Views, Rogahnmouth, KY 91013-8827

Phone: +5938540192553

Job: Administration Developer

Hobby: Embroidery, Horseback riding, Juggling, Urban exploration, Skiing, Cycling, Handball

Introduction: My name is Fr. Dewey Fisher, I am a powerful, open, faithful, combative, spotless, faithful, fair person who loves writing and wants to share my knowledge and understanding with you.