Counter, Arithmetic Mean, Division by Zero and 'using' Directive
The counter algorithm belongs to a small group of simple, but often used algorithms, of which two have already been described in detail: the algorithm of summing up and the minimum value algorithm. Due to the simplicity of the mentioned algorithms, most programmers do not even notice those algorithms when using them. It goes without saying that they should never be glossed over when teaching computer programming. The objective of the counter algorithm is to compute how many numbers in a given sequence of values satisfy some particular condition. To begin, as usual, let us solve a problem.
Problem: Write a program that reads in 7 numbers. The program should write out how many of them are greater than 10.
To follow recommendations from the previous
chapter, the program should first perform the data input. It should store the
numbers read in from standard input in an array, which will again be called inputValues
:
vector<double> inputValues(7); cout << "Type in 7 numbers: " << endl; for (int i=0;i<7;i++) cin >> inputValues[i];
That completes the data input part of the program.
To perform the counting, the program should
do as follows: first, it should introduce a new variable, which we will name count
, and
assign a value 0 to it. Notice that, when counting something, the result has to
be an integer. For that reason, we will make the variable count
to be
of type int
.
Again, as when calculating a sum, we should be extra careful not to forget this fundamental step:
int count = 0;
All the numbers the program has to
process are stored in array inputValues
. To find out how many of them are greater than 10, the program has
to examine all of the elements one by one. Let us use a for
loop to
iterate through the elements of the inputValues
array:
for (int i=0;i<7;i++)
Like in the previous chapter, we will
be using the loop variable i
as an index into the array inputValues
. That is, at each iteration the program will process the array element inputValues[i]
. As the loop variable i
will range through all possible indices
of array inputValues
, the program will have thus processed all of the elements of that array.
What should the program do in the body of
this for
loop? Well, if in the process of iterating through the elements of
the array an element greater than the number 10 is encountered, then it should be
counted in, that is the counter should be incremented by 1:
if(inputValues[i] > 10) count++;
If you remember, the expression count++
increments the variable count
by 1, which is exactly the same as (that is, equivalent to) writing
count=count+1
, or count+=1
. This was mentioned in the chapter
Variables. The operator ++
is called
the increment operator.
It can also be written this way: ++count
.
The name C++ stems from the way of incrementing values by 1 in this language. Hence, "C++" would be something like "C incremented by 1", or the successor of C.
As you can see, there are as many as three different ways to increment the value of a variable by 1. The operator ++
was added to make writing this very common operation more succinct.
The entire code for the program should be as follows:
#include <vector> #include <iostream> using namespace std; int main() { vector<double> inputValues(7); cout << "Type in 7 numbers: " << endl; for (int i=0;i<7;i++) cin >> inputValues[i]; int count = 0; for (int i=0;i<7;i++) { if (inputValues[i] > 10) count++; } cout << "The count of values greater than 10 is: " << count << endl; }
This program is divided into three parts as suggested in the previous chapter.
This is how the execution of this program may look like:
Type in 7 numbers: 3 10 18 2.43 243 0 -100 The count of values greater than 10 is: 2