0% found this document useful (0 votes)
16 views17 pages

CH-9 Problem Solving

Uploaded by

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

CH-9 Problem Solving

Uploaded by

aabrothers049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

4 This is a section of program code.

1 Total = 100.00
2 PRINT ′Enter the height of each member of your class, one at a
time, when prompted′
3 FOR Count = 1 TO 30
4 PRINT ′Enter a height in metres′
5 INPUT Height
6 Total = Total + Height
7 PRINT Total / 30
8 Count = Count + 1
9 NEXT Count

(a) There are three errors in this code.

State the line numbers that contain the errors and describe how to correct each error.

Error 1 .......................................................................................................................................

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

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

Error 2 .......................................................................................................................................

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

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

Error 3 .......................................................................................................................................

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

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

(b) State the purpose of this program.

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

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

...............................................................................................................................................[1]
3 Four programming concepts and four descriptions are shown.
Draw a line to connect each programming concept to the most appropriate description.

Programming concept Description


A subroutine that does not
Library routine
have to return a value.

Structure A standard subroutine that is


diagram available for immediate use.

A subroutine that always


Procedure
returns a value.

An overview of a program or
Function
subroutine. [3]

4 A programmer has written a routine to store the name, email address and password of a contributor
to a website’s discussion group.
(a) The programmer has chosen to verify the name, email address and password.
Explain why verification was chosen and describe how the programmer would verify this data.

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

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

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

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

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

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

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

...............................................................................................................................................[4]
(b) The programmer has also decided to validate the email address and the password.
Describe validation checks that could be used.

Email address ............................................................................................................................

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

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

Password ..................................................................................................................................

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

................................................................................................................................................[2]
5 A program checks that the weight of a basket of fruit is over 1.00 kilograms and under
1.10 kilograms. Weights are recorded to an accuracy of two decimal places and any weight not in
this form has already been rejected.

Give three weights as test data and for each weight state a reason for choosing it. All your reasons
must be different.

Weight 1 ............................................................................................................................................

Reason ..............................................................................................................................................

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

Weight 2 ............................................................................................................................................

Reason ..............................................................................................................................................

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

Weight 3 ............................................................................................................................................

Reason ..............................................................................................................................................

..........................................................................................................................................................
[3]
4 A programmer has written a routine to check that prices are below $10.00. These values are used
as test data.

10.00 9.99 ten


Explain why each value was chosen.
10.00 ................................................................................................................................................

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

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

9.99 ..................................................................................................................................................

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

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

ten ....................................................................................................................................................

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

..........................................................................................................................................................
[3]
2 This section of program code asks for 80 numbers between 100 and 1000 to be entered. It checks
that the numbers are in the correct range, and stores them in an array. It counts how many of the
numbers are larger than 500 and then outputs the result when the program is finished.

1 Count = 0
2 FOR Index = 1 TO 80
3 INPUT 'Enter a number between 100 and 1000', Number
4 WHILE Number = 99 AND Number = 1001
5 INPUT 'This is incorrect, please try again', Number
6 ENDWHILE
7 Num[80] = Number
8 IF Number > 500 THEN Count = Count + 1
9 UNTIL Index = 80
10 PRINT Index
11 PRINT ' numbers were larger than 500'
There are four lines of code that contain errors.
State the line number for each error and write the correct code for that line.

Error 1 Line Number .............................

Correct Code ....................................................................................................................................

Error 2 Line Number .............................

Correct Code ....................................................................................................................................

Error 3 Line Number .............................

Correct Code ....................................................................................................................................

Error 4 Line Number .............................

Correct Code ....................................................................................................................................


[4]
3 (a) Explain the difference between a validation check and a verification check.

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

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

...............................................................................................................................................[2]
(b) Describe, using an example, how data could be verified on data entry.
...................................................................................................................................................

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

...............................................................................................................................................[2]
(c) Explain what is meant by the term library routine.
...................................................................................................................................................

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

...............................................................................................................................................[2]
4 An algorithm has been written in pseudocode to input 100 numbers and print out the sum.
A REPEAT … UNTIL loop has been used.

Count ← 0
Sum ← 0
REPEAT
INPUT Number
Sum ← Sum + Number
Count ← Count + 1
UNTIL Count > 100
PRINT Sum

(a) Find the error in the pseudocode and suggest a correction.

Error ...........................................................................................................................................

Correction .................................................................................................................................

...................................................................................................................................................
[2]

