Professional Documents
Culture Documents
CLASS
SECTION
Computer Science
Midterm Exam. Paper 2
Max. Marks = 55
2210/02
December2014
1 hour 45 minutes
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
x(5) n
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(5) n
x(1)
x(5) n
(ii)
(iii)
x(1)
x(5) n
[2]
[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
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