10. Arrays

Minimum Value Algorithm with Arrays

Finally, we have all the tools that are needed to present the solution to the problem from the beginning of this chapter. Let us restate the problem: Write a program that reads in 5 numbers and prints out the smallest of them. The program should use arrays to store input data.

As the first step, the program should read in five numbers from standard input and store them in an array which we will name inputValues:

    vector<double> inputValues(5);
    for(int i=0; i<5; i++)
        cin >> inputValues[i];

The variable inputValues now represents all the values on the first paper in the minimum value algorithm.

A variable named minimum will be storing the currently smallest value. At the beginning of the algorithm the variable minimum has to be initialized, and it is quite convenient to do that by using the first input value. We will introduce and initialize the variable minimum in a single step:

    double minimum = inputValues[0];

The first input value has already been processed. The algorithm states that, in each iteration, the next input value should be selected. In the first iteration that is the second input value, in the second iteration the third input value, etc. The iterations themselves will be performed by virtue of a for loop, where the loop variable i iterates through all the elements of the array inputValues, except the first one.

Having processed the first input value, that is inputValues[0], the program has to process the remaining input values within a for loop. Those input values are: inputValues[1], inputValues[2], inputValues[3] and inputValues[4]. Therefore, the loop variable i has to range from 1 to 4. We can write that in the following manner:

    for(int i=1; i<5; i++)

In the body of the loop we have to write (i.e. to code) the part that examines the input values and updates the smallest value so far whenever it finds a yet smaller one:

    for(int i=1; i<5; i++)
        {
        if (inputValues[i] < minimum)
            minimum = inputValues[i];
        }

In each iteration, the program compares two numbers: the i-th input value (that is, inputValues[i]) to the smallest value found so far. If a yet smaller value has been found, it is stored in the minimum variable.

The entire program is presented here:

#include <iostream>
#include <vector>
using namespace std;
 
int main()
{
    vector<double> inputValues(5);
    cout << "Type in 5 numbers: " << endl;
    for(int i=0; i<5; i++)
        cin >> inputValues[i];

    double minimum = inputValues[0];
    for(int i=1; i<5; i++)
        {
        if (inputValues[i] < minimum)
            minimum = inputValues[i];
        }

    cout << "The smallest number is: " << minimum << endl;
}

What are the benefits of this program compared to the one without arrays? At this point, it may be hard to recognize the benefits. However, to successfully do the exercises you will need to use arrays and write programs similar to the one above.

Roll up your sleeves! Run this program to test whether it works.