簡體   English   中英

結構中的int數組

[英]int Array in a Array of Structs in a Struct

希望我不要在這個網站上受到打擊。 我是一個新手程序員,對此概念有很多困難。 我的任務是記住斐波那契數列。 我認為我掌握了一個概念,但是實現准則當然如下,這使我感到困惑。

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;

如您所見,斐波那契數字將不是簡單的“ int”,而是將它們存儲在int數組中的數字,因此例如int-134528將像int someNumber [6] = {1,3, 4,5,2,8}

我創建了備忘錄,如下所示:

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->F struct
newMemo->F = malloc(sizeof(HugeInteger *) * INIT_MEMO_SIZE);

        //Check if malloc failed
        if(newMemo->F == NULL)
            panic("ERROR: out of memory in createMemo()\n");

//Initialize the length of the Memo
newMemo->length = INIT_MEMO_SIZE;

//Make two calls to HugeInit to initialize F[0] and F[1] base cases
HugeInit(&newMemo->F[0], 0);
HugeInit(&newMemo->F[1], 1);

//Initialize the rest of F[i] to NULL & length to 0 so I now it's not memoized
for(i=2; i<INIT_MEMO_SIZE; i++)
    {
    newMemo->F[i].digits = NULL;
    newMemo->F[i].length = 0;
    }

//Return the newly created Memo
return newMemo;
}

創建新的備忘錄后,我現在進入斐波那契功能,這是我的大腦無法承受的地方。 我像這樣實現了我的斐波那契函數:struct HugeInteger * fib(int n,Memo * memo)

{
/*
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 < 0 || memo == NULL)
    return NULL;

//Handle for index out of bounds
if(n > memo->length)
    expandMemo(memo, n);

//Check Memory if F[n] already exists and return it
if(memo->F[n].digits != NULL)
    return &memo->F[n];

//return the base cases;
if(n < 2)
    return &memo->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(&temp1, &temp2, &memo->F[n]);
}

return &memo->F[n];

}

我完全不知道如何將斐波那契數放到各自的數組插槽中,然后將其推入HugeAdd函數。 我概述的功能無法更改,或者我只能創建輔助功能來提供幫助。

我只想了解,我明白我必須像其他所有人一樣獨自深入編程地獄的深處,我只想要一點指導,這樣我就可以一次停止6個小時的垃圾編碼,以至於一無所獲。

正如我在評論中指出的那樣,您應該首先嘗試使用HugeInteger的存根實現調試程序。 一旦確定斐波那契例程的邏輯正在運行,就可以嘗試集成真正的HugeInteger實現。 為了幫助您入門,這里有一個簡單的存根:

typedef struct HugeInteger {
    unsigned long long digits;
    int length;
} HugeInteger;

void HugeInit (HugeInteger *h, unsigned long long val) {
    h->digits = val;
    h->length = 1;
}

void HugeAdd (const HugeInteger *a, const HugeInteger *b, HugeInteger *c) {
    assert(a->length);
    assert(b->length);
    assert(!c->length);
    c->digits = a->digits + b->digits;
    c->length = 1;
}

void HugePrint (const HugeInteger *a) {
    assert(a->length);
    printf("%llu\n", a->digits);
}

另一個指針。 在您的createMemo()例程中,您沒有正確分配F數組:

//Malloc a newMemo->F struct
newMemo->F = malloc(sizeof(HugeInteger *) * INIT_MEMO_SIZE);

MemoF成員是HugeInteger * ,因此,您想改為分配sizeof(HugeInteger) * INIT_MEMO_SIZE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM