CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 22

Threaded View

  1. #1
    Join Date
    Sep 2007
    Posts
    55

    Professor Wrong? Program assignment?

    Ok So I have been given a program assignment over the weekend,
    http://www.cs.uky.edu/~jurek/cs315/cs315s08/pa/pa1.pdf
    doesn't look too bad, just getting running times and such.

    he gives us a formula
    a(n) = a(n - 2) + a(n - 3))

    hmmm sound almost like the fibonnaci number formula, which i happen to have a small program of.
    Code:
    #include <iostream>
    using namespace std;
    long fib(int n);
    
    
    
    int main()
    
    {
    int n;
    cin >> n;
    while(n<20)
    {
    cout << fib(n);
    cout << " ";
    n++;
    }
    
    
    return 0;
    }
    
    
    long fib(int n) {
        if (n <= 1) {
            return n;
        } else {
            return fib(n-1)+fib(n-2);
        }
    }
    now I checked wiki, and this DOES correctly output the first 20 numbers of the fibonnaci numbers.

    so I figure hey ill just change the n-1 to n-2, and the n-2 to n-3....right?
    should work, i mean their exactly the same.
    BUT, when I do that i get WAY different numbers than what he's getting, when you look at the program assignment he's getting like 3, 0, 2, 3, 2, 5, 5, 7, 10, 12, 17, 22 in his sequence, and when i change it.....well thats not what i get AT all.
    so my question is.......is he wrong, or is changing the n-1 to n-2, etc just not gonna be as simple as that.

    cuz...the formulas look EXACTLY the same, besides the 2 numbers??

    and he said it was recursive....and ^^ that is a recusive function correct?

    EDIT: also im supposed to do a memoization....version of this, but Im not sure I understand how to do that, our professor never really....touched on memoization. i mean I understand the concept but..... i wouldnt know how to implement it?

    thanks all!
    Last edited by MercenaryFH; February 16th, 2008 at 03:47 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured