Help with dynamic array in a class

Heres what im having trouble doing.

Modify your function to add a given element to the array at the next available location by including the following feature:
If the array has reached its capacity, the add function should call a helper function to double the size of the array. The helper function will dynamically allocate storage for an array of twice the size, copy all the elements in the original array and update all the data members. The original pointer to the array should point to this new storage area after the copy is complete. The add function will then add the new element to the array.

what i have doesnt work. say i make an array of 10 and try to add 1 more by calling the add function my output is like:
1
2
3
4
5
6
7
8
9
10
11
5
4
3
2
4
2
4
3
5
6
1.456fd245


my helper function is to the bottom.

Heres my .h file

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef _Data_BOX
#define _Data_BOX

// Declaration for the class PlainBox
template<class itemtype>
class Data
{
  private:
// Data field
  int capacity;
  int num_used;
  itemtype *vStore;
  public:
	  Data (int Default_Size=10);
	  ~Data(){delete [] vStore;};
	  bool isFull(int count);
	bool isEmpty(int count1);
	int arrayCapacity();
	int numUsed(int count2);
	void setItem (const itemtype& Entry,int count4);
	itemtype getItem (int index);
	int ADD(const itemtype& Entry, int count3);
	int PosOfElem(const itemtype& Entry);
	int SetPosOfElem(int pos,int number,int count2);
	int Helper(const itemtype& Entry,int Default_Size,int capacity);
};


	  template<class itemtype>
	  Data<itemtype>::Data(int Default_Size){
		  capacity=10;
		  num_used=0;
		  vStore= new itemtype[Default_Size];
	  }
	  template<class itemtype>
	  bool Data<itemtype>::isFull(int count){
		  if(count==capacity)
			cout<<"Array is Full"<<endl;
		else
			cout<<"Array is not full"<<endl;
		return 0;
	   }
	  template<class itemtype>
	  bool Data<itemtype>::isEmpty(int count1){
		  if(count1==0)
			cout<<"Array is empty"<<endl;
		else
			cout<<"Array is not empty"<<endl;
		return 0;
	  }
	  template<class itemtype>
	  int Data<itemtype>::arrayCapacity(){return capacity;}
	  
	  template<class itemtype>
	  int Data<itemtype>::numUsed(int count2){return count2;}
	  
	  template<class itemtype>
	  void Data<itemtype>::setItem(const itemtype& Entry,int count4){
		  if(count4<=10){
		  vStore[num_used]=Entry;
		  num_used++;}
		  else
			  cout<<"Error, out of bounds."<<endl;
		  
	  }
	  template<class itemtype>
	  itemtype Data<itemtype>::getItem(int index){
		  return vStore[index];
	  }
	  template<class itemtype>
	  int Data<itemtype>::ADD(const itemtype& Entry,int count3){
		  if(count3<=10){
		  vStore[num_used]=Entry;
		  num_used++;}
		  else
			  Helper(Entry,20,capacity);
		  return 0;
	  }
	  template<class itemtype>
	  int Data<itemtype>::PosOfElem(const itemtype& Entry){
		  for(int i=0;i<capacity;i++){
			if(vStore[i]==Entry)
			   return i+1;
			}
			cout<<"Error"<<endl;
			return -1;
	  }
	  template<class itemtype>
	  int Data<itemtype>::SetPosOfElem(int pos,int number, int count2){
		  if(pos>count2)
			cout<<"Error"<<endl;
		  else
			vStore[pos-1]=number;
			return 0;
	  }
	  
	   template<class itemtype>
	  int Data<itemtype>::Helper(const itemtype& Entry,int Default_Size, int capacity){
		  capacity=20;
		  int count=0;
		  double *vStore2;
		  vStore2= new itemtype[Default_Size];
		  delete [] vStore2;
		  for(int i=0;i<10;i++){
			  vStore2[i]=vStore[i];
			  count++;
		  }
		  if(count>capacity){
			cout<<"Out of bounds"<<endl;
		  
		  }
		  else
			  vStore2[count]=Entry;
			  num_used++;
			  
		  
		  return 0;
	  }
	

	   
#endif 

Last edited on
bump
In your ADD(...) function: Remove the parameter count3 (and probably all count... parameter).

You need to check whether

(num_used+1) >= capacity

if so call Helper() without parameter. Change the code:
1
2
		  double *vStore2 = vStore;
		  vStore= new itemtype[capacity * 2];


Copy vStore2[i]=vStore2[i]; and after the copy: delete [] vStore2; and capacity *= 2;.

You don't have magic numbers like 10 or 20. Use capacity instead.

That's it for Helper().

In ADD(...) lines 73/74 are always executed.
Topic archived. No new replies allowed.