0% found this document useful (0 votes)
121 views4 pages

Past Paper Quests. For C++

This document contains solutions to three programming questions involving different concepts: 1) A program to calculate grass mowing time given house and block dimensions. 2) A program to calculate quadratic equation roots given coefficients a, b, c. 3) A program to convert 50 Fahrenheit temperatures to Celsius and display results.

Uploaded by

Musa Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views4 pages

Past Paper Quests. For C++

This document contains solutions to three programming questions involving different concepts: 1) A program to calculate grass mowing time given house and block dimensions. 2) A program to calculate quadratic equation roots given coefficients a, b, c. 3) A program to convert 50 Fahrenheit temperatures to Celsius and display results.

Uploaded by

Musa Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SOLUTIONS TO QUESTIONS

This paper contains three questions; each question has three parts.

Students are required to answer all questions

QUESTION 1: SEQUENCE

A program is required to read in the length and width of a rectangular house block, and the
length and width of the rectangular house that has been built on the block. The program should
then compute and display the time required to cut the grass around the house, at the rate of two
square metres per minute.

a) Develop a Defining Diagram (Input, Process, Output) for the above problem. (13 marks)
b) Develop a Solution Algorithm (Pseudocode) for the above problem. (13 marks)
c) Write the C++ code for solving the above problem. (7 marks)

SOLUTIONS: Question 1 sequence:

a) Defining Diagram – IPO chart

INPUT PROCESS OUTPUT


block_length Prompt for block measurements mowing_time
block_ width Get block measurements
house_length Prompt for house measurements
house_width Get house measurements
Calculate mowing area
Calculate mowing time

b) The Solution algorithm:

Calculate_mowing_time
Prompt for block_length, block_width
Get block_length, block_width
block_area = block_length * block_width
Prompt for house_length, house_width
Get house_length, house_width
house_area = house_length * house_width
mowing_area = block_area - house_area
mowing_time = mowing_area/2
Output mowing-time to screen
End
QUESTION 2: SELECTION

The classic quadratic formula says that the two roots of the quadratic equation

are:

a) Develop a Defining Diagram (Input, Process, Output) for the above solution. (13 marks)

b) Develop a Solution Algorithm (Pseudocode) for the above solution. (13 marks)

c) Write the C++ code (program) for the above solution. (8 marks)

SOLUTIONS Question 2 selection:

Defining Diagram

INPUT PROCESS OUTPUT


a 1. READ values of a, b and c, x1
b 2. if a is zero then stop as we do not have a quadratic, x2
c 3. calculate value of discriminant
4. if D is zero then there is one root: ,

5. if D is > 0 then there are two real roots: and ,

6. if D is < 0 there are two complex roots: and


,
7. PRINT solution.
Question 2 The Solution algorithm:

QUESTION 3: REPETITION/ITERATION

Every day a weather station receives 50 temperatures expressed in degrees Fahrenheit. A


program is to be written that will accept each Fahrenheit temperature, convert it to Celsius and
display the converted temperature to the screen. After 50 temperatures have been processed, the
words “All temperatures processed” are to be displayed on the screen.

a) Develop a Defining Diagram (Input, Process, Output) for the above problem. (13 marks)
b) Develop a Solution Algorithm (Pseudocode) for the above problem. (13 marks)
c) Write the C++ code for solving the above problem (7 marks)
Note: Create an integer called temperature_count, initialized to zero (0) to control the 50
repetitions.

Use the formula celsius = (fahrenheit – 32) * 5/9


Question 3 Solutions Iteration/Repetition:
a) Defining Diagram – IPO chart

INPUT PROCESS OUTPUT


f_temp Get fahrenheit temperatures c_temp
Convert temperature
(50 temperatures) Display Celsius temperatures (50 temperatures)
Display screen message

Solution algorithm Question 3

Fahrenheit_Celsius_conversion
Set temperature_count to zero
DOWHILE temperature_count < 50
Prompt for f_temp
Get f_temp
Compute c_temp = (f_temp – 32) * 5/9
Display c_temp
Add 1 to temperature_count
ENDDO
Display ‘All temperatures processed to the screen
End

The solution algorithm could also have been written using a WHILE...DO and ENDWHILE

Fahrenheit_Celsius_conversion
Set temperature_count to zero
WHILE temperature_count < 50 DO
Prompt for f_temp
Get f_temp
Compute c_temp = (f_temp – 32) * 5/9
Display c_temp
Add 1 to temperature_count
ENDDO
Display ‘All temperatures processed to the screen
END

You might also like