PRINT: prints a header that reads "Vector Contents" and then
prints the index and sum of each vector entry one per
line.
- The index should be printed in a field 3 spaces wide and the
sum should be printed in a field 6 spaces wide with 2 digits of
precision. There should be a : after the index and then a space
between the : and the sum. For example "0: 4.85".
- If the vector entry has no sum (i.e., is 0) then print
the index and the string "Empty" instead. For example
"1: Empty". You are guaranteed that all numbers will be
positive and hence your sums will be positive and thus 0
serves as an indicator that no sum has been added to the
vector.
Here is some possible input:
ADD 3 4.85 PRINT ADD
0 6.21 ADD 3 3.15 ADD 6
7 ELEPHANT PRINT
and here is how your program would process the input:
- The "add 3 4.85" command will cause your program to resize the vector
to have 4 elements, all initialized to 0. Then it will assign 4.85 to
index 3.
- The "print" command will cause your program to print the contents
of the vector
- The "add 0 6.21" command will add 6.21 to index 0.
- The "add 3 3.15" command will add 3.15 to index 3, giving you
a sum of 8 for index 3.
- The "add 6 7" command will cause your program to resize the vector
to have 7 elements, with entries 4-6 initialized to 0, and then
will add 7 to index 6.
- The elephant command will be ignored
- The print command will print the contents of the vector
Note that you cannot assume that there is one command per line or that
a line terminates a command. You will need to use stream-oriented input as
opposed to line-oriented input for this problem.
Given the above input, your program should print:
Vector Contents
0: Empty
1: Empty
2: Empty
3: 4.85
Vector Contents
0: 6.21
1: Empty
2: Empty
3: 8.00
4: Empty
5: Empty
6: 7.00
Constraints:
- The only error checking you need to do is for the number of
command line arguments.
If the number of command line arguments is incorrect, you should
throw a string with the error message:
usage: ./sum filename
and then
catch it at the end of your main and print out the string.
- You must use printf to print your output
- You do not have to worry about writing the include statements