Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

TPD 4124 Program Design

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.

Enter the current meter reading : 34


Enter the previous meter reading : 21

-------------------------------------
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;

a. What value is assigned to f?


b. Explain what is actually happening, and what the programmer likely wanted.
c. Rewrite the code as the programmer intended.

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

Rose Price : RM 14.40 (6 x RM 2.40)


Lily Price : RM 3.90 (3 x RM 1.30)
Service Charge : RM 0.92
Payment : RM 19.22

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

Given the values :


int p=6, q=2, r=4, s;
Evaluate the following expressions. Show your workings clearly.
The expressions are NOT 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

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.

This program computes a baseball player's batting average.

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 .

SOLUTION (To be discussed)


Students can study this solution first.
// This program creates a random addition program, gives the student the
// opportunity to enter an answer. It then displays the correct answer.
#include <iostream>
#include <iomanip>
#include <cstdlib> // Needed to use rand() and srand()
#include <ctime> // Needed for system to "seed" the random number generator
using namespace std;

int main()
{
const int MAX_NUM = 9; // To allow numbers between 1 and 9

unsigned seed; // Seed for the random number generator


int number1, // The two randomly generated numbers to be added
number2,

Page 5 of 6
TPD 4124 Program Design

correctAnswer; // Correct answer to the problem


char ready;

seed = time(0); // Assigns a system-generated seed


// Alternatively, user can be prompted to enter a seed
srand(seed);

// Produce and display a problem


number1 = 1 + rand() % MAX_NUM;
number2 = 1 + rand() % MAX_NUM;
cout << "\n " << number1 << endl;
cout << "+ " << number2 << endl;
cout << " -- \n ";

// Calculate the correct answer


correctAnswer = number1 + number2;

// Display the answer when the student presses the ENTER key
cin.get(ready);
cout << " *** Correct answer is " << correctAnswer << " *** \n";

return 0;
}

/* SAMPLE RUN RESULT

9
+ 9
--
18
*** Correct answer is 18 ***
*/

Page 6 of 6

You might also like