Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Computer Assignment for AS

Uploaded by

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

Computer Assignment for AS

Uploaded by

sumanbasnet80123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment

1 (a) The following pseudocode is an attempt to define an algorithm that takes two numbers as
input and outputs the larger of the two numbers.

DECLARE A, B : INTEGER
INPUT A
INPUT B
IF A > B
THEN
OUTPUT A
ELSE
OUTPUT B
ENDIF

The algorithm needs to be amended to include the following changes:

1. Input three values, ensuring that each value input is unique.


2. Output the average.
3. Output the largest value.

Write the pseudocode for the amended algorithm.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................
© UCLES 2020
...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

............................................................................................................................................. [6]

(b) Complete the pseudocode expressions in the following table.

Use only functions and operators described in the Appendix on pages 18–19.

Expression Evaluates to

"ALARM: " & ...................... ("Time: 1202" , ......................) "ALARM: 1202"

...................... ("Stepwise." , ...................... , ......................) "wise"

1.5 * .......................... ("OnePointFive") 18

...................................... (27.5) "27.5"

.......................... (9, 4) 2
[5]

(c) A problem may be decomposed into sub-tasks when designing an algorithm.

Give three benefits of using sub-tasks.

1 ................................................................................................................................................

...................................................................................................................................................

2 ................................................................................................................................................

...................................................................................................................................................

3 ................................................................................................................................................

...................................................................................................................................................
[3]

© UCLES 2020 9608/22/O/N/20 [Turn over


8

2 (a) The following structured English describes an algorithm used to count the number of odd and
even digits in an input sequence.

1. Initialise variables OddCount and EvenCount to zero.


2. Prompt and input an integer.
3. If the integer is not in the range 0 to 9 then go to step 7.
4. If the integer is an even number then add 1 to EvenCount.
5. Otherwise add 1 to OddCount.
6. Repeat from step 2.
7. Output "Same" if there are the same number of odd and even integers.
8. Output "Odd" if there are more odd than even integers.
9. Output "Even" if there are more even than odd integers.

Draw a flowchart on the following page to represent the algorithm.

© UCLES 2020 9608/22/O/N/20


9

[7]
© UCLES 2020 9608/22/O/N/20 [Turn over
10

(b) The following pseudocode is an attempt to check whether two equal-length strings consist of
identical characters.

Refer to the Appendix on pages 18–19 for the list of built-in functions and operators.

FUNCTION Compare(String1, String2 : STRING) RETURNS BOOLEAN


DECLARE x, y, Len1, Len2 : INTEGER
DECLARE RetFlag : BOOLEAN
DECLARE NextChar : CHAR
DECLARE New : STRING

Len1 LENGTH(String1)
RetFlag TRUE

FOR x 1 TO Len1 // for each char in String1


Len2 LENGTH(String2)
NextChar MID(String1, x, 1) // get NextChar from String1
New ""
FOR y 1 TO Len2 // for each char in String2
IF NextChar <> MID(String2, y, 1) // no match
THEN
New New & MID(String2, y, 1) // save this char from String2
ENDIF
ENDFOR
String2 New // replace String2 with New
ENDFOR

IF LENGTH(String2) <> 0 // anything left in String2 ?


THEN
RetFlag FALSE
ENDIF

RETURN RetFlag

ENDFUNCTION

© UCLES 2020 9608/22/O/N/20


11

(i) Complete the trace table below by performing a dry run of the function when it is called
as follows:

Result Compare("SUB", "BUS")

The first row has been completed for you.

String1 String2 Len1 RetFlag x Len2 NextChar New y


"SUB" "BUS" 3 TRUE 1

[5]

(ii) State the value returned.

..................................................................................................................................... [1]

© UCLES 2020 9608/22/O/N/20 [Turn over

You might also like