Understanding the Function Foo in Simple Pseudocode with Inputs and Outputs

  • Thread starter Thread starter mohabitar
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around understanding the function Foo defined in pseudocode, which takes two integers and an array of integers as inputs. Participants are exploring how the function operates, particularly focusing on its recursive nature and the values it computes based on given inputs.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants describe the function Foo and its parameters, noting the conditions under which it returns 0.
  • There is confusion regarding how the recursive calls work, with some participants questioning how many times the function adds values before stopping.
  • One participant attempts to break down the function's operation step-by-step, indicating that Foo(2,5,a) leads to 6 + Foo(3,5,a).
  • Another participant clarifies that Foo does not display values but returns a computed result through recursion.
  • Participants express uncertainty about how to derive a general formula for what Foo computes using summation notation.

Areas of Agreement / Disagreement

Participants generally agree that Foo is a recursive function, but there is significant confusion and disagreement about how the recursion unfolds and what the final output is. The discussion remains unresolved regarding the exact value returned by Foo and the formulation of a general expression.

Contextual Notes

Participants highlight the need to understand the recursive nature of the function and the conditions that lead to its termination. There are unresolved questions about the summation notation and how to express the output of the function mathematically.

Who May Find This Useful

Individuals interested in pseudocode, recursion, and mathematical reasoning in programming contexts may find this discussion valuable.

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 19 ·
Replies
19
Views
3K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
6K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K