Operations on Arrays
We have already explained how to find a minimum value among the numbers in a list of a given or known length. In this chapter we will show how to read in a list of numbers which has a flexible or unknown length.
In that case, there must be a way to figure out where the input list ends (i.e. where it terminates). In other words, when the program is reading in numbers, the question is when should it stop doing that. How will the program discern the moment when we have completed typing in the input values? Obviously, there has to be something to mark the end of the input. So, let us just agree that we will use the number zero to mark the end of the list, while the zero itself will not be considered a part of the list. Now, we can formulate the problem.
Problem: Write a program that reads in a sequence of numbers until it reads in a value zero. The program needs to write out the smallest value in the input sequence, with the exception of the last element equal to zero.
When starting to write a program, we usually consider some of the necessary variables first. This program might use an array of numbers, since it will have to process a sequence containing many numbers. In our previous programs, we would know in advance the number of elements that the array would have. This time, that is not the case.
What shall we do now? The answer is: we will
introduce an array named data
that does not have any elements. That is accomplished by the
following statement:
We will not be utilizing the using directive in this chapter.
Instead, all the names from the namespace std
will be fully qualified.
std::vector<double> data;
But, what is the use of an array without elements?
The answer is in the fact that it is easy to add (more precisely, to append) elements into an array, for example:
std::vector<double> test; test.push_back(3);
The second command adds a new element of
value three into the array. The array test
now has one element: test[0]
.
Let us add another element:
test.push_back(7);
Now, the array test
has two elements: test[0]
which
equals 3 and test[1]
which equals 7. A new element is always appended at the end of an
array, meaning that a new element will become the last element of an array.
We can also remove the last element from an array using this statement:
test.pop_back();
Following this statement, the array will be
reduced to one element again, and that remaining element is test[0]
that
equals 3. If we were to remove another element by using
test.pop_back();
then the array test
would become empty once again.
It is time to get back to the problem of
reading the unknown quantity of numbers to the point when the number zero is
input. This looks like a good case for using a do
while
loop. So, let us use a do
while
loop to
read in numbers until the number zero is encountered:
double input; do { std::cin >> input; } while(input!=0);
This will do just fine, except that we
should also add the read numbers into the array data
:
double input; do { std::cin >> input; data.push_back(input); } while(input!=0);
Upon completion of this loop, the
array data
will also contain the terminating zero. Since this zero is not part
of the data, we need to remove it from the end of the array:
data.pop_back();