# Simple question...

Printable View

• 08-10-2011, 06:45 PM
Onyx
Simple question...
Hi Guys, can anyone help me with a past paper question i'm looking at for MSc Computing.. it's only worth 4 marks so i'm guessing it's a brief answer but I need to be sure of it incase it comes up again.

Given the following recursive function definition

<CODE>
public static int rec (int n) {
if (n <= 2) return 1;
else return rec(n-1) + rec(n-2);
}
</CODE>

What is the value of rec(5)? Explain your answer.

Is this related to fibonacci numbers?

Any help appreciated (and just incase anyone says they can't help with homework... the year is completed, there aren't any more assignments it's just revision and no masters homework would be this simple :P )
• 08-10-2011, 07:05 PM
Norm
Quote:

What is the value of rec(5)?
Have you executed the code and gotten an answer from it?
What was the result?

If you want to know what the code is doing, add some printlns to it to show how the variables are changing.
• 08-10-2011, 07:40 PM
KevinWorkman
In addition to what Norm said, have you traced through this with a piece of paper and a pencil? Keep track of what methods are called, what the parameters are, etc. Cross things out, draw lines, etc. That's how you'd answer this on a test, so it's a good skill to pick up.
• 08-10-2011, 09:03 PM
Hibernate
ÖööhhUhhmm!

I'm not sure want »past paper question i'm looking at for MSc Computing.. it's only worth 4 marks» (the underlined parts) means, but you are clearly asking us to do your homework.

And that part with »the year is completed, there aren't any more assignments» does ensure us that this is not so you can get a better grade than you already got (sv: plussning; do not know the english word), that it is not completion nor a summer class, neither that school has not started for you (just 2½ weeks left for me, and the school is full (really, the trains are more overloaded than usually)).
• 08-10-2011, 09:05 PM
Hibernate
And:

If you do not understand recursion read this line!

Not to be confused with:

If you do not understand recursion read this line again!

However:

If you do not understand iteration read this line again!
• 08-10-2011, 09:45 PM
Hibernate
Quote:

Originally Posted by Onyx
and no masters homework would be this simple

I have had simpler, examples (explain and translated):

What is your ID on the computer network maintained by the university's school/department (do not remember which) for computer science?
How large is the biggest file in your home folder (including subfolders) in that network? (command line supplied)
How many multiplications is needed for square matrix multiplication? (The naïve method is allowed)
Play a specific game and tell how many moves that can be done in the initial state.
Use any operation in C except for - (minus) and use only [0, 255] numbers, to return -1.
In C, reverse the order of the bytes in an integer.
In C, reverse the order of the bits in an integer.

The list goes on…

Edit:

Another, although in pure mathematic (real analysis), but this was on (both an final exam and) home exam:

Construct a set with exactly three cluster points.
• 08-10-2011, 10:08 PM
Onyx
You are clearly a crazy person.... and as for 'you are clearly asking us to do your homework'... this is 1 small question from 1 past paper of which I have 6 of dating back to 2005. I don't know how university works in your country you offensive little man but in my country we don't give in 'homework' assignments in the middle of August! The teaching year finished months ago!

To the others that actually said useful things... I get the answer 5 when I use an applet but it doesn't give me an explanation as to why. It isn't too important that I know this as the chances of the question coming up again are very slim but if an intelligent person on here manages to recognise the fact that this isn't even worthy of being a bloody 'homework' question and can give me a simple explanation that would be great.
• 08-10-2011, 10:20 PM
Hibernate
Quote:

Originally Posted by Onyx
You are clearly a crazy person.... and as for 'you are clearly asking us to do your homework'... this is 1 small question from 1 past paper of which I have 6 of dating back to 2005. I don't know how university works in your country you offensive little man but in my country we don't give in 'homework' assignments in the middle of August! The teaching year finished months ago!

No reason to be rude[ish], but yes, I'm crazy how else do you become a geek, and take advanced classes years before you are supposed to take them, &c.
Are you telling me that you cannot take classes over the summer [and maybe even get paid for it], in your country‽‽
Neither that you can complete them after the class has finished, or try to get a higher grade‽‽
But that you think the question is rudimentary, but that you cannot answer it?
I'm [truly] sorry, but I do not believe you.

Quote:

Originally Posted by Onyx
To the others that actually said useful things...

Didn't you see my last post, it explain how you should solve it!
(I got the impression that you do not understand recursion; if you do, say so)

Quote:

Originally Posted by Onyx
I get the answer 5 when I use an applet but it doesn't give me an explanation as to why. It isn't too important that I know this as the chances of the question coming up again are very slim but if an intelligent person on here manages to recognise the fact that this isn't even worthy of being a bloody 'homework' question and can give me a simple explanation that would be great.

Well, give you the explanation [The classic explanation of recursion: »If you do not understand recursion read this line!»]…
• 08-10-2011, 10:24 PM
Hibernate
And yes, that was the Fibonacci numbers.

Fⁿ = Fⁿ⁻¹ + Fⁿ⁻²; F¹ = F² = 1
• 08-10-2011, 10:31 PM
Hibernate
But if you need another explanation:

Code:

```[1] = 1            [2] = 1         \        /      \         \      /        \     [3] = [1]+[2] = 2      \           |        \      \           |          \      |           |    [4] = [2]+[3] = 3           |      |           |      /           |    /     [5] = [3]+[4] = 5```
• 08-10-2011, 10:44 PM
Onyx
I don't enjoy being called a liar when i'm not lying so that's why I took offence but thank you for explaining it all the same.

Yes I knew it was a recursive function but I couldn't get my head around the wording of the question. I was ill during the original programming exam in June so I am having my first attempt at a new paper on Wednesday and this question was one of the smaller starter questions in the June exam. I'm sorry that you don't believe me but if it matters that much that you think you've helped someone cheat a homework assignment I will take a photograph of the paper and send it to you!