(b) Rewrite the correct algorithm using a more suitable loop structure.

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

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

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

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

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

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

...............................................................................................................................................[3]
2 This section of program code asks for 50 numbers to be entered. The total and average of the
numbers are calculated.

1 Total = 0
2 Counter = 50
3 PRINT ′When prompted, enter 50 numbers, one at a time′
4 REPEAT
5 PRINT ′Enter a number′
6 INPUT Number
7 Total + Number = Total
8 Number = Number + 1
9 UNTIL Counter = 50
10 Average = Number * Counter
11 PRINT ′The average of the numbers you entered is ′, Average

There are four errors in this code.


State the line number for each error and write the correct code for that line.
Error 1 Line number .............................
Correct code .....................................................................................................................................

Error 2 Line number .............................

Correct code .....................................................................................................................................


Error 3 Line number .............................
Correct code .....................................................................................................................................

Error 4 Line number .............................


Correct code .....................................................................................................................................
[4]
3 A satellite navigation system works using destination details entered by the user, either a new
destination or chosen from previously saved destinations. The satellite navigation system will then
output directions to the destination in the form of either a visual map or a list of directions.
A satellite navigation system is an example of a computer system that is made up of sub-systems.
This structure diagram shows some of its sub-systems.
Complete the diagram by filling in the empty boxes.

Satellite navigation system

Input destination

Map List

[2]
4 For each of the four statements in the table, place a tick in the correct column to show whether it
is an example of validation or verification.

Statements Validation Verification


To automatically check the accuracy of a bar code
To check if the data input is sensible
To check if the data input matches the data that has been
supplied
To automatically check that all required data fields have been
completed [4]

5 (a) Describe the purpose of each statement in this algorithm.


FOR I 1 TO 300
INPUT Name[I]
NEXT I
...................................................................................................................................................

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

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

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

...............................................................................................................................................[2]
(b) Identify, using pseudocode, another loop structure that the algorithm in part (a) could have
used.
...................................................................................................................................................

...............................................................................................................................................[1]
(c) Write an algorithm, using pseudocode, to input a number between 0 and 100 inclusive. The
algorithm should prompt for the input and output an error message if the number is outside
this range.
...................................................................................................................................................

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

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

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

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

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

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

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

...............................................................................................................................................[3]
2 Read this section of program code that:

• inputs 10 numbers
• checks whether each number is within a specified range
• totals the numbers within the range and outside the range

1 InRange = 0
2 OutRange = 1000
3 FOR Count = 1 TO 10
4 INPUT Num
5 IF Num > 10 AND Num < 20 THEN InRange = InRange + 1
6 ELSE OutRange = OutRange - 1
7 Count = Count + 1
8 NEXT X
9 PRINT InRange, OutRange

(a) There are four errors in this code.

Locate these errors and suggest a correction to remove each error.

Error 1 ........................................................................................................................................

Correction ..................................................................................................................................

Error 2 ........................................................................................................................................

Correction ..................................................................................................................................

Error 3 ........................................................................................................................................

Correction ..................................................................................................................................

Error 4 ........................................................................................................................................

Correction ..................................................................................................................................

...............................................................................................................................................[4]

(b) Decide, with reasons, whether the numbers 10 and 20 are within or outside the range.

Within Outside
Number Reason
range (✓) range (✓)

10
……………………………………………………………………..

……………………………………………………………………..

20
……………………………………………………………………..

……………………………………………………………………..

[4]
4 Four validation checks and four descriptions are shown below.
Draw a line to link each validation check to the correct description.

Validation check Description

Numbers between two


Presence check
given values are accepted

Data is of a particular
Range check
specified type

Data contains an exact


Type check
number of characters

Ensures that some data


Length check have been entered
[3]
2 Read this section of program code that inputs positive numbers, discards any negative numbers
and then outputs the average. An input of zero ends the process.
1 Total = 0
2 Counter = 100
3 REPEAT
4 REPEAT
5 INPUT Num
6 UNTIL Num < 0
7 Total = Total + 1
8 Counter = Counter + Num
9 UNTIL Num = 0
10 Average = Total / (Counter - 1)
11 Print Average
There are four errors in this code.
Locate these errors and suggest a correction to remove each error.

Error 1 ..............................................................................................................................................

Correction .........................................................................................................................................

Error 2 ..............................................................................................................................................

Correction .........................................................................................................................................

Error 3 ..............................................................................................................................................

Correction .........................................................................................................................................

Error 4 ..............................................................................................................................................

