Question

Algorithm MyAlgorithm (A,B) Input: Arrays A and B each storing n >= 1 integers. Output: What...

Algorithm MyAlgorithm (A,B) Input:

Arrays A and B each storing n >= 1 integers.

Output: What is the output? (Refer to part b below)

Start: count = 0

C = 10 for i = 0 to C do

{

sum = 0 for j = 0 to n-1 do {

sum = sum + A[0]

for k = 1 to j do

sum = sum + A[k]

}

if B[i] == sum then count = count + 1

}

return count

Document a hand-run on MyAlgorithm for input arrays A = [9 2 5 1] and B = [40 29 2 57] and show the final output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Initially the values of n is 4

Arrays A = [9 2 5 1] and B = [40 29 2 57]

count = 0 and C = 10

C = 10

for i = 0

sum = 0

for j = 0

sum = sum + 9 =0+9=9

for k = 1 to 0 do // condition fail

so sum is 9

for j = 1

sum = sum + 9 =9+9=18
for k = 1 to 1 do

k=1:

sum = sum + A[1] = 18 + 2 = 20

so sum is 20

for j = 2

sum = sum + 9 =20+9=29
for k = 1 to 2 do

k=1:

sum = sum + A[1] = 29 + 2 = 31

k=2:

sum = sum + A[2] = 31 + 5 = 36

so sum is 36

for j = 3

sum = sum + 9 =36+9=45
for k = 1 to 3 do

k=1:

sum = sum + A[1] = 45 + 2 = 47

k=2:

sum = sum + A[2] = 47 + 5 = 52

k=3:

sum = sum + A[3] = 52 + 1 = 53

so sum is 53

for j = 4 // condition fail

if 40 == 53 // condition fail

now i=1

Repeat the same process

Final comparisons as follows

if 29 == 53 // condition fail

if 2 == 53 // condition fail

if 57 == 53 // condition fail

Therefore the count is 0.

return 0

Add a comment
Know the answer?
Add Answer to:
Algorithm MyAlgorithm (A,B) Input: Arrays A and B each storing n >= 1 integers. Output: What...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT