Understanding the Function Foo in Simple Pseudocode with Inputs and Outputs

  • Thread starter mohabitar
  • Start date
In summary, Foo is a recursive function that takes two integers and an array of integers as inputs. It returns a value that involves a recursive call to itself. The function will keep calling itself until certain conditions are met, and then it will return a final value. The value returned by Foo can be represented using summation notation, but the specific formula will depend on the inputs given to the function.
  • #1
mohabitar
140
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
  • #2
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?
 
  • #3
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?
 
  • #4
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?
 
  • #5
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.
 
  • #6
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.
 

1. What is pseudocode?

Pseudocode is a simple and informal way of writing code that helps programmers plan and organize their thoughts before writing actual code. It uses common language and resembles a mix of code and English language, making it easier to understand and modify.

2. Why is pseudocode important?

Pseudocode is important because it allows programmers to plan and visualize the logic of their code before actually writing it. It helps identify any potential issues or errors in the logic early on, saving time and effort in the coding process.

3. How is pseudocode different from actual code?

Pseudocode is not an actual programming language, and therefore cannot be executed by a computer. It is simply a tool for planning and outlining the logic of a program. Actual code, on the other hand, is written using a specific programming language and is meant to be executed by a computer.

4. Can pseudocode be used in any programming language?

Yes, pseudocode can be used in any programming language as it is not tied to any specific syntax. It is a general outline of the logic and can be translated into any programming language of choice.

5. Is it necessary to use pseudocode?

While it is not necessary to use pseudocode, it can be a useful tool for planning and organizing code, especially for larger and more complex programs. It can also be helpful for collaboration between multiple programmers as it provides a clear and simple overview of the program's logic.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
839
  • Engineering and Comp Sci Homework Help
Replies
15
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
20
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top