Correction ......................................................................................................................................... [8]


2 Read this section of program code that inputs 10 positive numbers and then outputs the total.

1 Total = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
5 Total = Total + Num
6 PRINT Total
7 Counter = Counter + 1
8 UNTIL Counter = 10

This code works, but it is inefficient.

(i) Suggest three improvements that could be made.

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

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

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

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

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

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

(ii) Rewrite the program code with your improvements.

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

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

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

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

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

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

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

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

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

...............................................................................................................................................[3]
2 Read this section of program code that inputs 10 positive numbers and then outputs the smallest
number input.

1 Small = 1000
2 Counter = 0
3 REPEAT
4 INPUT Num
5 IF Num < Small THEN Small = Num
6 Counter = Counter + 1
7 UNTIL Counter = 10
8 PRINT Small

(i) Identify three changes you would need to make to find the largest number input instead
of the smallest number.

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

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

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

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

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

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

(ii) Rewrite the program code with your changes.

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

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

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

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

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

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

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

.......................................................................................................................................[3]
2 Read this section of program code that should input 50 numbers and then output the average of
the positive numbers only.

1 Total = 0

2 PosCount = 0

3 FOR Counter = 1 TO 50

4 INPUT Num

5 IF Num < 0 THEN Total = Total + Num

6 IF Num > 0 THEN Counter = Counter + 1

7 Average = Total/PosCount

8 NEXT Counter

9 PRINT Num

There are four errors in this code.

Locate these errors and suggest code corrections to remove each error.

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

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

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

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

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

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

4 .......................................................................................................................................................

......................................................................................................................................................[4]
4 A routine checks the age and height of children who are allowed to enter a play area. The children
must be less than 5 years of age and under 1 metre in height.

(a) The first set of test data used is age 3 and height 0.82 metres.

State what type of test data this is.

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

Give a reason for using this test data.

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

...............................................................................................................................................[2]

(b) Provide two additional sets of test data. For each, give

• the type of each set of test data


• the reason why it is used

Each type of test data and reason for use must be different.

Set 1 .........................................................................................................................................

Type ..........................................................................................................................................

Reason .....................................................................................................................................

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

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

Set 2 .........................................................................................................................................

Type ..........................................................................................................................................

Reason .....................................................................................................................................

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

...............................................................................................................................................[6]
2 Read this section of program code that should input 50 numbers and then output the average.

1 Total = 0

2 For Counter = 1 TO 50

3 INPUT Num

4 Total = Total + 1

5 Counter = Counter + 1

6 Average = Total/Counter

7 NEXT Counter

8 PRINT Average

There are four errors in this code.

Locate these errors and suggest code corrections to remove each error.

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

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

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

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

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

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

4 .......................................................................................................................................................

......................................................................................................................................................[4]

4 A routine checks the weight of melons to be sold in a supermarket. Melons weighing under
0.5 kilograms are rejected and melons weighing over 2 kilograms are also rejected.

Give an example of each type of test data for this routine.

Normal .............................................................................................................................................

Extreme ............................................................................................................................................

Abnormal ......................................................................................................................................[3]
2 Read this section of program code that should input 10 positive numbers and then output the
smallest number input.

1 Small = 0

2 Counter = 0

3 REPEAT

4 INPUT Num

5 IF Num < Small THEN Num = Small

6 Counter = Counter + 1

7 PRINT Small

8 UNTIL Counter < 10

There are four errors in this code.

Locate these errors and suggest a corrected piece of code for each error.

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

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

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

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

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

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

4 .......................................................................................................................................................

......................................................................................................................................................[4]
2 Read this section of program code that should input 30 positive numbers and then output the
largest number input.

1 Large = 9999

2 Counter = 0

3 WHILE Counter > 30

4 DO

5 INPUT Num

6 IF Num < Large THEN Large = Num

7 Counter = Counter - 1

8 ENDWHILE

9 PRINT Large

There are four errors in this code.

Locate these errors and suggest a corrected piece of code for each error.

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

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

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

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

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

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

4 .......................................................................................................................................................

......................................................................................................................................................[4]
4 Read this section of program code that inputs twenty (20) numbers and then outputs the largest
number input.

1 h = 0
2 c = 0
3 REPEAT
4 READ x
5 IF x > h THEN x = h
6 c = c + 1
7 PRINT h
8 UNTIL c < 20

There are three errors in this code.

Locate these errors and suggest a corrected piece of code.

[3]

You might also like