- Given the following fragment of C++ code.
int prod, i;
prod = 1;
i = 2;
while (i <= 10)
prod *= i;
i++;
cout << prod;
- What would be the output of the C++ fragment?
(Run your program and elaborate on the results obtained).
- If the purpose of this fragment
of C++ was to output the value of 1*2*3*4*5*6*7*8*9.
How should it be changed? Once you have made the necessary changes,
run your program.
- Now if the purpose of this fragment
of C++ was to output the value of 2*4*6*8*10*12*14*16*18*20;
How should the above program be changed?
Once you have made the necessary changes,
run your program.
Note: For questions (b) and (c), do not write a new program
from scratch you only need to add the necessary changes to the
above program.
-
A user has to enter positive float values from the keyboard when
prompted by the program. To indicate the end of the input, the
user enters a negative number1.
When all data has been entered the program should output the number of numbers entered, the minimum value
entered, the maximum value entered and the average of the positive values entered.
If there is no data to be entered
(the user enters a negative number initially,)
then the program should output a message indicating that no data has been entered.
1. Note: If the user enters a
negative number when there are no more numbers to process, then the program can easily
recognize this entry as not being a data
entry and hence terminate the entry of data. This technique of placing a
sentinel to terminate
an input list is very common
Here is a sample of output:
There were 15 numbers entered
The average of all positive values is: 145.67
The minimum value is: 43.67
The maximum value is: 323.35
Here is another possible sample of input/output:
Enter next value: -145.67
No data has been entered
- Write a program that asks the user to enter a word. The program then
repeats the
word for as many times as it has characters. Use string and any related
functions.
Here is an example of input/output:
Enter a word: Zombie
Zombie
Zombie
Zombie
Zombie
Zombie
Zombie
- Textbook: Page 225, problem #9. (Use the switch statement
for the usage code).
-
Write program which will print out a table of ascending
powers of 2.
- The greatest value
than should be printed must be less than 10,000.
- Now change your program so that the user can
enter a value for the stopping value (i.e., a value to replace the 10,000).
Here is a sample of output:
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
... ...