0% found this document useful (0 votes)
53 views3 pages

Pseudocode Algorithms for Score Analysis

Uploaded by

muaadba
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views3 pages

Pseudocode Algorithms for Score Analysis

Uploaded by

muaadba
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Worksheet 2 Peudocode

Unit 1 Problem solving

Worksheet 2 Developing algorithms using pseudocode


1. Examine the pseudocode program given below. The operator MOD, e.g. a MOD b gives the
remainder when integer a is divided by integer b.

(a) Which lines show an example of the ‘Sequence’ programming construct?

(b) Which lines show an example of the ‘Selection’ programming construct?

(c) There are two examples of iteration statements in the program. On which lines do each
of the ‘Iteration’ programming constructs begin and end?

(d) If the user enters 1 and 10 for the first and last numbers in the range, what will be
printed out at line 15? Which numbers is the program counting?

1. OUTPUT (“This program prints selected numbers in in given range.”)


2. SET anotherGo TO “Yes”
3. WHILE anotherGo = “Yes”
4. SEND “Please enter the first number in your chosen range” TO DISPLAY
5. RECEIVE lowNumber FROM (INTEGER) KEYBOARD
6. SEND “Please enter the last number in your chosen range” TO DISPLAY
7. RECEIVE highNumber FROM (INTEGER) KEYBOARD
8. SET x TO 0
9. FOR count = lowNumber TO highNumber
10. IF count MOD 5 <> 0) AND (count MOD 7<>0) THEN
11. SEND count TO DISPLAY
12. SET x TO x + 1
13. END IF
14. END FOR
15. SEND x, “numbers” TO DISPLAY
16. SEND “Another go?” TO DISPLAY
17. RECEIVE anotherGo FROM (STRING) KEYBOARD
18. ENDWHILE

1
Worksheet 2 Peudocode
Unit 1 Problem solving

2. Here is a flowchart for an algorithm which asks a user to enter a new ID.

Start

Ask new user to


input their chosen
ID

Input ID

Is this Yes Output “Choose


ID already in use? another ID”

No

Write ID to file

End

Write pseudocode equivalent to this flowchart. You can omit the detail of how to check
whether the ID is already in use. Test it with the statement

IF IDinUse ….

2
Worksheet 2 Peudocode
Unit 1 Problem solving

3. Write a pseudocode algorithm which inputs numeric scores and outputs how many of them
are over 100. The end of the data is signalled by a user input of -1.

4. Write a pseudocode algorithm which inputs numeric scores and outputs the average score.
The end of the data is signalled by a user input of -1.

Common questions

Powered by AI

The pseudocode ensures repeatability through the use of a `WHILE` loop. The construct checks the variable `anotherGo` at line 3. As long as `anotherGo` is set to 'Yes', the loop continues, allowing the user to repeat the input and counting process. This provides a mechanism for continuous execution until the user decides to stop .

The 'Selection' programming construct is utilized in line 10 with the IF statement: `IF (count MOD 5 <> 0) AND (count MOD 7<>0) THEN`. Selection allows the algorithm to make decisions based on conditions, directing the flow along different paths depending on whether the condition is true or false .

The algorithm for counting scores over 100 is as follows: ``` SET countOver100 TO 0 SEND 'Enter score or -1 to end:' TO DISPLAY RECEIVE score FROM (INTEGER) KEYBOARD WHILE score <> -1 DO IF score > 100 THEN countOver100 = countOver100 + 1 END IF SEND 'Enter next score or -1 to end:' TO DISPLAY RECEIVE score FROM (INTEGER) KEYBOARD END WHILE SEND countOver100 TO DISPLAY ``` This pseudocode repeatedly checks and counts scores exceeding 100 .

To calculate the average of scores, the pseudocode is: ``` SET total TO 0 SET count TO 0 SEND 'Enter score or -1 to end:' TO DISPLAY RECEIVE score FROM (INTEGER) KEYBOARD WHILE score <> -1 DO total = total + score count = count + 1 SEND 'Enter next score or -1 to end:' TO DISPLAY RECEIVE score FROM (INTEGER) KEYBOARD END WHILE IF count > 0 THEN average = total / count SEND average TO DISPLAY ELSE SEND 'No scores were entered' TO DISPLAY END IF ``` This ensures the calculation accounts for the number of scores entered, including a check for zero scores .

The MOD operator returns the remainder of a division operation. In the pseudocode, it filters out numbers that are divisible by 5 or 7; specifically, `count MOD 5 <> 0` and `count MOD 7<>0` ensure that numbers that are multiples of 5 or 7 are not printed .

To convert the flowchart into pseudocode: ``` SEND 'Please enter a new ID' TO DISPLAY RECEIVE newID FROM KEYBOARD IF IDinUse(newID) THEN SEND 'Choose another ID' TO DISPLAY ELSE WRITE newID TO FILE END IF ``` This emphasizes the decision-making process through condition checks and corresponding actions .

The 'Iteration' constructs begin at lines 3 and 9 and end at lines 18 and 14, respectively. Iteration is crucial for this algorithm to repeatedly execute a block of code, essential for tasks like counting or accumulating values over a range or set times .

The program employs logical reasoning by using conditional checks within a loop structure. Specifically, it evaluates each number in the range for divisibility by 5 and 7 using the MOD operator. If a number is not divisible by either 5 or 7 (i.e., `count MOD 5 <> 0 AND count MOD 7 <> 0`), it is displayed; otherwise, it is skipped. This logic ensures only those numbers that meet the criteria are outputted .

The 'Sequence' programming construct is demonstrated in lines 1, 2, 4, 6, 8, 15, and 16. Sequence is significant in algorithm development as it ensures steps are executed in a specific order, which is crucial for the logical flow and correctness of the algorithm .

At line 15, the program will output '8 numbers', as the algorithm counts the numbers from 1 to 10, excluding those that are multiples of 5 or 7, specifically counting 1, 2, 3, 4, 6, 8, 9, and 10 .

You might also like