Você está na página 1de 18

1

BEACONHOUSE SCHOOL SYSTEM, MARGALLA CAMPUS


NORTHERN REGION, ISLAMABAD

CLASS

SECTION

Computer Science
Midterm Exam. Paper 2
Max. Marks = 55

Problem solving and Programming

2210/02
December2014
1 hour 45 minutes

Weightage: Paper 1 = 60%, Paper 2 = 40%


Candidates answer on the Question Paper.
No Additional Materials are required.
READ THESE INSTRUCTIONS FIRST
Write your Class, section and name on all the work you hand in.
Write in dark blue or black pen.
You may use a soft pencil for any diagrams, graphs or rough working.
Do not use staples, paper clips, highlighters, glue or correction fluid.
DO NOT WRITE IN ANY BARCODES.
Answer all questions.
No marks will be awarded for using brand names of software packages or hardware.
At the end of the examination, fasten all your work securely together.
The number of marks is given in brackets [ ] at the end of each question or part
question.

This document consists of 11 printed pages and 1 blank pages.

1.

2.

3. Read the following algorithm and the details of the dry run shown:
BEGIN
INPUT n
FOR i 1 TO n
INPUT x [ i ]
NEXT i
z 0
i 2
a x[1]
IF a <> 0 THEN z 1
WHILE i <= n DO
IF x [ i ] <> 0 THEN
a z *a+x[i]
z z + 1
a a/z
ENDIF
i i + 1
ENDWHILE
PRINT a
END

This is the dry run:


x(1)

x(2) x(3) x(4)

x(5) n

Print

0*0+1=1
1

1/1=1
3

1*1+2=3
3/2=1.5

4
3

2*1.5+3=6
6/3=2

(a) Write down the test data used for this dry run and the output produced.

[1]

(b) Here is the test data for four more dry runs. For each set of test data, produce a
table similar to the one shown above. Show all working.
(i) 5
0
1
0
2
3
(ii) 5
1
0
-1
0
3
(iii) 3
6
0
0
[6]
(i)
x(1)

x(2) x(3) x(4)

x(5) n

Print

x(1)

x(2) x(3) x(4)

x(5) n

Print

(ii)

(iii)
x(1)

x(2) x(3) x(4)

x(5) n

Print

(c)Explain what each of the variables z, i, a and n do.

[2]

(d) What is the purpose of this algorithm?

[2]

4.

5.

6.

10

7.
Write an algorithm, using pseudo code, which performs the following tasks:
a. declares an array of size n and inputs n integers in it(the value
[1]
of n has to be input before the array declaration)
b. prints the average of the integers in the array
[2]
c. prints the number of integers that are greater than 300 in the array [3]
d. prints the number of integers that are between 100 and 200 in
[3]
the array
[5]
e. reverses the order of the integers of the array and prints all n
integers.

11

BLANKPAGE

1.

13

2.

14

3.(a) Theinputdataisn=4,[1mark]
andx(1)tox(4)are0,1,2,3[1mark]
The output is 2 [1 mark]
[3 marks]

(b)(i)
x(1) x(2) x(3) x(4) x(5) n
z
i
a
Print
0
1
0
2
3
5
0
2
0

0*0+1=1

1/1=1

4
1*1+2=3

3/2=1.5

5
2*1.5+3=6

6/3=2

Initialdatacorrect(i.e.valuesofn,x(1),x(2),x(3),x(4)&x(5))[1mark]
Allzvaluescorrect[1mark]
Allivaluescorrect[1mark]
Allavaluescorrect[1mark]
andworkingshown[1mark]
Notethatfollowthroughmarkingshouldbeusedfortheavaluesifthezorivaluesare
wrong.

(ii)
x(1) x(2) x(3) x(4) x(5) n
z
i
a
Print
01
0
1
0
3
5
0
2
1

3
1*11=0

0/2=0

5
3*0+3=3

3/3=1

Correctoutput[1mark]
andallworkingshown.[1mark]

15

(iii)
x(1)
6

x(2)
0

x(3)
0

x(4)

x(5)

n
3

z
0
1

i
2
3
4

a
6

Print

Correctoutput[1mark]
andallworkingshown.[1mark]

(c)

(d)

zcountsthenumberofnonzeroesinput
usedtodividetofindaverage
iusedtopointtoitemsinarray
controlsthewhileloop
aisusedtoholdrunningaverage
nissizeofarray
usedtocontrolwhileloop

1markperpointtoamaximumof4.

[4]

Calculatestheaverage
ofnonzeronumbers.
1markperpoint.

[2]

16

4.

17

5.

18

6.

7. (a)

(b)

INPUT n
arr [ n ]

sum 0
FOR i 1 TO n STEP 1
sum sum + arr [ i ]
NEXT i
ave sum / n
PRINT ave

(c)

FOR i 1 TO n STEP 1
IF arr [ i ] > 300 THEN
PRINT arr [ i ]
END IF
NEXT i

(d)

FOR i 1 TO n STEP 1
IF arr [ i ] > 100 AND arr [ i ] < 200
PRINT arr [ i ]
END IF
NEXT i

(e)

arr2 [ n ]
FOR i 1 TO
arr2 [ i ]
NEXT i
FOR i 1 TO
PRINT arr2 [ i
NEXT i

n STEP 1
arr [ n + 1 i ]
n STEP
]

THEN