(E) Number series in a vector¶
Goal: I will learn the basic idea of STL containers by using vector in implementing small operations.
Instructions:
Retrieve the code template: templates/04/container/
->
student/04/container/
.
Attention
Recall from section ”4.1 Git and remote repositories” how to act to obtain new, later published code templates in use.
The code template comprises main function that uses an integer vector. Implement the functions that are called in main function.
The function read_integers
has been implemented partly.
Go on and finish the implementation.
This function lets the user give as many numbers as stated by the latter
parameter, and it stores the numbers to the vector given as the first
parameter.
(OBS! Consider also why the author of the assignment has chosen to
pass the vector as a parameter instead of a return value.)
The function same_values
returns the truth value telling if all the
numbers of the vector are the same.
The function is_ordered_non_strict_ascending
returns the truth value
telling if the numbers of the vector are in a non-strict ascending order
(identical values are allowed).
The function is_arithmetic_series
returns the truth value telling if
the numbers of the vector form an arithmetic series.
(A series is arithmetic, if the difference between the consecutive
terms is constant.)
The function is_geometric_series
returns the truth value telling if
the numbers of the vector form a geometric series.
(A series is geometric, if the ratio between the consecutive
terms is constant.
In the case of integers, this ratio is a rational number, and thus,
the series consisting only of zeros is not considered as geometric.)
An example on program execution:
How many integers are there? 4
Enter the integers: 1 2 4 8
All the integers are not the same
The integers are in a non-strict ascending order
The integers do not form an arithmetic series
The integers form a geometric series
Tips for completing the assignment:
- The partially implemented function
read_integers
demonstrates the simplest way to read several integers in C++. However, note that when input operator>>
omits all spaces in the input, it also omits line breaks. You can test the program also by giving each integer in the line of its own.
A+ presents the exercise submission form here.