Algorithm and Programming Languages (COME2102)

National Higher Polytechnic Institute (NAHPI)

Semester: Second Semester

Level: 200

Year: 2018

(30 marks)
THE UNIVERSITY OF BAMENDA-2017/2018
SECOND SEMESTER EXAMINATION
School: NAH.PI Bamenda Department: ALL Departments
Course title: Algorithms and Programming Languages Code: C0ME2202 Semester: 2
Instructor: Mr. MAIMII R. Yiran Time: 2 hours
Instruction: Answer All Questions
We want to compute a
n
for a real number a and for a positive integer n.
a.
Write two versions of this algorithm, the first should use the for loop and the second the
while loop
b.
Evaluate in terms of n, the number of times we need to enter the loop to compute a
n
.
We want to now compute


c.
Write an algorithm Sum _Power that uses the algorithm Power above. Evaluate in
terms of n the number of loops turn necessary to compute


d.
Write a second algorithm to compute


This time, try to optimize the number of
loop turns.
(20 marks)
The algorithm below rearranges numbers stored in a one-dimensional array called List. Ptr is
an integer variable used
as an
index (subscript) which identifies elements within List. Temp is
a variable, which is used as a temporary store for numbers from List.
Ptr 1
While Ptr< 10 Do
If List[Ptr] > List[Ptr + 1] then
Temp List[Ptr]
List[Ptr] List[Ptr + I]
List[Ptr + 1] Temp
Erdif
PtrPtr + 1
End While
(a) Dry run the following by completing the table below
Ptr
Temp
List
[1] [2] [3] [4] [5]
[6] [7] [8]
[9 ]
[ 1 0 ]
43
25
37
81
18
10
64
96
52
4
1
(b)
What will happen when Ptr = 10?
(c)
If the whole algorithm is now applied to this this rearrangement list, what will be the value of:
( i )
L i s t [ 1 ]
( i i )
L i s t [ 9 ]
( i i i )
L i s t [ 1 0 ]
www.schoolfaqs.net