Removing from a bag

Feb 22, 2014 at 6:47am
I'm learning to write a bag class and have written this code that adds elements into the existing bag. Problem is that once I return the bool type for the array, it doesn't print the array as I planned.

This is my header.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef _BAG_
#define _BAG_

typedef int bag_type;

class Bag
{
private:
	int count;
	bag_type data[5];

	//copy constructor
	Bag::Bag( const Bag& other )
	{
		count= other.count;
	}

public:
	Bag();
	bool insert(bag_type);
	void remove(bag_type); 
	void add(bag_type);
	int size();
	void clear();
	void print(bag_type);
};

#endif 



This is the (Edited) code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

#include <iostream>
#include <cstdlib>
#include "BagWR.h"

using namespace std;

Bag::Bag()    // Constructor
{
   // set the initial state of the bag
   count = 0;
}

void Bag::clear()  // Clear the bag
{
   count = 0;
}

void Bag::print(bag_type)
{
	
	for (int i = 0; i < count ; i++)
		{
			cout << data[i] << endl;
		}
}

bool Bag::insert(bag_type value) //Place a value in Bag
{
   bool reply;
   if(count < 5)
   {
      data[count] = value;
      reply = true;
      count++;
   } 
   else 
   {
      reply = false;
   }

   
   return reply;
}

void Bag :: add(bag_type value) 
{
data[count] = value;
count++;
}

void Bag :: remove(bag_type value) 
{
data[count] = value;
count--;
}

int Bag::size()
{
   Bag b;
   return count;
}

int main()
{ 
  bool t;
  Bag b;
  bag_type value;

  //t=b.insert(1);
  for (int i=1; i < 6; i++)
  {
     value=i;
	 t= b.insert(value);
	 //system("Pause");
	 //return 0;
  } 
  
  cout << b.size() << " elements in this bag" << endl;
	 Bag instance;
		b.print(5);
  b.add(6);
  cout << b.size() << " elements in this bag" << endl;
		b.print(6);
  b.add(8);
    cout << b.size() << " elements in this bag" << endl;
		b.print(6);

  system("Pause");
  return 0;
}
Last edited on Feb 23, 2014 at 7:14am
Feb 22, 2014 at 11:21am
You add elements to bag b but print the elements of bag instance.

I also don't understand why a number is passed to the print function when it's not used for anything within the function.

On line 82 you add an element to an already full bag. The add function doesn't check if the bag is already full so it will just write outside the data array.
Feb 22, 2014 at 9:11pm
Okay, I fixed that error. It acknowledges that there are five and later six elements of the bag, but I think what I'm trying to ask is how do I display the value of the five/six elements of the bag?
Feb 22, 2014 at 9:29pm
Please read these notes again:
Peter87 wrote:
You add elements to bag b but print the elements of bag instance.

I also don't understand why a number is passed to the print function when it's not used for anything within the function.
Feb 23, 2014 at 7:10am
Okay, so I fixed it and it does print the five elements. Is there any way I can remove elements from a bag? There are very few reference to bags on the site.

Feb 23, 2014 at 9:29am
Sure. Find the element you want to remove, then shift all elements in the array left one step (overwriting it) and decrement the size by 1.
Feb 23, 2014 at 9:55am
You can do as Zhuge says but as I understand it the order of the elements in a bag is not important. If that's so you could just copy the last element to the position that you want to remove and decrement the count variable.
Topic archived. No new replies allowed.