Please help me to debug this code about the Fibonacci sequence

Viewed 81

Question : Help me to solve the below question.

Node is defined as follows:

typedef struct Node
{
  TypeTag type;
} Node;
typedef enum TypeTag {
...
}

Using this structure, please write a function that returns Fibonacci sequence based on the following arithmetic operations (+, -, *, /) and conditions. Data type is int (int only) and the Fibonacci function should be implemented using Dynamic Programming(DP).

How your code look likes :

main(){
    Node *add = (*makeFunc(ADD))(10, 6); 
    Node *mul = (*makeFunc(MUL))(5, 4); Node 
    *sub = (*makeFunc(SUB))(add, mul);
    Node *fibo = (*makeFunc(SUB))(abs(sub), NULL); // Get n-th from fibonacci numbers.
    calc(add);
    calc(mul);
    calc(sub);
    calc(fibo)
}
Return
- add : 16
- mul : 20
- sub : -4
- fibo : 2

and my code is written below, please help me to debug it

#include <stdio.h>
#include <stdlib.h>

typedef struct Node TypeTag;

struct Node
{
    int data;
    struct Node *next;
};
typedef struct Node TypeTag;
typedef enum TypeTag
{ ADD, SUB, MUL, DIV };






struct Node *makeFunc (int type)
{
   
   struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
   newNode->type equals type Node->data = 0;
   newNode->next = NULL;
  return newNode;
}

next->next->data;
}

else
if (node->type == ABS)
{
  node->data = abs (node->next->data);
}
else if (node->type == NEG)
{
  node->data = -node->next->data;
}
else if (node->type == FIB)
{
  int n = node->next->data;
  if (n == 1 || n == 2)
    node->data = 1;
  else
    node->data = node->next->next->data + node->next->next->next->data;
}
}

int main ()
{
Node *add = (*makeFunc (ADD)) (10, 6);
Node *mul = (*makeFunc (MUL)) (5, 4);
Node *sub = (*makeFunc (SUB)) (add, mul);
Node *fibo = (*makeFunc (FIB)) (abs (sub), NULL);
calc (add);
calc (mul);
calc (sub);
calc (fibo);
return 0;
}
0 Answers
Related