0% found this document useful (0 votes)
5 views

Algorithm Design 1

Uploaded by

Claire Firman
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Algorithm Design 1

Uploaded by

Claire Firman
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

ALGORITHMS Page 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

The following conventions will be used:


1. Keywords are written in capitals.
2. Structural elements come in pairs, e.g. for every BEGIN there is an END
3. Indenting is used to show the structure.
4. The names of the methods are underlined in the main algorithm
5. Nested loops are numbered e.g. LOOP1, ENDLOOP1; LOOP2, ENDLOOP2
6. If more than one file is used, the files are numbered e.g. ACCESS FILE1 (customers.txt),
CREATE FILE2 (videos.txt)
ALGORITHMS Page 2

EXAMPLES USING IEB STANDARD ALGORITHM DESIGN METHODS

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

2. Display a menu giving a choice of converting a temperature in Celcius to Fahrenheit, or


Fahrenheit to Celcius. Input the menu choice and the temperature. Do the conversion, then
print both temperatures. (F=9/5C+32)

display “1 = convert Celcius to Fahrenheit, 2 = convert Fahrenheit to Celsius”


input choice
input temperature
if choice = 1
Then F  9 / 5 * temperature + 32
Display “Celcius: “, temperature, “Fahrenheit: “, F
Else C  (temperature – 32) * 5 / 9
Display “Fahrenheit: “, temperature, “Celcius: “, C
end if

3. Input 3 words. Print the longest word.

READ 3 words wrd1, wrd2, wrd3


Find the number of characters in each word len1, len2, len3
IF (len1 > len2) AND (len1 > len3)
THEN longest = wrd1
ELSE IF (len2 > len1) AND (len2 > len3)
THEN longest = wrd2
ELSE longest = wrd3
ENDIF
ENDIF
WRITE longest
ALGORITHMS Page 3

4. Display the following menu:


A. Print red stars all over the screen
B. Print blue stars all over the screen
C. Quit

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

Method printstars (col)


REPEAT
Change text colour to col
Generate random number(s) for a position on the screen
Move to that position on the screen
WRITE (screen) a star
UNTIL a key is pressed
End Method

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.

ACCESS FILE CDS.TXT


CREATE CDsArray
size  0
READ from file into line
REPEAT
numberCD  get first token from line
singer  get second token from line
song  get third token from line
CDsArray[size]  instantiate a new object with (numberCD, singer, song)
size  size + 1
READ from file into line
UNTIL end of file is reached
CLOSE FILE

7. Generate and print the following pattern of stars:


*
**
***
****
*****
BEGIN MAIN
LOOP1 from 1 to 5
LOOP2 from 1 to LOOP1
WRITE (screen) ‘* ’
ENDLOOP2
Move to beginning of next line
ENDLOOP1
END MAIN
ALGORITHMS Page 5

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

BEGIN SUB inputData


LOOP from 0 to 9
READ (from keyb)letters[loop]
END LOOP
END SUB

BEGIN SUB sortData


LOOP1 from 8 to 0
LOOP2 from 0 to LOOP1
IF (letters [LOOP2]is bigger than letters[LOOP2+1]
THEN
temp = letters [LOOP2]
letters [LOOP2] = letters [LOOP2 + 1]
letters [LOOP2 + 1] = temp
END IF
END LOOP2
END LOOP1
END SUB

BEGIN SUB displayData


LOOP from 0 to 9
WRITE (screen) letters[LOOP1]
END LOOP
END SUB

BEGIN SUB searchForData


READ (keyb) searchItem
found = false
first = 0
last = 9
WHILE found = false and first <= last
mid = (first + last) divided by 2
IF letters[mid] = searchItem
THEN found = true
ELSE IF letters[mid] > searchItem
THEN last = mid –1
ELSE first = mid + 1
END IF
END IF
END WHILE
IF found = true
THEN WRITE (screen) searchItem found
ELSE WRITE (screen) searchItem not found
END IF
END SUB

You might also like