Given this pseudocode:
input: sequence of numbers ak
k, length of sequence
answer := a1
for i = 2 to k
if (ai > answer), then answer = ai
End-for
What is the value of answer for the sequence {-1, 4, -7, 10, 2}
with k = 5?
Please provide detailed answer! Thank you!
Here a1 = -1, a2=4, a3=-7, a4=10, a5=2
answer=a1=-1
The loop will run from i=2 to 5.
@ i= 2 , a2>answer, so answer => 4
@ i= 3 , a3! >answer , so value of answer remains same ie 4.
@ i=4 , a4>answer is True, so answer = a4= 10
@ i=5 , a5>answer is False, so value of answer remains unchanged.
Thus the value of answer is 10.
The given sequence is finding maximum value of the given sequence.
If you have any questions comment down. Please don't simply downvote and leave. If you are satisfied with answer, please? upvote thanks
Get Answers For Free
Most questions answered within 1 hours.