Homework No.3

[Functions, Advanced Control Flow, Vectors]

Write a program that consists of at least four functions:
(1) read_vector function asks the user to input a vector of integers and returns this vector to the main function;
(2) write_vector function prints out the vector;
(3) a particular function, described below;
(4) the main function, which calls other three functions.

The number of your particular function is the value of the expression faculty_number%21.
Your homework must be written in according to the rules described in:
4.6 Processing a Sequence of Inputs
5.3 Function Comments
5.6 Side Effects
9.3 Vector Parameters and Return Values


0. Write a function that computes the alternating sum of all elements in a vector. For example, if alternating_sum is called with a vector containing 1 4 9 16 9 then it computes 1-4+9-16+9=1. [P9.2]

1. Write a function all_minimal that returns an index vector containing the indexes of all elements in a vector that are equal to the minimal element. For example, if the function is called with a vector containing 1 4 1 2 2 1 2 then it returns indexes 0 2 5.

2. Write a function ge_mean that returns an index vector containing the indexes of all elements in a vector that are greater than the mean value of all elements in the vector. For example, if the function is called with a vector containing 1 4 1 2 3 then it returns indexes 1 4 because the mean value is (1+4+1+2+3)/5=2.2.

3. Write a function count_zeros that counts the zeros in a vector. For example, if the function is called with a vector containing 1 0 1 2 0 then it returns 2.

4. Write a function that removes duplicates from a vector. For example, if remove_duplicates is called with a vector containing 1 4 9 16 9 7 4 9 11 then the vector is changed to 1 4 9 16 7 11 [P9.10].

5. Write a function that removes all zeros from a vector. For example, if remove_zeros is called with a vector containing 1 4 9 16 0 7 0 0 11 then the vector is changed to 1 4 9 16 7 11.

6. Write a function that removes all negative elements from a vector. For example, if remove_negative is called with a vector containing 1 4 9 16 -2 7 -3 -4 11 then the vector is changed to 1 4 9 16 7 11.

7. Write a function that removes all elements from a vector  with even values and  which have odd indexes. For example, if remove_evenv_oddi is called with a vector containing 1 4 9 16 2 7 3 4 11 then the vector is changed to 1 9 2 7 3 11.

8. Write a function that duplicates all elements with odd indexes. For example, if  duplicate_odd is called with a vector containing 1 4 9 16 9 7 then the vector is changed to 1 4 4 9 16 16 9 7 7.

9. Write a function count_all that counts the different values in a vector. For example, if the function is called with a vector containing 1 0 1 2 0 then it returns 3 because the vector elements have values 1, 0 and 2.

10. Write a function count_neg0pos that counts the negative, zero and positive elements in a vector. For example, if the function is called with a vector containing 1 0 1 -2 0 1 4 then it returns the vector 1 2 4.

11. Write a function that removes all negative elements from a vector. For example, if remove_negatives is called with a vector containing 1 -4 -9 -16 0 -7 0 0 11 then the vector is changed to 1 0 0 0 11.

12. Write a function that computes the sum of the elements in a vector with odd indexes. For example, if odd_index_sum is called with a vector containing 1 4 9 16 9 then it computes 4+16=20.

13. Write a function all_maximal that returns an index vector containing the indexes of all elements in a vector that are equal to the maximal element. For example, if the function is called with a vector containing 1 4 1 2 4 1 2 then it returns an index vector 1 4.

14. Write a function le_mean that returns an index vector containing the indexes of all elements in a vector that are lower than the mean value of all elements in the vector. For example, if the function is called with a vector containing 1 4 1 2 3 then it returns indexes 0 2 3 because the mean value is (1+4+1+2+3)/5=2.2.

15. Write a function count_nonzeros that counts the nonzeros in a vector. For example, if the function is called with a vector containing 1 0 -1 2 0 then it returns 3.

16. Write a function that removes non duplicates from a vector. For example, if remove_nonduplicates is called with a vector containing 1 4 9 16 9 7 4 9 11 then the vector is changed to 4 9 9 4 9.

17. Write a function that counts non duplicates from a vector. For example, if count_nonduplicates is called with a vector containing 1 4 9 16 9 7 4 9 11 then the function returns 4.

18. Write a function that counts duplicate values from a vector. For example, if count_duplicates is called with a vector containing 1 4 9 16 9 7 4 9 11 then the function returns 2 because only the values 4 and 9 duplicates.

19. Write a predicate function sorted that checks whether the values of elements in a vector are in increasing order, i.e. the vector is sorted. For example, if the function is called with a vector containing 1 4 9 16 then it returns true but if it called with 2 4 5 3 3 - the returns value is false.

20. Write a function index_between that returns an index vector containing the indexes of all elements in a vector that are in the closed interval, defined by the first and the last elements. For example, if the function is called with a vector containing 1 4 1 2 4 1 2 then it returns an index vector 0 2 3 5 6.