Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The goal of this program is to be able to add and remove elements from a bag cla

ID: 3619474 • Letter: T

Question

The goal of this program is to be able to add and remove elements from a bag class.
Array = 5, 10, 15
Goal: 5, 15Here's my code:

#include
#include

typedef struct
{
int num;

}bag_stuff;

static int count;

void init_count();
bag_stuff* add_elem(bag_stuff elem,bag_stuff *bag);
void print_bag(bag_stuff *bag);
int is_in(bag_stuff elem,bag_stuff *bag);
bag_stuff* remove_one(bag_stuff elem,bag_stuff *bag);
void clear(bag_stuff *bag);
int empty(bag_stuff *bag);

int main()
{
bag_stuff *bag = NULL;
init_count();

bag_stuff elem;

bag = remove_one(elem,bag);
print_bag(bag);

elem.num = 5;
bag = add_elem(elem,bag);
print_bag(bag);

elem.num = 10;
bag = add_elem(elem,bag);
print_bag(bag);

elem.num = 15;
bag = add_elem(elem,bag);
print_bag(bag);

elem.num = 10;
bag = remove_one(elem,bag);
print_bag(bag);
empty(bag);


return 0;
}

void init_count()
{
count = 0;
}

bag_stuff* add_elem(bag_stuff elem,bag_stuff *bag)
{
count++;
if(count == 1)
  {
   bag = malloc(sizeof(bag_stuff)*count);
   bag[count-1].num = elem.num;
  }
else
  {
   bag_stuff *temp = bag;
   bag = malloc(sizeof(bag_stuff)*count);
   bag[count-1].num = elem.num;
   int i;
   for(i = 0;i < count-1;i++)
   {
   bag[i].num = temp[i].num;
   }
   free(temp);
  }
  return bag;
}

void print_bag(bag_stuff *bag)
{
int i;
for(i = 0;i < count;i++)
  {
   printf("|%d|",bag[i].num);
  }
  printf(" ");
}

int is_in(bag_stuff elem,bag_stuff *bag)
{
int i;
for(i = 0;i < count;i++)
  {
   if(bag[i].num == elem.num)
   {
   return 1;
   }
  }
  return 0;
}

/*This is the part I'm having trouble with
bag_stuff* remove_one(bag_stuff elem,bag_stuff *bag)
{
int i;
if(empty(bag) == 1)
   {
   if(is_in(elem,bag) == 1)
   {
   count--;
   bag_stuff *temp = bag;
   bag = malloc(sizeof(bag_stuff)*count);
   bag[count-1].num = elem.num;
   for(i = 0; i < count-1;i++)
   {
   bag[i].num = temp[i].num;
   bag[i+1].num = temp[i+2].num;
   }
   free(temp);
   }
   else
   {
   printf("The element %d is not in the bag. ",elem.num);
   exit(0);
   }
   }
  else
   {
   printf("The bag is empty. Nothing can be removed. ");
   }
  return bag;
}*/

void clear(bag_stuff *bag)
{
count = 0;
free(bag);
}

int empty(bag_stuff *bag)
{
if(count == 0)
  {
   return 0;
  }
else
  {
   return 1;
  }
}
I would like to be able to do this with more elements than just the three that I added to the bag. In other words, I would like for the remove_one function to be universal if you know what I mean. Please help! Thanks! I would like to be able to do this with more elements than just the three that I added to the bag. In other words, I would like for the remove_one function to be universal if you know what I mean.

Explanation / Answer

please rate - thanks this what you're looking for?
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote