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?

where is the seg fault happening? Can you step into your code in a debugger or instrument the code (with printf calls) to see where it fails?

It initially seg faults when I call the print_bag function.

Then, I suspect that either the incoming bag is null or it has fewer than count items in it. Between line 86 and 87 insert: `if(! bag[i]) { printf("index: %d is null\n",i);}`

Then, I suspect that either the incoming bag is null or it has fewer than count items in it. Between line 86 and 87 insert: `if(! bag[i]) { printf("index: %d is null\n",i);}`

When I entered that in, it didn't work. I got a "unary exception" error or something of that nature. Then I saw it needed to be if(! bag.num) { printf("index: %d is null\n",i);
then the code seg faulted again.

Then, I suspect that either the incoming bag is null or it has fewer than count items in it. Between line 86 and 87 insert: `if(! bag[i]) { printf("index: %d is null\n",i);}` I usually write for loops as `for(int i = start; i < max; ++i){...}` unless the index is needed outside the loop. This has three good effects:

1. The index is local to the for-loop block: Locality is good
2. The code travels through the half-open interval "[start,max)" which is a good habit to have: Helps avoid fence-post problems.
3. The index is preincremented rather than postincremented: Also a good habit to have, though it usually only matters in object oriented languages where iterators might be complicated objects rather than being (in the end) an int in a register.

Item 1 matters. Don't declare the index outside the loop without good reason.
Item 2 matters a little. Good habits make for fast, efficient coding.
Item 3 has proven to matter in some production code in Java and C++, but often does not. It is harmless at worst, sometimes helpful. You should get the habit as soon as possible because otherwise, when you try to use it, it 'just looks weird'. (After a dozen or so years of doing it my way, i++ looks a little weird to me: Pre-increment is almost always what you wanted

Then, I suspect that either the incoming bag is null or it has fewer than count items in it. Between line 86 and 87 insert: `if(! bag[i]) { printf("index: %d is null\n",i);}` I usually write for loops as `for(int i = start; i < max; ++i){...}` unless the index is needed outside the loop. This has three good effects:

1. The index is local to the for-loop block: Locality is good
2. The code travels through the half-open interval "[start,max)" which is a good habit to have: Helps avoid fence-post problems.
3. The index is preincremented rather than postincremented: Also a good habit to have, though it usually only matters in object oriented languages where iterators might be complicated objects rather than being (in the end) an int in a register.

Item 1 matters. Don't declare the index outside the loop without good reason.
Item 2 matters a little. Good habits make for fast, efficient coding.
Item 3 has proven to matter in some production code in Java and C++, but often does not. It is harmless at worst, sometimes helpful. You should get the habit as soon as possible because otherwise, when you try to use it, it 'just looks weird'. (After a dozen or so years of doing it my way, i++ looks a little weird to me: Pre-increment is almost always what you wanted

In C, you have to intialize the index outside the loop. C++ you don't have to.
In other words, for(int i = start; i < max; ++i) would be incorrect in C.
I'm not sure what the other two have to do with the code seg faulting though.
I just know the max is supposed to be correct.
Count has to be initalized first outside so I can create the "bag" array and fill in the two items of 0 and 1. Count is initialized to 2. When I print the bag out at the stage that it is in the main function, it should print out the only 2 items in the bag being the 0 and 1.

Hmm. I guess I have been using a non-ANSI compiler then. Sorry for the error. The other two are comments about coding in general.

Yeah, I'm still not sure why my code is seg faulting though.

Never mind. I figured it out.
When I called my function in the main, I used bag but bag was still NULL.
I had to redefine bag pretty much.
Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.