Error passing dynamic array as arguments

I am trying to make quicksort and binary search and I get error when I am passing dynamic array to argument. It also says error during initialization of the dynamic array. I would appreciate if you can tell me what I am doing wrong. Thank you :)

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//.h file

#ifndef SortableArray_h
#define SortableArray_h

#include <iostream>
#include <string>

using namespace std;

template <class DataType>

class Array
{
  public:
  Array(int size);
  DataType& operator[](int index);
  void changeSize(int newSize);
  int length() const;
  string err() const;
  int lsearch(const DataType& parameter) const;
  void sort();
  int bsearch(const DataType& value) const;

  // for dynamic memory management
  Array(const Array&); // copy constructor
  ~Array(); // destructor
  Array& operator=(const Array<DataType>& a); // assignment operator

  private:
  DataType *elements; // dynamic array
  int capacity;
  DataType dummy;
  int errorCode;
  void quickSort (int t, int f);
  int bsearch2(Array <DataType> elements, const DataType & value, int t, int f);
};

template <class DataType>
void Array<DataType>::sort()
{
  int s = 0;
  int e = capacity - 1;

  if (s >= e)return; // done!

  int pivot = (s + e) / 2;
  int l = s; // index of left-most element
  int r = e; // index of right-most element

  while (true)
  {
    while(l < pivot)
      if (elements[pivot] < elements[l]) break; else ++l;

    while(pivot < r)
      if (elements[r] < elements[pivot]) break; else --r;
      if (l == r) // balanced
      {
        quickSort(s, pivot - 1);
        quickSort(pivot + 1, e);
        break; // done
      }
      else if (l == pivot) // swap w/pivot
      {
        DataType temp = elements[pivot];
        elements[pivot] = elements[r];
        elements[r] = temp;
        pivot = r;
        ++l;
      }
      else if (pivot == r) // swap with pivot
      {
        DataType temp = elements[pivot];
        elements[pivot] = elements[l];
        elements[l] = temp;
        pivot = l;
        --r;
      }
      else // swap around the pivot
      {
        DataType temp = elements[l];
        elements[l] = elements[r];
        elements[r] = temp;
        ++l;
        --r;
      }
    }
}

template <class DataType>
void Array<DataType>::quickSort(int t, int f)
{
  int s = t;
  int e = f;

  if (s >= e)return; // done!

  int pivot = (s + e) / 2;
  int l = s; // index of left-most element
  int r = e; // index of right-most element

  while (true)
  {
    while(l < pivot)
      if (elements[pivot] < elements[l]) break; else ++l;

    while(pivot < r)
      if (elements[r] < elements[pivot]) break; else --r;
      if (l == r) // balanced
      {
        quickSort(s, pivot - 1);
        quickSort(pivot + 1, e);
        break; // done
      }
      else if (l == pivot) // swap w/pivot
      {
        DataType temp = elements[pivot];
        elements[pivot] = elements[r];
        elements[r] = temp;
        pivot = r;
        ++l;
      }
      else if (pivot == r) // swap with pivot
      {
        DataType temp = elements[pivot];
        elements[pivot] = elements[l];
        elements[l] = temp;
        pivot = l;
        --r;
      }
      else // swap around the pivot
      {
        DataType temp = elements[l];
        elements[l] = elements[r];
        elements[r] = temp;
        ++l;
        --r;
      }
    }
}


template <class DataType>
Array<DataType>::Array(int size):capacity(size)
{
  errorCode = 0;

  if (capacity <= 0)
  {
    errorCode |= 1;
    elements = 0;
  }
  else
    elements = new DataType[capacity];
}

template <class DataType>
DataType& Array<DataType>::operator[](int index)
{
  if (index < 0 || index >= capacity)
  {
      errorCode |= 2; // invalid index
      return dummy;
  }
  errorCode &= (7 - 2); // unset
  return elements[index];
}

template<class DataType>
void Array<DataType>::changeSize(int newSize)
{
  errorCode &= (7 - 4); // unset
  if (newSize <= 0)
    errorCode |= 4;
  else
  {
    DataType* newElements = new DataType[newSize];
    int limit = newSize > capacity ? capacity : newSize;
    for (int i = 0; i < limit; i++)
      newElements[i] = elements[i];
    delete [] elements;
    elements = newElements;
    capacity = newSize;
  }
}

// to access capacity
template<class DataType>
int Array<DataType>::length() const
{
  return capacity;
}

// overloaded assignment operator

template <class DataType>
Array<DataType>& Array<DataType>::operator=(const Array<DataType>& a)
{
  if (this != &a)
  {
    delete [] elements;

    capacity = a.capacity;
    errorCode = a.errorCode;
    elements = 0;

    if (capacity > 0)
    {
      elements = new DataType[capacity];
      for (int i = 0; i < capacity; i++)
        elements[i] = a.elements[i];
    }
  }
  return *this;
}

template<class DataType>
int Array<DataType>::lsearch(const DataType& parameter)const
{
  int result = -1; // not found

  for(int i = 0; i < capacity; i++)
  {
    if(elements[i] == parameter)
      return i;
  }
  return result;
}

template <class DataType>
int Array<DataType>::bsearch(const DataType& value)const
{
  int s = 0;
  int e = capacity - 1;

  int m = (s + e)/2; // the middle element
  if(value == elements[m]) // got lucky -- matches middle element
  {
    return m; // return index of found element
  }
  else if(s == e)  // 1-element array
    return -1; // only element in array did not match
  else if(value < elements[m]) // look between s and m-1
  {
    if(s == m)
      return -1; // but that range is empty, no match
    else
      return bsearch2(elements, &value, s, m - 1); // look in s to m-1
  }
  else // look between m+1 and e
  {
    if (m == e)
      return -1; // but that range is empty, no match
    else
      return bsearch2(elements, &value, m + 1, e); // look in m-1 to e
  }
}

template <class DataType>
int Array<DataType>::bsearch2 (const Array <DataType> elements, const DataType & value, int t, int f)
{
  int s = t;
  int e = f;

  int m = (s + e)/2;
  if(value == elements[m])
  {
    return m;
  }
  else if(s == e)  // 1-element array
    return -1; // only element in array did not match
  else if(value < elements[m]) // look between s and m-1
  {
    if(s == m)
      return -1; // but that range is empty, no match
    else
      return bsearch(elements, & value, s, m - 1); // look in s to m-1
  }
  else // look between m + 1 and e
  {
    if (m == e)
      return -1; // but that range is empty, no match
    else
      return bsearch2(elements, & value, m + 1, e); // look in m-1 to e
  }
}

// copy constructor
template <class DataType>
Array<DataType>::Array(const Array<DataType>& a)
{
  capacity = a.capacity;
  errorCode = a.errorCode;
  elements = 0;

  if (capacity > 0)
  {
    elements = new DataType[capacity];
    for (int i = 0; i < capacity; i++)
      elements[i] = a.elements[i];
  }
}

// destructor
template <class DataType>
Array<DataType>::~Array()
{
  delete [] elements;
}

#endif

Hello,

Do you have a specific line?
line 232, 256, 261, 285, which basically are the function prototypes/calls for bsearch and bsearch2 function. I think I'm passing the dynamic array wrong, but I'm not sure what to do with them either.
Last edited on
Topic archived. No new replies allowed.