Note that there are some explanatory texts on larger screens.

plurals
  1. POFix recursive array
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/9441282/make-an-array-to-store-the-calculated-numbers-in-memory">Make an array to store the calculated numbers in memory</a> </p> </blockquote> <p>I have to modify some code to do some new things.</p> <p><strong>These are the new requirements:</strong></p> <p>-The program will need to use an array in which to store the calculated Fibonacci numbers in memory. (Which can be a globe variable)</p> <p>-The numbers array is also considered to be a partially-filled array and therefore I will need to declare a variable to keep track of the number of items which has been stored in the array.</p> <p>-The numbers array should be declared to have 46 positions.</p> <p>-The number 46 should be declared as a global constant named MAXFIB, and therefore the number 46 should not appear in my program more than once. </p> <p>-At the start of your program, the numbers array needs to be initialized with the "starting knowledge"(The Base cases)- the first two Fibonacci numbers are 1 and 1. Store these into the first two array positions.</p> <p>-Add an additional set of base cases so that if the "n" parameter value is too low or too high then the function will stop and return -1, indicating an error condition.</p> <p>-Add an additional base case so that if the Fibonacci number we are looking for is already stored in the array, then I simply need to retrieve it from the array and return it.</p> <p>-For the recursive case, it will still need to call on the function two times recursively as it did before, but after the number has been calculated, I will need to store it into the array before returning.</p> <p><strong>This is my code so far:</strong></p> <pre><code>#include &lt;iostream&gt; using namespace std; // Returns the nth number in the fibonacci sequence int fib(int n, int* memory); const int MAXFIB = 46; int main() { int memory[MAXFIB]; memory[0] = 1; memory[1] = 1; cout &lt;&lt; fib(46) &lt;&lt; endl; system("pause"); return 0; } int fib(int n, int* memory) { // Base cases if (n &lt; 1 || n &gt; MAXFIB) return -1; if (n == memory) return memory; // Recursive cases memory[n] = fib(n-1) + fib(n-2); return; } </code></pre> <p>So, my question is, what am I doing right and doing wrong? What do I need to correct and or finish?</p> <p>Thanks </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload