Note that there are some explanatory texts on larger screens.

plurals
  1. POint Array in a Array of Structs in a Struct
    primarykey
    data
    text
    <p>I hope I don't get hammered on this site. I am a novice programmer having much difficulty with this concept. I have been tasked with memoizing the fibonacci sequence. A concept which I think I have a descent grasp on but of course the implementation guidelines are as follows which is what is confusing me.</p> <pre><code>typedef struct Memo { // dynamically allocated HugeInteger array to store our Fibonacci numbers struct HugeInteger *F; // the current length (i.e., capacity) of this array int length; } Memo; typedef struct HugeInteger { // a dynamically allocated array to hold the digits of a huge integer int *digits; // the length of the array (i.e., number of digits in the huge integer) int length; } HugeInteger; </code></pre> <p>Already as you can see the fibonacci numbers will not be simple "int" but rather they will be numbers stored in an int array so for instance int - 134528 will be in an array like so int someNumber[6] = {1,3,4,5,2,8}</p> <p>I have created the memo like so:</p> <pre><code>Memo *createMemo(void) { /* Description: Create and initialize a Memo struct: 1. Dynamically allocate space for a new Memo struct. 2. Within the Memo struct, dynamically allocate an array of INIT_MEMO_SIZE number of HugeInteger structs. INIT_MEMO_SIZE is defined in Fibonacci.h. Note: This is an array of actual structs, not pointers to structs. So, you cannot set the individual elements of this array to NULL, and you cannot free() them. 3. Once the array is created, initialize memo→length appropriately. 4. Make two calls to HugeInit() to initialize F[0] and F[1] (your two Fibonacci base cases) within the Memo struct. 5. For all remaining F[i], initialize the digits field to NULL and the length field to 0 to indicate that F[i] has not yet been memoized. Panic: If any calls to malloc() fail within this function, call panic() (defined in HugeInteger.c) with the following string argument: “ERROR: out of memory in createMemo()\n”. Returns: A pointer to the new Memo struct. */ int i; //Dynamically allocate new Memo Memo *newMemo = malloc(sizeof(Memo)); //Check if malloc failed if(newMemo == NULL) panic("ERROR: out of memory in createMemo()\n"); //Malloc a newMemo-&gt;F struct newMemo-&gt;F = malloc(sizeof(HugeInteger *) * INIT_MEMO_SIZE); //Check if malloc failed if(newMemo-&gt;F == NULL) panic("ERROR: out of memory in createMemo()\n"); //Initialize the length of the Memo newMemo-&gt;length = INIT_MEMO_SIZE; //Make two calls to HugeInit to initialize F[0] and F[1] base cases HugeInit(&amp;newMemo-&gt;F[0], 0); HugeInit(&amp;newMemo-&gt;F[1], 1); //Initialize the rest of F[i] to NULL &amp; length to 0 so I now it's not memoized for(i=2; i&lt;INIT_MEMO_SIZE; i++) { newMemo-&gt;F[i].digits = NULL; newMemo-&gt;F[i].length = 0; } //Return the newly created Memo return newMemo; } </code></pre> <p>After I create the new Memo I now enter the fibonacci function and this is where my brain fails on me. I implemented my fibonacci function like so: struct HugeInteger *fib(int n, Memo *memo)</p> <pre><code>{ /* Description: Implement a recursive solution that checks whether F(n) has already been memoized and, if so, returns F(n) without making any recursive calls. 1. If n exceeds the bounds of the array in memo, call expandMemo(). For more information on what parameters to pass to expandMemo(), refer to its function description above. 3. In order to compute and memoize F(n), you must call the HugeAdd() function that is defined in HugeInteger.c. The function requires F(n - 1) and F(n - 2) to be memoized already. HugeAdd() will memoize F(n) if you call it correctly (i.e., it will store the result in memo). Returns: The address of the struct within memo that holds F(n). If memo is NULL or n is less than 0, return NULL without performing any recursive calls and without calling expandMemo(). */ int i; //Handle for negative numbers if(n &lt; 0 || memo == NULL) return NULL; //Handle for index out of bounds if(n &gt; memo-&gt;length) expandMemo(memo, n); //Check Memory if F[n] already exists and return it if(memo-&gt;F[n].digits != NULL) return &amp;memo-&gt;F[n]; //return the base cases; if(n &lt; 2) return &amp;memo-&gt;F[n]; else { //My thought process was to make two recursive calls and then use the provided HugeAdd function to add the information together. HOWEVER the HugeAdd function is a void function so it does it's thing and returns nothing. basically it takes the two arrays and adds them index by index like you would on paper. The first argument of Huge add is the first HugeInteger to be added, then the second argument is the second HugeInteger to be added, and the third argument is the HugeInteger where you want the result. second problem I see is that temp1 is never being malloc'd or initialized so as the fibonacci series grows temp1 will need to be malloc'd accordingly and then it will need to be filled accordingly. I am lost completely as to how to implement this. HugeInteger *temp1 = fib(n-1, memo); HugeInteger *temp2 = fib(n-2, memo); HugeAdd(&amp;temp1, &amp;temp2, &amp;memo-&gt;F[n]); } return &amp;memo-&gt;F[n]; } </code></pre> <p>I am completely lost as how to get the fibonacci numbers into their respective array slots and then push them to the HugeAdd function. The functions I have outlined cannot be changed either I can only create auxiliary function to assist. </p> <p>I SIMPLY want insight, I understand I must trek through the depths of programming hell alone just like everyone else, I just want a little guidance so I can stop coding garbage for 6 hours at a time only to get absolutely nowhere. </p>
    singulars
    1. This table or related slice is empty.
    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.
    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