Understanding the Function Foo in Simple Pseudocode with Inputs and Outputs

  • Thread starter Thread starter mohabitar
  • Start date Start date
AI Thread Summary
The function Foo takes two integers and an array, returning the sum of specific elements based on the input parameters. It checks conditions to determine if it should return 0 or proceed with recursion. For inputs k = 2 and m = 5 with the array [5, 6, 2, 3, 4, 8, 2], it first returns the value at index 2, which is 6, and then recursively calls itself with updated parameters. The recursion continues until the base case is met, ultimately summing the values from the specified range of the array. Understanding the recursive nature of Foo is crucial to grasping its output and the general formula it computes.
mohabitar
Messages
140
Reaction score
0
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].

Code:
1 Foo(k,m, a[1],...,a[n]) 
2   if (k < 1 or m > n or k > m) return 0 
3   elsereturn 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: So 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. So line 3 says: return a[k] which is a[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])

So is what I have done correct up there? If so, how would I know is a[n] is? Am I supposed to be finding that? What would be the final result of all this?
 
Physics news on Phys.org
mohabitar said:
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].

Code:
1 Foo(k,m, a[1],...,a[n]) 
2   if (k < 1 or m > n or k > m) return 0 
3   elsereturn 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: So 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.
The function doesn't display anything, but control passes to line 3.
mohabitar said:
So line 3 says: return a[k] which is a[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])
Not quite. The function returns 6 + Foo(3, 5, a[1], a[2], ..., a[n]), or IOW, 6 + Foo(3, 5, 5, 6, 2, 3, 4, 8, 2).

Now, what does that evaluate to?
mohabitar said:
So is what I have done correct up there? If so, how would I know is a[n] is? Am I supposed to be finding that? What would be the final result of all this?
 
Ahh ok I'm still confused. So this is a recursive function. The last line tells me to take 6 and add it to Foo(...), so doesn't Foo(...) just display line 3, which is 6+Foo(...). So I don't get it, do I keep adding and adding, when does it ever stop? Or am I looking at this the wrong way?
 
mohabitar said:
Ahh ok I'm still confused. So this is a recursive function. The last line tells me to take 6 and add it to Foo(...), so doesn't Foo(...) just display line 3, which is 6+Foo(...). So I don't get it, do I keep adding and adding, when does it ever stop? Or am I looking at this the wrong way?

Foo() does NOT display anything, it returns a value that involves a recursive call to Foo again.

In the first call to Foo(), it returns 6 + Foo(3, 5, 5, 6, 2, 3, 4, 8, 2). Please answer the question I asked in my last post: What's the value of this expression?
 
So it would start with Foo(2,5,a)=6+Foo(3,5,a)
Foo(3,5,a)=2+Foo(4,5,a)
and so on until this whole thing returns o, correct? So the final output is zero?

But now what about the 2nd part: Using summation notation, give a general formula for what Foo computes.

I don't even know where to start with this one.
 
Foo(2,5,a)
= 6+Foo(3,5,a)
= 6 + 2 + Foo(4, 5, a)
= ?

I'll take your word that the 3rd line above is correct. The "whole thing" won't return 0. Eventually, Foo(x, y, a) will return 0, but you need to add all the other values that have been returned, as in what I show above.

Don't worry about the 2nd part until you understand what Foo is doing.

For the third time, Foo does not display anything, and there is no output. The only thing Foo does is calculate some number by a recursive algorithm.
 

Similar threads

Replies
21
Views
3K
Replies
2
Views
2K
Replies
15
Views
5K
Replies
4
Views
5K
Replies
10
Views
2K
Replies
1
Views
341
Replies
8
Views
2K
Back
Top