Type Systems

Using Type int

All the programs we have written so far have been operating on a fixed number of values given on standard input. Is it possible to programs that read in some arbitrary number of values instead? Here is one such problem.

Problem: Write a program that reads in one natural number from standard input – let us call it n. Upon that, it should read in additional n numbers from standard input. Finally, the program should output the sum of those n numbers.

This does not seem too difficult to do. First, the program should read in the number n:

    cout << "Type in the number n: ";
    int n;
    cin >> n;

It would be best if variable n is of type int, as given, since the problem does not make sense if n is not a natural number. Even more, the problem explicitly states that the input value n has to be a natural number.

We should now write a for loop that does n iterations to read in n numbers. The number of an iteration has to be a whole number, too. Accordingly, the loop variable, often named i, should also be an integer! Let us try that out:

    for(int i=1; i<=n; i++)
        {
        double inputValue;
        cin >> inputValue;
        sum += inputValue;
        }
    cout << "The sum is: " << sum << endl;

It is all obvious. But, what did we forget?

We forgot to introduce the sum variable and set it to 0! Here is the entire program:

#include <iostream>
using namespace std;

int main() 
{ 
    cout << "Type in the number n: ";
    int n;    
    cin >> n;    

    cout << "Type in " << n << " numbers:" << endl;
    double sum = 0;
    for(int i=1; i<=n; i++)
        {
        double inputValue;
        cin >> inputValue;
        sum += inputValue;
        }
    cout << "The sum is: " << sum << endl;
}

Be careful because the problem as stated obliges a user to type in a natural number for the value n. Should he fail to do so, the program will not work correctly. The cin statement will misinterpret the input data if the user does not provide a whole number as a first value.

If you run this program, this is what you might get on output:

Type in the number n: 4
Type in 4 numbers:
5.1  -3  0.55  8.88
The sum is: 11.53

Local Variables

There is something else that is new in the given program: it might seem somewhat weird to see the variable inputValue being introduced in the middle of a for loop. How does such a thing work?

In the C++ language, and in many others, variables are local to a statement block in which they are introduced. What does that mean? It means that those variables are valid only in the said statement block. When the execution of a given statement block is completed, all the variables introduced in it are removed.

When a statement block is repeated by a for loop, on every iteration the local variables introduced in that statement block first get created and then they get removed when an iteration is completed.

In our example, the variable inputValue gets created on every iteration at the beginning of the statement block, and it gets removed on every iteration at the end of the block's execution. The variable inputValue is a local variable of the statement block controlled by the for loop.