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
What does Fire mean online? | Later Social Media Glossary
What are Disadvantages and Advantages of Financial Statement Analysis?
Thor Majestic 23A Floor Plan
Main Moon Ilion Menu
Jailbase Orlando
Blanchard St Denis Funeral Home Obituaries
Www.metaquest/Device Code
Chris wragge hi-res stock photography and images - Alamy
Top Financial Advisors in the U.S.
Best Transmission Service Margate
The Best English Movie Theaters In Germany [Ultimate Guide]
Lowes 385
Heska Ulite
The Haunted Drury Hotels of San Antonio’s Riverwalk
Does Pappadeaux Pay Weekly
Olivia Ponton On Pride, Her Collection With AE & Accidentally Coming Out On TikTok
Mid90S Common Sense Media
5808 W 110Th St Overland Park Ks 66211 Directions
Morocco Forum Tripadvisor
Nioh 2: Divine Gear [Hands-on Experience]
Dump Trucks in Netherlands for sale - used and new - TrucksNL
Vcuapi
Current Time In Maryland
Illinois Gun Shows 2022
Rugged Gentleman Barber Shop Martinsburg Wv
Mccain Agportal
Webcentral Cuny
north jersey garage & moving sales - craigslist
Phoebus uses last-second touchdown to stun Salem for Class 4 football title
Walmart Near South Lake Tahoe Ca
Betaalbaar naar The Big Apple: 9 x tips voor New York City
Buying Cars from Craigslist: Tips for a Safe and Smart Purchase
Deshuesadero El Pulpo
fft - Fast Fourier transform
Gen 50 Kjv
How rich were the McCallisters in 'Home Alone'? Family's income unveiled
Dtlr On 87Th Cottage Grove
Hoofdletters voor God in de NBV21 - Bijbelblog
Moxfield Deck Builder
Grapes And Hops Festival Jamestown Ny
Planet Fitness Lebanon Nh
Streameast.xy2
Yogu Cheshire
9 oplossingen voor het laptoptouchpad dat niet werkt in Windows - TWCB (NL)
Bill Manser Net Worth
Joey Gentile Lpsg
Busted Newspaper Mcpherson Kansas
Jammiah Broomfield Ig
Canada Life Insurance Comparison Ivari Vs Sun Life
Sj Craigs
Pauline Frommer's Paris 2007 (Pauline Frommer Guides) - SILO.PUB
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.