(E) Number series in a vector

Goal: I will learn the basic idea of STL containers by using vector in implementing small operations. In addition, I will practice loop structures of C++ (for loop).

Instructions: Retrieve the code template: templates/04/container/ -> student/04/container/.

Reminder

Recall from the end of previous round (or from the menu on the left ”Fetching new templates”) 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. You must not modify the main function.

The function print_integers has been implemented completely.

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.)

The function triple_integers triples the numbers in the vector.

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
Tripled values: 3 6 12 24

Tips for completing the assignment:

  • Consider which kind of for loop suits best in each function.
  • 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.