Tutorial Week 5 - An Introduction to Selection


Home


Question 1

For the following problems specify the conditional part of the problem (what the conditions are and what actions are dependent on the conditions).

(a) A program is required to add up three numbers and display their total.

(b) A program is required to divide two numbers together and display the result. However if the second number is a zero then the calculation can't be done as dividing a number by zero gives a result of infinity.

(c) A Bank requires a program to calculate the balance of an account after a transaction. The possible transaction types are "D" for Deposit and "W" for Withdrawal. The bank teller inputs the initial account balance, the transaction type and the transaction amount.

(d) A dog kennel requires a program to calculate the cost of kennelling (boarding) dogs. The cost of kennelling a dog depends on the size of the dog (Small = $6, Medium = $8, Large = $10) and the number of days the dog stays in the kennel. No discounts are given for longer stays (e.g. the same daily rate applies for each day). The user inputs the daily charge amount (e.g. $8) depending on the size of the dog, and the number of days of the stay. The total cost is then to be displayed.

(e) A program is required to display a grade (e.g. B) for a numeric mark (e.g. 75) input by the user. The grades are A (80 to 100), B (70 to 79), C (60 to 69), D (50 to 59) and N (0 to 49).

Question 2

For the following problem, work through steps 1 (Understanding) through 6 (Writing Code) of the Steps in Program Development discussed in lectures.

Problem Description

Write a program to display the larger of two numbers input by the user.

Example Runs

Run
First number  ? 10
Second number ? 7
Largest number is 10

Run
First number  ? 6
Second number ? 22
Largest number is 22

Run
First number  ? 9
Second number ? 9
Largest number is 9

Question 3

Problem Description

Write a program to calculate the water rates for a customer. All customers are charged a service fee of $100. In addition domestic customers are charged $0.01 per litre and commercial customers $0.02 per litre of water used..

Example Runs

Run
Volume used (litres) ? 5000
Type of user (D=Domestic, C=Commercial) ? D
Cost = $150.00

Run
Volume used (litres) ? 10000
Type of user (D=Domestic, C=Commercial) ? C
Cost = $300.00

Written by Tim Whitfort.