FPPGG Tutorial 3 PDF
FPPGG Tutorial 3 PDF
NB:
Please consult on these exercises even if you think you know the
answer you can even utilize the communication groups you have
for consultations.
Theory Section: Do not use Visual studio or any other compiler to answer this
Se questions, this will help you get a better understanding of the subject
matter at hand.
Question 1:
1.1 In each of the following cases, write only the specified structures do not write the while
algorithm. Make use of the pre-test loop (for-loop and do-while) in pseudocode using
the variable names as provided.
1.1.1 Use a counter variable called count that has an initial value of 5, a final value of
20, and an increment of 2.
1.1.2 Use a counter variable called count that has an initial value of 1.05, a final value of
16.5, and an increment of 0.2.
FPPGG01 Tutorials
1
1.2.1 for x = 31 to 15 step -3
z = z + (x – 5)
next x
1.2.4 z = 1
for x = 1 to 6 step 3
z=z+1
for y = 6 to 3 step -1
z=z+y*2
next y
next x
1.2.5 x = -4
z=1
do while x <= 7
display z, “ “ , x ~ On the same line
z=z+1
x=x+2
loop
1.2.6 x = 6
Z = 15
do
display x , “ ” z
x=x–1
z=z+1
loop until x <= 4
FPPGG01 Tutorials
2
1.3 Study the following algorithm in pseudocode. Display the exact output that will be
displayed after the statements have been executed:
1.3.1 ExactOutput1
x=5
y=x*4–2
z=x*y
display “This is the exact output”
for w = 0 to x step 2
y = y mod 4 + w + 3
z=z+y
display w, x, y, z ~ on new line
display “******”
next w
display “This is outside the loop”
end
1.3.2 ExactOutput2
k=5
m=2
n=9
do while k < n
k = k +2
m=m*n
loop
display k, m, n
end
1.3.3 ExactOutput3
d=2
e=4
do while d < e
display “d is less than e”
f=1
do while f < e
display “f is less than e”
f = f +1
loop
display “Outer loop ends”
d=d+1
Loop
display “Inner loop ends here”
end
FPPGG01 Tutorials
3
1.4 Rewrite the following using the specified looping structure/s:
num1 = 20
do while x <= -15
display “x = “, x
x=x-3
loop
FPPGG01 Tutorials
4
Question 2
2 In each of the following cases, do the necessary planning (IPO) and write an algorithm in
pseudo code for the following:
2.1 Display every number from 1 to 25 along with it’ s value double and tripled.
2.1.1 Use a for-loop.
2.1.2 Use a while-loop.
2.2 The factorial of an integer, like 5, is the product of 5x4x3x2x1. Calculate the factorial of an
integer value entered by the user.
2.3 Write an algorithm to allow users to enter a quantity of numbers until a negative
number is entered. Then display the highest and lowest number entered.
2.3.1 Use a while-loop to determine the answer.
2.3.2 Use a do-loop-until to determine the answer.
2.4 Write an algorithm that will calculate the current tuition for Computer Systems
engineering. The current tuition is R35 000 per year, and the tuition is expected to
increase by 4 percent each year. Display the tuition each year for the next 10 years.
2.5 Write an algorithm that will accept the following for 100 students: The students’ name
(studName) and 3 test marks (marks), as well as one exam mark (examMark). The
program must calculate the final mark (finMark) for every student. The final mark
comprises of 50% of the exam mark added to 50% of the average (agv) of the 3 tests.
Display the final mark for every student.
2.5.1 Use a simple for-loop to determine the answer.
2.5.2 Use a nested for loop to determine the answer.
2.5.3 Mix the for loop with a do-while
2.6 The user must enter a positive integer between 5 and 15. If this number is valid, the
user must also choose between a triangle (T or t) and a square (S or s). Display suitable
error messages if necessary; otherwise, the program must use an asterisk to draw a
triangle or a square of the chosen size and display. Use a Select Case structure to make
the decision. Display appropriate error messages where applicable.
2.7 Do the necessary planning (IPO) and write an algorithm in pseudo code for the
following:
Practical Section:
Question 3
3.1 Convert the following design into C++:
Write C++ program for the algorithm planned in the design below into the source file. Also,
enter appropriate comments and any additional instructions required by the compiler.
The cost of a theatre ticket is R125 per ticket if less than 5 tickets are bought, R100 per
ticket if 5 to 24 tickets are bought, and R90 per ticket if a block of 25 or more tickets are
bought. Calculate the total cost for a purchaser depending on the number of tickets bought.
Continue with the process until the number of tickets bought is 0.
calculate_amount_due
~ get the first number of tickets
display “Enter the number of tickets bought – 0 to stop”
enter number
do while number <> 0
~ calculate the amount due
if number < 5
amount = number * 125
else
if number < 25
amount = number * 100
else
amount = amount * 90
endif
endif
~ display the result
display “The amount due is R“ amount
display “Ready for next customer”
display “Enter number of tickets bought – 0 to stop”
enter number
loop
end
FPPGG01 Tutorials
6
Question 4
Students in Programming 1 had written a test and they need to know their performance.
Write a C++ program that will help them achieve this using the information given below:
FPPGG01 Tutorials
7
Figure 4.2: No response
Question 5
Students in Engineering 1 would like to understand the concept of Ohm’s law. You are
required to write an application that will allow them to calculate the relation between
Voltage, Current and Resistance using the formulas below.
V = IR
Resistance= V/R
Prompt the user for the calculation he/she would like to see or N/n to exit (See
Figure 5.1).
Based on the option selected by the user (I/I, V/v, R/r),
o The user must be prompted for the related data as shown in Figure 5.2 –
Figure 5.4.
o The program must then calculate the related measurements based on the
option selected.
o Display the desired measurement.
If an invalid option is selected an appropriate error message must be displayed, see
Figure 5.5.
FPPGG01 Tutorials
8
Figure 5.1 Options available
FPPGG01 Tutorials
9
Figure 5.4 Options R/r
FPPGG01 Tutorials
10