Draw a series of variable diagrams for the program below using the conventions of the Study Guide. Assume that
the following input is given: 2010 t
1 #include <iostream>
2 #include <string>
3 using namespace std;
4 int main()
5 {
6 int year; char code;
7 bool book = true; float discount = 0.20;
8 cin >> year >> code;
9 switch (year)
21
10 {
11 case 2008: case 2009:
12 if (code == 'c')
13 if (!book)
14 discount += 0.20;
15 break;
16 case 2010:
17 if (book)
18 if (code == 't')
19 {
20 book = false;
21 code = 'g';
22 }
23 case 2011:
24 if (discount > 0.20 || code == 'g')
25 discount = 0.15;
26 else
27 discount += 0.10;
28 default:
29 discount = 0.25;
30 code = 'b';
31 book = true;
32 }
33 discount = 0.35;
34 cout << year << " " << code << " " << book << " "
<< discount << endl;
35 return 0;
36 }
At every
voting station the voters vote by choosing A, B or C on a ballot paper. The voting officer must enter the votes into
the program so that they can be counted. X is entered when all the votes at a specific voting station have been
entered.
The program :
The three totals and the number of spoilt votes are initialised to 0. Use the following integer variables
votesForA, votesForB, votesForC, spoiltVotes
Use a for loop, going from 1 to the number of voting stations.
Inside this loop is a while loop. A prompting message appears on the screen, asking the voter for which
candidate he or she wants to vote. The choice of the voter is then input.
Inside the while loop is a switch statement to increment the correct total. The default option is used
to count the number of spoilt votes.
The while loop is exited when X is entered for the choice.
When the for loop is exited, the three totals and the number of spoilt votes are displayed.
The following incomplete program first asks the user to enter the number of items he/she has eaten today and then
to enter the number of calories for each item. It then calculates the number of calories he/she has eaten for the day
and displays the value.
You have to complete the code. First complete the code by using a while loop to read in the calories of all the
items. Compile and run your program and submit only the code that you added and the output. Then change you
program to use a for loop to read in the calories of all the items. Compile and run the program again and submit
only the code that you added and the output. Use the variables that have already been defined in the given program.
Test your program by entering 7 for the number of items and the following values for the calories:
20
7 120 60 150 600 1200 300 200
If your logic is correct, the following will be displayed:
Total calories eaten today = 2631
The following code is supposed to display the positive even numbers less than 12. That is, it will output the numbers
2, 4, 6, 8 and 10. However, there is a logical error in the code. Explain what the output of the code below will be.
Then write a small program including the code below and make the necessary changes to fix the code so that it
displays what it is intended to display. Ensure that your program works correctly. Only submit the program, not the
output.
Hint: Use variable diagrams to trace the program to help you find the logical error.
int x = 1;
while (x != 12)
{
cout << x << endl;
x = x + 2;
A bookshop gives discount to customers as follows:
Students get 10% discount,
Book dealers get 12% discount and
Pensioners get 15% discount.
All other customers get 10% discount only if their total purchases are more than R200.
You are requested to write two versions of a program that calculates and displays the final amount that is due, after
discount.
(i) The first version of the program uses a switch statement to implement the above program.
(ii) The second version of the program uses nested-if statements.
A bookshop gives discount to customers as follows:
Students get 10% discount,
Book dealers get 12% discount and
Pensioners get 15% discount.
All other customers get 10% discount only if their total purchases are more than R200.
You are requested to write two versions of a program that calculates and displays the final amount that is due, after
discount.
(i) The first version of the program uses a switch statement to implement the above program.
(ii) The second version of the program uses nested-if statements
Include the for loop below in a small program and complete the program. The loop should execute 10 times. Do
not change the for loop below. Compile and run your program to see for yourself that it works. You do not have
to submit this program and output.
for (int i = 1; i <= n; i++)
cout << i * i;
Now convert the for loop into a while loop and add any variable initialisations that you think are necessary.
Compile and run your program and submit only the program containing the while loop and its output.
The Computer Science Department follows certain criteria when a student learns to program. A number of
programming exercises must be worked through. To proceed to the next exercise a student has to obtain a mark
of 50% or more and must have completed 5 or more program runs. You are requested to write a program to validate
if a student can proceed to the next program.
Your program should have the following structure:
Declare two integer variables programsDone and result.
Validate the data captured for the two variables using a while loop.
The loop should be repeated until the value of result is greater than or equal to 50 and the value of
programsDone is greater than or equal to 5.
Display a message like "Good! You can now proceed to the next exercise"
You are requested to write a very simple calculator. Your calculator should be able to handle the five basic
mathematic operations – add, subtract, multiply, divide and modulus – on two input values.
18
Your program should have the following structure:
Ask the user to enter two float variables named var1 and var2
Ask the user to enter a character variable named operation to represent the operation to be performed
on the two variables.
Perform the appropriate operation by using if-statements
The output must be given in fixed-point notation with two digits after the decimal point.
A typical run is displayed below:
Please enter the first float value: 35.6
Please enter the second value: 24.12
Please enter the operation required : +
The sum of 35.6 and 24.12 is 59.72
Find a good cookbook. Read the instructions for your favourite dish. Write a program to display the recipe on the
screen but with a difference. The quantity of each ingredient must be multiplied by a value entered from the
keyboard.
For example, if the recipe needs 2 cups of flour the output displayed will be something like this
Please enter the factor to multiply the ingredients with : 4
Recipe name
Ingredients
// other ingredients
8 cups flour
Method
The program has the following structure:
Declare an int variable named mFactor to store the value with which the quantity of each ingredient
must be multiplied.
The program must make use of a cin statement to input a value from the keyboard and store it in mFactor.
Submit both your program and output.