Question

Given this pseudocode:   input: sequence of numbers ak k, length of sequence answer := a1 for...

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!

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Given this algorithm written in pseudocode:   Algo(n) Input: A positive integer n Output: Answer, Algo(n) If...
Given this algorithm written in pseudocode:   Algo(n) Input: A positive integer n Output: Answer, Algo(n) If n = 1 Answer = 3 Else Answer = 3 × Algo(n-1) End if What is Algo(4)? Please provide a detailed answer! thank you!
10. Let P(k) be the following statement: ”Let a1, a2, . . . , ak be...
10. Let P(k) be the following statement: ”Let a1, a2, . . . , ak be integers and p be a prime. If p|(a1 · a2 · a3 · · · ak), then p|ai for some i with 1 ≤ i ≤ k.” Prove that P(k) holds for all positive integers k
. Consider the sequence defined recursively as a0 = 5, a1 = 16 and ak =...
. Consider the sequence defined recursively as a0 = 5, a1 = 16 and ak = 7ak−1 − 10ak−2 for all integers k ≥ 2. Prove that an = 3 · 2 n + 2 · 5 n for each integer n ≥ 0
1) Suppose a1, a2, a3, ... is a sequence of integers such that a1 =1/16 and...
1) Suppose a1, a2, a3, ... is a sequence of integers such that a1 =1/16 and an = 4an−1. Guess a formula for an and prove that your guess is correct. 2) Show that given 5 integer numbers, you can always find two of the numbers whose difference will be a multiple of 4. 3) Four cats and five mice form a row. In how many ways can they form the row if the mice are always together? Please help...
Let A = (A1, A2, A3,.....Ai) be defined as a sequence containing positive and negative integer...
Let A = (A1, A2, A3,.....Ai) be defined as a sequence containing positive and negative integer numbers. A substring is defined as (An, An+1,.....Am) where 1 <= n < m <= i. Now, the weight of the substring is the sum of all its elements. Showing your algorithms and proper working: 1) Does there exist a substring with no weight or zero weight? 2) Please list the substring which contains the maximum weight found in the sequence.
The programmer intends for this pseudocode to display three random numbers in the range of 1...
The programmer intends for this pseudocode to display three random numbers in the range of 1 through 10. Locate the error and clearly explain the error. // This program displays three random numbers in the range of 1 through 10. Declare Integer count Declare Real value // Display three random numbers. For count = 1 To 5 Step 2 Set value = count Display random(10, 1) End For
C++ only. Must use loops. Please do not use sequence of if statements. Given as input...
C++ only. Must use loops. Please do not use sequence of if statements. Given as input string and an int N, set result a string made of N repetitions of the last N characters of the string. You may assume that N is between 0 and the length of the string, inclusive.   • for input of "Hello", 3 → "llollollo" • for input of "Hello", 2 → "lolo" • for input of "Hello", 1 → "o"
A sequence is a list of numbers that are calculated based on a certain rule. For...
A sequence is a list of numbers that are calculated based on a certain rule. For instance, the progression described by the rule An = 2 ∗ n results in the numbers: 0 2 4 6 8 10 ··· 2 ∗ n. The sum of this sequence can be calculated as Sn = 0+2+4+6+8+10+···+2 ∗ n. Write a function that takes as input the number n and calculates the sum of the sequence up to the nth term (inclusive) for...
Problem 4 (5 marks). Hibbard’s gap sequence for Shellsort is defined as follows. 2 k −...
Problem 4 . Hibbard’s gap sequence for Shellsort is defined as follows. 2 k − 1, 2 k−1 − 1, 2 k−2 − 1, . . . , 7, 3, 1 where k is the largest number satisfying 2k − 1 < N. (b) Implement Shellsort with Hibbard’s gap sequence, with Shell’s gap sequence, and with a gap sequence of your own invention, and compare them on three input/output examples for duplicate-free arrays of size 10, 100, and 1000 (one...
Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A):...
Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A): A is a list of n integers 1 for i = 1 to n-1 do 2   x=aix=ai 3 j = i − 1 4 while (j ≥≥ 0) do 5 if x≥ajx≥aj then 6 break 7 end if 8   aj+1=ajaj+1=aj 9 j = j − 1 a end while b   aj+1=xaj+1=x c end for d return A The complexity of this algorithm is O(n2)O(n2)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT