Bojan Nikolic: Numerical and Quantitative Methods in C++

[website home] | [BN Algorithms]

Computing the Mean: Simplest Version

Here is a simple program which computes the arithmetic mean of a set of numbers:

// mean_v1.cpp
// Bojan Nikolic <bojan@bnikolic.co.uk>

#include <iostream>

int main(void)
{
  const double data[] = { 1,2,3,4,5};

  double sum =0;
  for (size_t i =0 ; i < 5 ; ++i )
  {
    sum += data[i];
  }

  std::cout<<"The mean is: "<< (sum / 5) <<std::endl;
}

If you compile and run this program it will produce the output:

bnikolic@bnikolic-laptop:~/c/bnweb/prog/cpp-mean$ ./mean_v1
The mean is: 3

There is nothing terribly wrong with this program, but in this series articles I will illustrate a number of programming concepts, and many specifically C++ concepts, by incrementally improving on the above example.

Firstly, however, I will go desribe the structure of this, the simplest, version of the program.

Deconstructing mean_v1

Comments

The program begins with two lines starting with string of characters //:

// mean_v1.cpp
// Bojan Nikolic <bojan@bnikolic.co.uk>

In C++, the string // denotes a comment, and the compiler simply ignores the rest of any line once it finds these characters. These first two lines are therefore completely ignored by the compiler.

There is no magic about there being two lines – any number of lines starting with // would have been ignored, and if there were no lines at all, the program would continue to work just as well.

The #include directive

The next non-empty line of the program is:

#include <iostream>

In practical terms, the effect of this line is to find the file iostream on your system and include its entire contents at this point of your program.

Conceptually the effect of this line is simpler: it lets your program know what the library <iostream> can do for you. It does not import the entire content of the library (that is the job of the linker program) but rather just enough to let the compiler know what it can do.

The library <iostream> is a part of the C++ standard library, and must be included with every standard conforming C++ compiler. It allows input and output of data from the program: the io stands for input/output.

The main function

The next section of the program defines a function called main:

int main(void)
{
....
}

The function returns an integer value, as specified by the keyword int before the function name and takes no parameters, as specified by the void in the parenthesis after the function name.

The function called main has a special meaning in C++ (as well as C): this is where the execution of the progam will start. This special meaning is not encoded in the C++ compiler, it compiles the function main in exactly the same way as any other function. However, the set of routines which help C++ program run (the run-time environment) are arranged so that they start your program by calling the function main.

Unless you are writing a library, your program must contain a main function.

Variable declaration: array of doubles

The next line of the program declares a variable which is an array of constant double-precision floating point numbers:

const double data[] = { 1,2,3,4,5};

Here is what the components of this line mean:

  • const tells the compiler that members of the array should not change throughout the program. The compiler will, when possible, enforce this by not allowing you to call functions or expressions which assign to these elements.
  • double tells the compiler that the members of the array are double-precision floating point numbers.
  • data is the name of the variable.
  • [] tells the compiler that this variable is an array. If there were no brackets, i.e., just const double data then the compiler would assume data is a single floating point value.
  • = { 1,2,3,4,5} initialises the values of the array to those specified in the braces. This is necessary! Since we declared the members const, unless we set them at the time of the initialisation of the variable, we could never set them.

Summing the array (for loop)

The next section of the program computes the sum of the elements of the array data. The first line of this section:

double sum =0;

declares and initialises the double-precision variable sum. We will accumulate the running sum of the elements in this variable.

The next three lines of this section form a for loop expression:

for (size_t i =0 ; i < 5 ; ++i )
{
  .....
}

In this context this expression executes the contents of the loop five times, incrementing with each iteration the value of the variable i. This logic is expressed in the three expressions in the parenthesis after the keyword for. The expressions are separated by semi-colons (;) and have the following meaning:

  • First expression allows declaration and initialisation of a variable local to the loop. This expression is executed only once, i.e., it is not repeated along with the rest of the loop. In this example we have: size_t i =0. The type size_t is an integer type suitable for iteration over an array of elements.
  • The second expression defines the exit condition of the loop: it is executed at the beginning of every iteration of the loop, and if it returns a true value then the loop continues; if it returns a false value, the loop will stop and proceed with the first statement after the closing brace }.
  • The third expression is executed at the end of each iteration of the loop. Typically it is used to adjust the value of the loop variable; for example in this case it increases the value of the variable i by one. (The exact meaning of the expression ++i is: increase the value of i by one and then return this incremented value).

Inside the loop there is only one statement:

sum += data[i];

The expression data[i] returns the i-th element of the array data (the counting starts from zero). The operator += means: increase the value of the variable on the left of the operator by the value of the expression on the right of the operator. Therefore, this statement as a whole increments the value of the variable sum by the value of the i-th element of the array data.

Output

The last line of the program outputs the result:

std::cout<<"The mean is: "<< (sum / 5) <<std::endl;

This line has the following structure:

  • std::cout is the “standard output”, which normally just prints the text to the console of the application. This variable is declared by the iostream header and defined in the run-time environment – this program just uses it. If you did not include the iostream in the program, the compiler would produce an error message about an unknown variable.
  • << is an operator that has several meanings. In this context however, it simply means print item on the right to the output on the left. Several of these can be strung together as illustrated above.
  • (sum / 5) divides the value of sum by 5.0
  • <<std::endl prints the new-line character the output, so that the text stands out.

That is it, with the result output the program runs to the end of the main function and the run-time returns the control to the calling program.

Summary of mean_v1

The above program illustrates a number of basic C++ programming constructs. It is very basic in its aims and could be designed better in some respects, but it is correct and will run. In the following articles, I will look how to improve and not improve this simple program.