Sample Algorithms

Here are some samples to try.

Algorithm 1

A greengrocer wants to make sure that all his bags of fruit are a similar weight – between 1.4 and 1.6kg.

Write an algorithm that asks how many bags need to be checked. Allow the greengrocer to enter the weight of these bags. A message should be output to say whether the bag is too heavy or too light. It should also output the total number of bags that were the correct weight.

For example, with inputs:

Enter number of bags to be checked 23
Enter weights of bag 1 1.57
Enter weight of bag 2 1.62
Bag is too heavy
Enter weight of bag 3 1.39
Bag is too light
 - - - 
Enter weight of bag 23 1.48
15 Bags had the correct weight

 Algorithm 2

Write an algorithm simple number guessing game. A secret number should be generated at random. The user should input a guess and a message should be output to tell them if the secret number is higher or lower, or if they guessed the correct number. They should continue to guess until they guess the correct number

For example, with inputs:

Enter Guess 7
Higher
Enter Guess 9
Lower
Enter Guess 8
Correct Number

Algorithm 3

The owner of a car dealership wants to regularly update his website to show the typical prices of his cars.

Write an algorithm that allows the user to enter the prices of a number of cars. The program should output the highest, lowest and average price of the cars.

For example, with inputs:

Please enter number of cars: 4
Please enter price of car 1:4800
Please enter price of car 2:5600
Please enter price of car 3:9700
Please enter price of car 4:7100
The highest price is: 9700
The lowest price is: 4800
The average price is: 6800

 

Advertisement
%d bloggers like this: