WAP to overload insertion and extraction operator
How will the C++ program would look like if we create a UDF that will prompt the user to input the size of the figure (5-15) odd numbers only and display the figure like this:
Sample Output:
Input size of figure to generate (5 - 15) odd numbers only: 5
# # #
# #
# # # # #
# #
# # #
Parents of the pupils of the Park Primary School must pay an amount for outfits for the annual
play. All pupils take part in the play, except the Grade 0 pupils. The amount that the parents have
to pay is calculated as follows:
The cost of the outfits for Grade 1 and 2 pupils is R45.
The cost of the outfits for Grades 3 to 5 is R65.
Grade 6 and 7 pupils may play one or two roles. If they play a lead role, they may only play
one role. The cost of the outfits is R70 if they play one role. If this role is a lead role, the
cost is R100. If they play two roles, the cost is R130
Use a switch statement and write down ONLY the necessary C++ statements to
calculate and display the amount to be paid or display an appropriate error message if
required.
Do NOT write a complete program. Use the following
variables:
int grade;
int fee;
bool leadrole; //true if a child plays a lead role
bool roles2; //true if a child plays 2 roles
Parents of the pupils of the Park Primary School must pay an amount for outfits for the annual
play. All pupils take part in the play, except the Grade 0 pupils. The amount that the parents have
to pay is calculated as follows:
The cost of the outfits for Grade 1 and 2 pupils is R45.
The cost of the outfits for Grades 3 to 5 is R65.
Grade 6 and 7 pupils may play one or two roles. If they play a lead role, they may only play
one role. The cost of the outfits is R70 if they play one role. If this role is a lead role, the
cost is R100. If they play two roles, the cost is R130.
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