X
wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 14 people, some anonymous, worked to edit and improve it over time.
This article has been viewed 35,733 times.
Learn more...
If you have a big set of numbers you need to calculate the mean for, it can become a tedious task. In this case, a program can calculate mean easily. Fire off your favorite C++ coding environment and get started with step one below to learn how you can write a C++ program to calculate mean.
Steps
Part 1
Part 1 of 2:
Setup and Planning
-
1Plan your program. The first step to make this program is to plan how the program will work. If the numbers that will be calculated are long, floating-points then the double data type can be used to store numbers. If however, they are large integers, it is more appropriate to use long longs.
-
2Setup a basic skeleton program. In this step, include the iostream header file. Write out the main function: this will be where most of your code will be written.
#include <iostream> using namespace std; int main() { return 0; }
- The first line of code will start with include<iostream>. The iostream header file will add functions for input/output operations.
- You may optionally choose to include the using namespace std; directive as well as the return 0; statement in the main function.
Advertisement -
3Make an outline of the basic flow of the program. Use comments to take notes on what needs to be done. This will make it easier for you to fill in your code as you progress along. In larger projects, you might forget what your overall goal is. Comments help out here.
#include <iostream> using namespace std; int main() { // TODO read number of values // TODO read data and accumulate the sum // TODO take the average of the sum to determine the mean // TODO print output return 0; }
Advertisement
Part 2
Part 2 of 2:
Writing the Code
-
1Declare and read an int variable (n) to store the number of values in the data set. Use cin to read input.
... // read number of values int n; cout << "Enter the number of values in the data set:\n"; cout << ": "; cin >> n; cin.ignore(); // TODO read data and accumulate the sum ...
- You can output string literals to prompt the user using cout.
- On some systems, you may need to add the cin.ignore(); statement to tell the buffer to ignore the newline or return-carriage from the Enter-key.
-
2Use a loop to iterate from 0 to n, reading data and accumulating the sum. We first need to declare a variable to store the sum and initialize it to 0.0. We then use a for-loop, setting a temporary variable i to iterate from 0 to n and using x to read in temporary values. These temporary values are then added to the sum.
... // read data and accumulate the sum double sum = 0.0; for (int i = 0; i < n; i++) { double x; cout << "Enter the value #" << i+1 << ":\n"; cout << ": "; cin >> x; cin.ignore(); sum += x; } // TODO take the average of the sum to determine the mean ...
- Again, you may prompt the user for input using cout.
-
3Determine the mean by dividing by the number of values in the data set.
... // take the average of the sum to determine the mean double mean = sum / n; // TODO print output ...
- Note that if you declared sum to integer data types, integer division will be performed and there may be a loss of accuracy. To work around this, cast sum into a float or double first before dividing.
-
4Print the output to the user, showing the result. Use the cout stream to show your final results to the user.
... // print output cout << "Average/Mean = " << mean << '\n'; return 0; ...
-
5Review, comment, and clean your code.
#include <iostream> using namespace std; int main() { // read number of values int n; cout << "Enter the number of values in the data set:\n"; cout << ": "; cin >> n; cin.ignore(); // read data and accumulate the sum double sum = 0; for (int i = 0; i < n; i++) { double x; cout << "Enter the value #" << i+1 << ":\n"; cout << ": "; cin >> x; cin.ignore(); sum += x; } // take the average of the sum to determine the mean double mean = double(sum) / n; // print output cout << "Average/Mean = " << mean << '\n'; return 0; }
Advertisement
Warning
- Make sure signs in for loop are in correct direction or the program might fall into an infinite loop.
- Make sure your code is syntactically correct. Look out for any missing semicolons and brackets. IDEs will generally warn you about these common syntax errors.
About This Article
Advertisement