
Originally Posted by
Algohelp
Hi guys,
Took an exam back in June which dealt mainly with algorithms, however I never really put the time into the module so the exam was beyond me at times. Was just wondering if anyone could spare the time to check out a few of the questions I came up against, as I've got a retake around the corner and can't seem to get my head around them. Probably very trivial to you guys!
Ok, here it goes..
1) Write an algorithm which calculates both the maximum and minimum of a non-empty array A[1...n]. Examples of its application:
Input:
[3,7,0]
[10,11,2,1]
Output:
7 0
11 1
When writing the algorithm you are allowed to use only once occurrence of a looping construct; that is one for loop or once occurrence of a while loop.
and,
Ask your self what is difficult about:
Code:
read n, A[1..n]
lo = maxint
hi = minint
for idx=1 to n
if A[idx]<lo
lo=A[idx]
endif
if A[idx]>hi
hi=A[idx]
endif
endfor
return {lo,hi}
end Then let us know and we can address your real problem
RonL