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.
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 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 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.
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:
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:
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.
The last line of the program outputs the result:
std::cout<<"The mean is: "<< (sum / 5) <<std::endl;
This line has the following structure:
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.