Analysis and Design Tutorial 2
Analysis and Design Tutorial 2
The purpose of this tutorial is to give you a basic understanding of program analysis
and design, focusing in selections and is based on lecture 4c. The tutorial is split into
two: Examples and Exercises. The examples section shows you how to analyse a
question and design a program that implements the analysis. The exercises are
questions for you to attempt. Advice: Attempt the exercise questions in groups and
share ideas.
EXAMPLES
1. [If Statement]
Write a program that will receive a person’s age as an input, checks if the
person is eligible to vote. (Voting age is 18 or older).
ANALYSIS
Purpose: Check if the person is eligible to vote
Inputs: Age
Outputs: ‘Eligible to Vote’
Processes: Age >= 18
DESIGN
L1: Start
L2: Age as Real
L3: Output ‘Enter Age’
L4 : Input Age
L5: If Age >= 18 then
L6: Output ‘Eligible to Vote’
L7: End If
L8: Stop
DESIGN
L1: Start
L2: Temperature as Real
L3: Output ‘Enter Temperature’
L4 : Input Temperature
L5: If Temperature > 30 then
L6: Output ‘Hot’
L7 Else
L9 Output ‘Cool’
L10: End If
L11: Stop
DESIGN
L1: Start
L2: Number as Real
L3: Output “Enter Number”
L4 : Input Number
L5: If Number == 1 then
L6: Output “Sunday”
L7 Else if Number == 7
L8 Output “Saturday”
L9 Else
L10 Output “Working Day”
L11: End If
L12: Stop
4. [Logical Operators]
A person can be eligible for a loan if their income is greater than 5000 OR
they have a credit score higher than 700. Write a program that receives the
income and credit score as input and check if the person qualifies for a loan
and if they do, display ‘Eligible’ else display ‘Ineligible’.
ANALYSIS
Purpose: Check if a person is eligible for a loan
Inputs: Income, credit score
Outputs: ‘Eligible’. ‘Ineligible’
Processes: Income > 5000 OR credit score > 700
DESIGN
L1: Start
L2: Income as Real
L3 Credit Score as Real
L4: Output “Enter Income”
L5 : Input Income
L6: Output “Enter Credit Score”
L7 Input Credit Score
L5: If Income > 5000 OR credit score > 700 then
L6: Output “Eligible’”
L9 Else
L10 Output “Ineligible’”
L11: End If
L12: Stop
5. [Nested If Statements]
You’re tasked with writing a program that will check if a customer qualifies for
a discount after ordering a meal at a restaurant. The program will receive the
bill amount as an input. If the bill amount is greater than 100, the program
should do the following:
- calculate and display a discount amount which is 10% of the bill amount.
- If the discount amount is greater than 20, Recalculate the discount amount
by dividing it by 2, else Recalculate the discount amount by subtracting 5
from it.
- Recalculate the bill amount by subtracting the discount amount from it.
Display the bill amount at the end of the program.
ANALYSIS
Purpose: Calculate and display discount amount if the bill amount
exceeds 100 and display the Bill amount
Inputs: Bill amount
Outputs: Discount amount, bill amount
Processes: -Bill amount > 100,
-Discount amount = Bill amount * 0.1,
-Discount amount > 20
-Discount amount = Discount amount / 2
-Discount amount = Discount amount - 5
-Bill amount = Bill amount – Discount amount
DESIGN
L1: Start
L2: Bill amount as Real
L3: Discount amount as Real
L4: Output “Enter Bill Amount”
L5 : Input Bill amount
L6: If Bill amount > 100 then
L7: Discount amount = Bill amount * 0.1
L8: Output Discount amount
L9 If Discount amount > 20 then
L10: Discount amount = Discount amount/2
L11: Else
L12: Discount amount = Discount amount - 5
L13: End if
L14: Bill amount = Bill amount – Discount amount
L15: End If
L16: Output Bill Amount
L17: Stop
EXERCISES
Use the examples as a guide to help you answer the questions below.
1. You have been tasked to write a program that informs a student if they
passed. The student will provide their marks as an input. Your program
should check if the score is greater than or equal to 50 and display ‘Passed’
if its.
2. Write a program that will receive the users age as input and check for the
following: If the age is below 13, display "Child", else display "Adult".
3. Write a program that will be used to display the body mass index (BMI) of a
user. The user will be asked to provide their weight and height. The program
will then calculate the BMI as: weight divided by the square of the height. If
the BMI is less than 18.5 then display ‘Underweight’ else if the BMI is greater
than 25.0 then display ‘Overweight’, else display ‘Normal Weight’.
4. Write a program that will display whether an individual is allowed to withdraw
from their bank account. The program should receive the account balance
and the withdraw amount as inputs. A person is eligible to withdraw money
if their account balance is greater than or equal to the withdrawal amount. If
they are eligible check if the withdrawal amount is not greater than the
withdrawal limit of 2500. Display ‘Eligible’ if all the conditions are met else
display ‘Ineligible’.
5. Write a program that will inform a student if they can go on a school trip with
or without consent. Students that are between 22 and 25 years of age, and
are in fourth year or higher are allowed to travel without consent. While
students between 19 and 21 of age and are below fourth year, will need
consent. Your program should display ‘No Consent Needed’ or ‘Consent
Needed’, depending on the above stated conditions.
6. Write a program that checks if a year that is provided by the user is a leap
year and output ‘Leap year’ if it is, else output ‘Regular year’. The condition
for a leap year is that is divisible by 4 but not 100 OR it is divisible by 400.