0% found this document useful (0 votes)
7 views7 pages

7.1 Worksheet 4-1 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Name:...................................................................................................... Class: ......................

Task 1
An algorithm has been given below.

1 MoreItems  TRUE
2 A  0
3
4 REPEAT
5 INPUT ItemPrice
6 A  A + ItemPrice
7 INPUT More
8 IF More = "No"
9 THEN
10 MoreItems  FALSE
11 ENDIF
12 UNTIL NOT MoreItems
13
14 OUTPUT A

(a) What is the minimum number of times that the loop will be run?

(b)The user enters the text “Yes” in line 7. What will the value of MoreItems be at the ENDIF in
line 11?

(c) The user enters the text “No” in line 7. What will the value of MoreItems be at the ENDIF in
line 11?

(d) Explain the line of code in line 6.

(e) State the purpose of this algorithm.

1
(f) Give a more suitable name for the variable currently named A.

Task 2
An array named Ages is set up that has five numbers stored in it:

1 2 3 4 5

Ages 17 15 13 14 16

To access age 13 in the array, the programmer uses Ages[3].

Make use of a FOR loop to calculate the average for all the ages in the array. Output the
average that is calculated.

2
Task 3
An array named Players is set up that has five names stored in it:

1 2 3 4 5

Players "Tony" "Amy" "Aruna" "Sophia" "Jack"

The pseudocode to search this array is given below.

1 DECLARE Names : ARRAY[1:5] OF STRING


2 Names  ["Tony", "Amy", "Aruna", "Sophia", "Jack"]
3 Found  FALSE
4
5 INPUT SearchItem
6
7 FOR i  1 TO 5 //alternatively 1 TO
LENGTH(Ages)
8 IF Names[i] = SearchItem
9 THEN
10 OUTPUT SearchItem, "Found"
11 Found  TRUE
12 ENDIF
13 ENDFOR
14 IF NOT Found
15 THEN
16 OUTPUT SearchItem, "Not Found"
17 ENDIF

Describe each of the following lines of code in the table below:

Line of code Description


3

10

11

14

16

3
(b) State the name of this algorithm.

Task 4 Bubble sort


Your teacher will give you a set of name cards.

Place the cards in the sequence of numbers 1, 2, 3… 10

Perform a manual Bubble Sort to get the cards in alphabetical order.

1. What are the last two names after the first pass?

2. What are the first two names after the second pass?

3. What are the third and fourth names after the third pass?

4. What are the seventh and eighth names after the fourth pass?

5. What are the fourth and fifth names after the fifth pass?

4
6. What are the first two names after the sixth pass?

7. Which names are out of sequence after the seventh pass?

8. Are any names out of sequence after the eighth pass?

9. How many passes were needed to sort the cards?

5
Task 5: Algorithms
You have studied three algorithms for sorting.

Look at the following algorithm written in pseudo-code for one type of sorting algorithm.

1 Names ← ["Toby", "Abdul","Gale","Ruby","Amy","Boris"]


2 NumItems ← LENGTH(Names) - 1
3 WHILE NumItems > 1 DO
4 FOR Item ← 0 TO NumItems
5 IF Names[Item] > Names[Item+1]
6 THEN
7 Temp ← Names[Item]
8 Names[Item] ← Names[Item+1]
9 Names[Item+1] ← Temp
10 ENDIF
11 ENDFOR
12 NumItems ← NumItems - 1
13 ENDWHILE
14 FOR i ← 0 TO NumItems
15 OUTPUT Names[i]
16 ENDFOR

1. There is a for loop at the end of the algorithm (lines 13-15) which contains the
pseudo-code:
OUTPUT names[i]
What do you think this does?

2. At the start of the program, the array called names is initialised with six names.
What will the value be that is stored in numItems?

6
3. Look at lines 5 to 10 in the code and explain what they are doing.

4. State the name of this algorithm.

You might also like