Results 1 to 11 of 11
Like Tree4Likes
  • 1 Post By Norm
  • 2 Post By KevinWorkman
  • 1 Post By Hibernate

Thread: Simple question...

  1. #1
    Onyx is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default 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 )

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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.
    JeffGrigg likes this.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    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.
    Norm and JeffGrigg like this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Ööö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)).
    Last edited by Hibernate; 08-10-2011 at 10:28 PM.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  5. #5
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    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!
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  6. #6
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by Onyx View Post
    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.
    Last edited by Hibernate; 08-10-2011 at 10:56 PM.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  7. #7
    Onyx is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    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.

  8. #8
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by Onyx View Post
    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 View Post
    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 View Post
    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!»]…
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  9. #9
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    And yes, that was the Fibonacci numbers.

    Fⁿ = Fⁿ⁻¹ + Fⁿ⁻²; F¹ = F² = 1
    JeffGrigg likes this.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  10. #10
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    But if you need another explanation:

    Java Code:
    [1] = 1            [2] = 1
            \         /       \
             \       /         \
        [3] = [1]+[2] = 2       \
               |         \       \
               |          \      |
               |     [4] = [2]+[3] = 3
               |      |
               |      /
               |     /
        [5] = [3]+[4] = 5
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  11. #11
    Onyx is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    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!

Similar Threads

  1. Simple Question
    By stackptr89 in forum New To Java
    Replies: 13
    Last Post: 01-29-2011, 06:35 PM
  2. very simple Question
    By arsenal4ever_11 in forum NetBeans
    Replies: 2
    Last Post: 05-27-2010, 09:51 PM
  3. some simple question?
    By jperson in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 06:32 PM
  4. Simple Question
    By barusk in forum Networking
    Replies: 13
    Last Post: 03-04-2009, 08:33 PM
  5. Probably a really simple question...
    By ibanez270dx in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 02:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •