简体   繁体   English

结构中的int数组

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

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. 我认为我掌握了一个概念,但是实现准则当然如下,这使我感到困惑。

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;

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} 如您所见,斐波那契数字将不是简单的“ int”,而是将它们存储在int数组中的数字,因此例如int-134528将像int someNumber [6] = {1,3, 4,5,2,8}

I have created the memo like so: 我创建了备忘录,如下所示:

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;
}

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) 我像这样实现了我的斐波那契函数: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];

}

I am completely lost as how to get the fibonacci numbers into their respective array slots and then push them to the HugeAdd function. 我完全不知道如何将斐波那契数放到各自的数组插槽中,然后将其推入HugeAdd函数。 The functions I have outlined cannot be changed either I can only create auxiliary function to assist. 我概述的功能无法更改,或者我只能创建辅助功能来提供帮助。

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. 我只想了解,我明白我必须像其他所有人一样独自深入编程地狱的深处,我只想要一点指导,这样我就可以一次停止6个小时的垃圾编码,以至于一无所获。

As I noted in a comment, you should try to debug the program with a stub implementation of HugeInteger first. 正如我在评论中指出的那样,您应该首先尝试使用HugeInteger的存根实现调试程序。 Once you are convinced the logic of your fibonacci routine is working, then you can try integrating the true HugeInteger implementation. 一旦确定斐波那契例程的逻辑正在运行,就可以尝试集成真正的HugeInteger实现。 To get you started, here's a simple stub: 为了帮助您入门,这里有一个简单的存根:

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);
}

One other pointer. 另一个指针。 In your createMemo() routine, you don't allocate your F array properly: 在您的createMemo()例程中,您没有正确分配F数组:

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

The F member of Memo is a HugeInteger * , so, you want to allocate sizeof(HugeInteger) * INIT_MEMO_SIZE instead. MemoF成员是HugeInteger * ,因此,您想改为分配sizeof(HugeInteger) * INIT_MEMO_SIZE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM