Writing Algorithms Flashcards
The three parts to an algorithm:
Input
Process
Output
Input -
Here the user needs to input some data
Process
Here you’ll need to perform some form of process on your data e.g perform a calculation, perform a condition or check on your data)
Output
Output something to the user
Algorithm used to calculate the price of an item with a 10% off discount
price = input(“Please enter the process of the product: “) INPUT
new_price = price * 0.9 PROCESS
print(new_price) OUTPUT
Write an algorithm that:
1. Allows the user to input the base and height of a triangle
2. Calculates the area of the triangle
3. Outputs the area of the triangle
- height = input(“Enter the height: “)
- base = input(“Enter the base: “)
- area = (base * height) / 2
- print(area)
A company wants to create a program that alerts the user to the freeing and boiling points
of water. The boiling point of water is 100 degrees Celsius, and the freezing point of water is 0 degrees Celsius.
Write an algorithm that:
• Allows the user to input the temperature of the water
• Calculates whether the water is boiling, freezing, or neither
• Outputs appropriate messages to the user
INPUT -
- water = input(“Input the temperature of the water: “)
PROCESS -
- if water > 100 then
- elseif water <= 0 then
- else
- endif
OUTPUT -
- print(“The water is at boiling point”)
- print(“The water is at freezing point”)
- print(“The water isn’t not boiling or freezing”)
A parcel tracking company want to create a program to check whether an inputted parcel
number is the required 8 characters or not. If a parcel number is 8 characters it should output
the message ‘VALID’, otherwise it should output the message ‘INVALID’.
Write an algorithm that:
• Allows the user to input the parcel number
• Calculates whether the parcel number is 8 characters or not
• Outputs appropriate messages to the user
INPUT -
- parcel = input(“Enter the parcel number: “)
PROCESS -
- if parcel.length == 8 then
- else
- endif
OUTPUT -
- print(“VALID”)
- print(“INVALID”)
A student wants to create a program using a function. The aim of the program is to return
the day of week based on its number e.g. Monday = 1, Tuesday = 2 etc. up to Sunday = 7.
Write an algorithm that:
• Creates a function named DayOfWeek and takes the number of the day as a parameter
• Calculates what day is outputted for each number
• Returns the day of the week
INPUT -
function DayOfWeek (number)
endfunction
PROCESS -
if number == 1 then
elseif number == then
(up to 7)
else
endif
OUTPUT -
day = “Monday”
day = “Tuesday”
(up to Sunday)
return day