Lab 2 Exercise 1
You cannot submit for this problem because the homework's deadline is due.
Ex.1 Play with Integers
Related Topics: Program Arguments.
Problem: You will use cin
to read an array of int
and do some data manipulation according to the program arguments. There are several options for the program:
- --help: print the message
Hey, I love Integers.
and exit, ignore all other options. - --add: add all the numbers in the array, record the result. No need to worry about the integer overflow.
- --small
<int>
: find the smallest element in the array, add this element to theint
passed through the argument, and print the result. - --verbose: verbose mode, see the output format for details.
Input Format:
There will be 2 lines, the first line only contains the number of int
in the array. The second line gives the whole array.
cpp
// sample input
5
1 2 3 4 5
Example 1 (contains --help
):
Program Argument: ./ex1 --help
or ./ex1 --verbose --help
Output Format: Hey, I love Integers.
Notice that when we have --help
in the argument, we will only print this line and return.
Example 2 (contains add
):
Program Argument: ./ex1 --add
Output Format: 15
because 1+2+3+4+5=15
.
Example 3 (contains verbose
):
Program Argument: ./ex1 --add --verbose
Output Format:
This is add operation.
15
Example 4 (contains verbose
):
Program Argument: ./ex1 --small 3 --verbose
Output Format:
This is small operation.
4
Note that if no data operation is mentioned in the argument, no matter whether the verbose mode is on or not, we simply output one line: No work to do!
. If both data operations are there, always first output the things related to the add operations. Please also be aware that the order of all the options will be random and you may assume that there are no unknown options in the argument and the argument must be valid.
Requirement: Create a file with name ex1.cpp
and write your function there.