1a Which of the following functions does NOT return a correc

1a) Which of the following functions does NOT return a correct maximum of any list of n integers a1, a2, . . . , an?

      
a)function Max (a1, a2, . . . , an : Integers)
max := a1
for i := 1 to n-1
if max < ai+1 then max := ai+1
return max

      
b)function Max (a1, a2, . . . , an : Integers)
min := a1
for i := 2 to n
if min < ai then min := ai
return min

      
c)function Max (a1, a2, . . . , an : Integers)
max := a1
for i := 1 to n
if max < ai then max := ai
return max

      
d)function Max (a1, a2, . . . , an : Integers)
max := a1
for i := 2 to n
if max < ai then max := ai
return amax

Part b

What does the following function compute?

function Unknown (a1, a2, . . . , an : Integers)
j := 1
x := a1
for i := 2 to n
if x ai then
j := i
x := ai
return j

      
a)The maximum value among a1, a2, ..., an.

      
b)The minimum value among a1, a2, ..., an.

      
c)The index of the last element that has a minimum value among a1, a2, ..., an.

      
d)The index of the first element that has a maximum value among a1, a2, ..., an.

Solution

Part a)


a)function Max (a1, a2, . . . , an : Integers)
max := a1
for i := 1 to n-1
if max < ai+1 then max := ai+1
return max

Here, we are comparing \'max\' with (ai)+1, it should be max with a(i+1)

Part b)

c)The index of the last element that has a minimum value among a1, a2, ..., an.

we are finding minimum elements (x) and also storing index

1a) Which of the following functions does NOT return a correct maximum of any list of n integers a1, a2, . . . , an? a)function Max (a1, a2, . . . , an : Intege
1a) Which of the following functions does NOT return a correct maximum of any list of n integers a1, a2, . . . , an? a)function Max (a1, a2, . . . , an : Intege

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site