You are on page 1of 8

Semester 2

DSO17BT Delicious Dish 2 2018

Your weekly intellectual supplement

1. TUTORIAL 1
1.1 Indicate the exact output that will be produced after the following code has been
executed: (5)

mainModule
num1 = 5
num2 = 10
do while num1 < num2
result = DoFunc1 (num2, num1)
display result
num1 = num1 + 3
loop
display num1, “,”, num2
answer = DoFunc2 (num2)
display “Answer = ”, answer
if result = answer then
display “The same”
else
display “Not the same”
endif
end

function DoFunc1 (valNum1, valNum2)


return valNum1 + valNum2 \ 2

function DoFunc2 (valNum)


valNum = valNum mod 2
return valNum

1 DSO17AT
2.2 Write a full algorithm in pseudocode to solve the following:

Troy Baloyi owns four communes near Excellent University of Technology, as follows:

Commune Address Price per Sq Metre


1 260 Maroela Road R750
2 12 Mopani Road R620
3 111 Cabbage Tree Road R555
4 325 Coral Tree Road R500

He wants you to write a program to calculate the rent per month for each room of each
commune. Each commune has a different number of rooms. The rent is determined by the
size of the room. Enter for each commune, for each room in the commune, the name of the
room, as well as the length and width of the room. Calculate the size of the room in a function.
Then determine the rent for the room in another function. Display the rent. Also determine
the total rent that each commune brings in, as well as the address of the commune that
brought in the highest rent. Determine this address also in a function.

The output must be in the following format:

The rent for room Carnation is R2500


.
.
The total rent for 260 Maroela Road is R15500
.
.
The commune that brings in the highest rent is 12 Mopani Road

2 DSO17AT
2. PRACTICAL ASSIGNMENT 2

2.1 Convert the following design into C++. Also enter appropriate comments and any
additional instructions required by the compiler. See figure 1 for sample output:

DetermineBookWorth
~ This program determines the price of books delivered to a
~ bookstore. The price is dependent on a certain mark-up on the
~ purchase price, that in tern is dependent on the mark-up code.

totAmntForDelivery = 0
highestWorth = 0
highestSupplName = “”

moreBoxes = true
do while moreBoxes
boxWorth = 0
display “Enter the name of the supplier of the box ”
enter supplierName
display “How many books in this box?”
enter numBooks
for book = 1 to numBooks
display “Enter the book’s mark-up code”
enter markUpCode
display “Enter the purchase price of the book”
enter purchasePrice

sellPrice = DeterminePrice(markUpCode, purchasePrice)


if sellPrice <> 0 then
display “Book ”, book, “ has a price of R”, sellPrice
else
display markUpCode, “ is an invalid mark-up code”
endif
boxWorth = AccumulateTotal(boxWorth, sellPrice)
next book

if boxWorth > highestWorth then


highestWorth = boxWorth
highestSupplName = supplierName
endif

totAmntForDelivery = AccumulateTotal(totAmntForDelivery, boxWorth)


display “Are there any more boxes? (true(1) or false (0))”
enter moreBoxes
loop
display “--------------------------------------------”
display “The total worth of the delivery is R”, totAmntForDelivery
display “The supplier whose box had the highest worth is ”,
highestSupplName
display “--------------------------------------------”
end

function DeterminePrice(valMarkUpCode, valPurchasePrice)


select case valMarkUpCode
case “A”, “B”
price = valPurchasePrice * 1.45
case “C”
price = valPurchasePrice * 1.55
case “D”, “E”, “F”
price = valPurchasePrice * 1.7
case else
price = 0.0

3 DSO17AT
endselect
return price

function AccumulateTotal(valCurrTot, valValToAdd)


newTot = valCurrTot + valValToAdd
return newTot

Figure 1 – sample output

4 DSO17AT
2.2 Consider the following programming problem and solution algorithm. Code this
solution in C++, then compile and run it. See figure 3 for sample output.

For each customer, enter the number of items bought and the unit price for one
item. The amount due must then be calculated and displayed as follows:
 Call a function to determine the percentage discount:
o If a user bought 5 or fewer items, no discount is granted.
o If the user bought more than 5 but less than or = 10 items, 5%
discount is granted
o If the user bought more than 10 but less than or equal to 20 items,
7.5% discount is granted
o If the user bought more than 20 items, 10% discount is granted.
o The function must return the value 0, 5, 7.5 or 10, depending on the
outcome of the if-statement.
 Calculate the initial amount due in the main algorithm
 Call a second function to calculate the discount amount
 Deduct the discount amount from the amount due in the main algorithm
 Call a third function to add 14% vat to the amount due
 Display the answer in the main algorithm.

Repeat this for a number of purchases. In the end display the total amount due by all
customers. Accumulate the total amount due

MainAlgorithm
totalAmntDue = 0
display “Enter the number of items bought (-1 to end)”
enter number
do while number <> -1
display “Enter the unit price”
enter price
discountPerc = CalcDiscPerc(number)
amountDue = number * price
discountAmount = CalcDiscount(amountDue, discountPerc)
amountDue = amountDue – discountAmount
amountDue = CalcFinal(amountDue)
display “The amount due = R”, amountDue
totalAmntDue = totalAmntDue + amountDue

display “Enter the number of items bought (-1 to end)”


enter number
loop
display “The total amount due by all customers is R”, totalAmntDue
end
function CalcDiscPerc(valNumber)
If valNumber < 5 then
discPerc = 0
else
if valNumber <= 10 then
discPerc = 5
else
if valNumber <= 20 then
discPerc = 7.5
else
discPerc = 10
endif
endif

5 DSO17AT
endif
return discPerc
function CalcDiscount(valAmt, valDisc)
return valAmt * valDisc / 100

function CalcFinal(valAmtDue)
return valAmtDue * 1.14

Figure 2.2 – sample output

2.3 Write а value-turning function, isVowel, that returns the value true if а given
character is а vowel and otherwise return false.
Write а program that prompts the user to input а sequence of characters and
outputs the number of vowels. (Use the function isVowel)

Refer to figure 1 and 2 for sample output. Save your source file as
runTest11.cpp. Include your student number, surname and initials, and group
at the beginning of the source files. Submit only the C++ source file.

6 DSO17AT
Figure 1: output 1

Figure 2: output 2

7 DSO17AT
INSTRUCTIONS TO SUBMIT PRACTICAL ASSIGNMENT 2

1. Copy and paste all the Practical Assignment Solutions into one document (e.g. Ms Word
or Notepad or Any code editor)
2. Include the Student Number, Surname and Initials, Group and Lecturer Surname as
comments inside the document.
3. Logon to myTutor
4. Select the Subject on myTutor Dashboard
5. Click the Delicious Dishes link on the left menu
6. Click on Delicious Dish1 link (Not on the Attached File)
7. Click Browse My Computer button to attach your file
8. Navigate to the file that contains all your Programs and click Open button from the
Choose File Dialog
9. Click on Submit Button on from Top-Right or Bottom-Right of web page

Due date: (Fri) 31 Aug 2018 Time: 23:59

8 DSO17AT

You might also like