The goal of this program is to create a "bag" array and a function that will calculate the Fibonacci sequence. The "bag" array should be initialized to a size of 2 where the the elements in order are 0 and 1 respectively.
For example: array[0] = 0 and array[1] = 1
There should be a function for when a number is added to the bag, the Fibonacci sequence is solved up to the position of the number that is added to the bag.
For example: If I wanted to add to the 6th element in the sequence, the array would look like this: {0 1 1 2 3 5}
The should also be a function for when a number is removed all the elements after it would be removed and the bag would be resized.
For example: If we had the above array and I wanted to remove all the elements after the 3rd term, the array would look like this: {0 1 1}
There should also be functions to check and see if an element is in the bag and to print the size of the current bag.
Here's my code so far:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int num;
}bag_stuff;
static int count;
void init_count();
bag_stuff* init_bag(bag_stuff *bag);
void print_size(bag_stuff *bag);
void is_in(bag_stuff elem,bag_stuff *bag);
void print_bag(bag_stuff *bag);
bag_stuff* add_fib(bag_stuff elem,bag_stuff *bag);
bag_stuff* remove_fib(bag_stuff elem,bag_stuff *bag);
int main()
{
bag_stuff *bag = NULL;
init_count();
bag_stuff elem;
init_bag(bag);
print_size(bag);
print_bag(bag);
elem.num = 1;
is_in(elem,bag);
elem.num = 12;
add_fib(elem,bag);
print_size(bag);
elem.num = 6;
remove_fib(elem,bag);
print_size(bag);
return 0;
}
void init_count()
{
count = 2;
}
bag_stuff* init_bag(bag_stuff *bag)
{
int i;
bag = malloc(sizeof(bag_stuff)*count);
for(i = 0;i < count;i++)
{
bag[i].num = i;
printf("|%d|",bag[i].num);
}
printf("\n");
return bag;
}
void print_size(bag_stuff *bag)
{
int x = (sizeof(bag_stuff)*count)/sizeof(bag[0].num);
printf("The size of the bag is %d\n",x);
}
void is_in(bag_stuff elem,bag_stuff *bag)
{
int i;
for(i = 0;i < count;i++)
{
if(bag[i].num == elem.num)
{
printf("The element %d is in the bag.\n",bag[i].num);
}
}
}
void print_bag(bag_stuff *bag)
{
int i;
for(i = 0;i < count;i++)
{
printf("|%d|",bag[i].num);
}
printf("\n");
}
bag_stuff* add_fib(bag_stuff elem,bag_stuff *bag)
{
int i;
init_bag(bag);
bag_stuff *temp = bag;
for(i = 0;i < 2;i++)
{
bag[i].num = temp[i].num;
}
count = count+(elem.num-2);
bag = malloc(sizeof(bag_stuff)*count);
for(i = 0;i < 2;i++)
{
temp[i].num = bag[i].num;
}
for(i = 0;i < count-2;i++)
{
bag[i+2].num = bag[i].num+bag[i+1].num;
}
for(i = 0;i < count;i++)
{
printf("|%d|",bag[i].num);
}
printf("\n");
free(temp);
return bag;
}
bag_stuff* remove_fib(bag_stuff elem,bag_stuff *bag)
{
count = count-elem.num;
bag = malloc(sizeof(bag_stuff)*count);
int i;
for(i = 0;i < count-2;i++)
{
bag[0].num = 0;
bag[1].num = 1;
bag[i+2].num = bag[i].num+bag[i+1].num;
}
for(i = 0;i < count;i++)
{
printf("|%d|",bag[i].num);
}
printf("\n");
return bag;
}
I keep getting segmentation faults. Can someone explain why?