Algorithm Design 1
Algorithm Design 1
DESIGNING ALGORITHMS
An algorithm is a set of well-defined steps in a logical order that can be followed to produce a
solution to a problem. It’s a “recipe” or plan describing a solution to a problem in simple steps,
written in ordinary English, or in pseudocode, or in the form of a flowchart.
When writing algorithms, remember it’s not JAVA. Don’t use the same syntax as JAVA. EG.
When you want the length of a word, don’t say “word.length”, it’s OK to say “word length”.
Describing something is acceptable: EG: “if not the end of the word” OR “if character at loop in
word is a number”
BUT NOT if (word.charAt(loop).isNumber())
Pseudocode Guidelines
Pseudocode is essentially English statements, with some rules of structure.
BEGIN MAIN, END MAIN For start and finish of the main segment
BEGIN METHOD (name), END METHOD(name) For a method
READ (from keyboard/file) For input
WRITE (to screen/file) Output
IF, THEN, ELSE, ENDIF Selection
CASE, ELSE, ENDCASE Selection
LOOP FROM startvalue TO endvalue, ENDLOOP Unconditional repetition
WHILE, ENDWHILE Pre-test conditional repetition
DO, WHILE Post-test conditional repetition
CREATE FILE (external filename), CLOSE FILE Creating a file
ACCESS FILE (external filename), CLOSE FILE Accessing an existing file
1. A person buys 3 articles of clothing. Their prices exclude VAT. Determine the total of the
prices excluding VAT, VAT at 14% and the final total including VAT. Print all 3 calculated
amounts.
input price1
input price2
input price3
TotalExVAT = price1 + price2 + price3
VAT = totalExVAT * 0.14
Total = totalExVAT + VAT
print totalExVAT, VAT, total
Input the menu choice. Call a method using a parameter for either of the first 2 choices, then, after
a short delay, redisplay the menu, until Quit is chosen.
BEGIN MAIN
REPEAT
Display menu
A) Print Red Stars
B) Print Blue Stars
C) Quit
READ choice
CASE choice is
A : printstars (red)
B : printstars (blue)
ENDCASE
UNTIL choice = ‘C’
END MAIN
5. In a simple number game, the computer generates a random number between 1 and 100
(inclusive). The player has to try to guess the number by inputting guesses and receiving
responses, until s/he gets it correct. Then a congratulatory message is printed.
BEGIN MAIN
num = a random number in the range 1..100
READ a guess
WHILE guess <> num
IF guess < num
THEN WRITE “Too low”
ELSE WRITE “Too high”
ENDIF
READ a guess
END WHILE
WRITE “Congratulations! You guessed it”
END MAIN
ALGORITHMS Page 4
6. A text file called CDS.TXT stores details of songs on CDs. It contains an unknown number of
lines of text in the form
number of CD#name of singer#song
eg 23#Elton John#I’m Still Standing
Read each line of text from this file, separate each line into 3 fields (Number of CD, Singer and
Song), then instantiate a new object that is stored in an array.
8. Store10 lowercase letters in an array. Display them in alphabetical order. Ask the user to input a
letter and search for the letter. Display a message on the screen indicating whether the item was
found or not.
BEGIN MAIN
iputData
sortData
displayData
searchForData
END MAIN