Professional Documents
Culture Documents
PD Practice1 Question
PD Practice1 Question
Practice 1
Question 1
Write a program that inputs a character from the keyboard and then outputs a large block
letter “C” composed of that character. For example, if the user inputs the character “X,”
then the output should look as follows:
XXX
X X
X
X
X
X
X
X X
XXX
Question 2
Write a complete C program that calculates the water bill for a consumer. The details
are as stated below.
Each unit used costs RM 0.05. Set this as a constant
Initialise all variables in main().
Ask the user to enter the current month and previous month meter readings.
Calculate the total units used.
Calculate the bill and display the output as shown below.
-------------------------------------
BILL
-------------------------------------
Units used : 13
Bill : RM 0.65
-------------------------------------
Page 1 of 6
TPD 4124 Program Design
Question 3
Given the following fragment that purports to convert from degrees Celcius to degrees
Fahrenheit, answer the following questions:
double c = 20;
double f;
f = (9/5) * c + 32.0;
Question 4
Write a complete C program that calculates the payment the customer has to pay when
buying flowers at a florist.
Rose costs RM2.40 each and Lily costs RM1.30 each. Set both of the values as
constant using the const keyword.
Initialise all variables in main().
Ask the user to enter the amount of roses and lilies that they would like to buy.
Calculate the cost of the purchase.
There is a service charge of 5% for the purchase.
Calculate and display how much a customer has to pay for the overall purchase.
The output is as shown in the next page.
Stalks of Rose ? : 6
Stalks of Lily ? : 3
Page 2 of 6
TPD 4124 Program Design
QUESTION 5
Convert the following mathematical formulas to its equivalent C++ expressions.
[Note: Use appropriate functions from cmath header file]
i. √𝑎 + 𝑏
√
ii.
QUESTION 6
i. s = (p++ % --r*q) + 5
ii. s = r % ++q - p
SOLUTION
i = (p++ % --r*q) + 5
= (6++ % --4*2)+5
= (6 % 3*2) +5
= (0*2) + 5
= 5
ii
Page 3 of 6
TPD 4124 Program Design
QUESTION 7
Given the values :
int p=6, q=2, r=4, s;
Evaluate the following expressions. Show your workings clearly.
The expressions ARE related to each other.
i. s = (p++ % --r*q) + 5
ii. s = r % ++q - p
SOLUTION
i = (p++ % --r*q) + 5
= (6++ % --4*2)+5
= (6 % 3*2) +5
= (0*2) + 5
= 5
ii
QUESTION 8 (HOMEWORK)
Write a program to find a baseball player's batting average . The program should ask
the user to enter the player’s name, the number of times the player was at bat and the
number of hits earned. It should then display the batting average to four decimal
places. See the sample output.
Enter first name <space> last name of the player : John Lion
How many times was the player at bat? 84
How many hits did the player get? 18
John Lion's batting average is 0.2143
Page 4 of 6
TPD 4124 Program Design
QUESTION 9
What is the output of the following program lines when embedded in a correct program
that declares month, day, year, and date to be of type string?
month = "03";
day = "04";
year = "06";
date = month + day + year;
cout << date << endl;
QUESTION 10
Write a program that can be used as a math tutor for a young student . The program
should display two random numbers between 1 and 9 to be added, such as
2
+1
After the student has entered an answer and pressed the [Enter] key, the program should
display the correct answer so the student can see if his or her answer is correct .
int main()
{
const int MAX_NUM = 9; // To allow numbers between 1 and 9
Page 5 of 6
TPD 4124 Program Design
// Display the answer when the student presses the ENTER key
cin.get(ready);
cout << " *** Correct answer is " << correctAnswer << " *** \n";
return 0;
}
9
+ 9
--
18
*** Correct answer is 18 ***
*/
Page 6 of 6