I'm new to psuedocode, and I'm having trouble putting all the pieces together:
Here is the definition of a function named foo whose inputs are two integers and an array of integers a[1] ... a[n]
.
1 Foo(k,m, a[1],...,a[n])
2 if (k < 1 or m > n or k > m) return 0
3 else return a[k] + Foo(k+1,m,a[1],...,a[n])
Suppose that the input integers are k=2
and m=5
and the input array contains [5, 6, 2, 3, 4, 8, 2]
. What value does Foo return? Using summation notation, give a general formula for what Foo computes.
This one is making my head hurt. Here's what I did so far:
Line 2 has three conditional statements:
- If k<1 // if 2<1..this is false
- If m>n // if 5 is greater than the amount of values in the array, which is 7, so this is false
- If k>m // if 2>5, this is false
So this function will display line 3. Line 3 says:
- return
a[k]
which isa[2]
which is the second value of the array, which is 6. So take 6 and add it to(2+1, 5, a[1].....,a[n])
Is what I have done correct up there? If so, how would I know what a[n]
is? Am I supposed to be finding that? What would be the final result of all this?