Help

Hello I am trying to create a setCapacity(unsigned newCapacity) method for circular queues. I was given a test that this method has to pass and it is failing on accessing the getLast() and getFirst() assertions.
Thanks for any help. I think it I have to set the the first and last to something but I do not know to what.
sorry I don't know how to use code tags

here is my code for setCapacity
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
#include "ArrayQueue.h"
#include <cstring> // memcpy()
using namespace std;
void ArrayQueue::setCapacity(unsigned newCapacity){

Item* newArray = NULL;
if(newCapacity==0 or newCapacity<mySize){
throw QueueException("setCapacity()","capacity equals zero or is greater than the size");
} else {
newArray = new Item[newCapacity];
if (newCapacity >myCapacity) {
for (unsigned i = 0; i < mySize; i++) {
newArray[i] = myArray[(i-mySize)%myCapacity];
}

} else if(newCapacity<myCapacity){
for (unsigned i = 0; i < newCapacity; i++) {
newArray[i] = myArray[i %myCapacity];
}

newArray[myLast]= myArray[myLast];
}

myCapacity = newCapacity;
delete[] myArray;
myArray = newArray;

}
}

Item ArrayQueue::getFirst() const {
if (mySize == 0) {
throw EmptyQueueException("Queue is Empty");
} else {
return myArray[myFirst];
}
}

Item ArrayQueue::getLast() const {
if (mySize == 0) {
throw EmptyQueueException("Queue is empty");
} else {
return myArray[(myLast-1+myCapacity) % myCapacity];
}
}
ArrayQueue::ArrayQueue(unsigned capacity) {
if (capacity == 0) {
throw QueueException("Constructor()", "capacity is 0");
} else {

myCapacity = capacity;
myFirst = 0;
myLast = 0;
mySize = 0;
myArray = new Item[capacity];
}
}
ArrayQueue::ArrayQueue(const ArrayQueue& original) {
makeCopyOf(original);
}

void ArrayQueue::makeCopyOf(const ArrayQueue& original) {
mySize = original.mySize;
myCapacity = original.myCapacity;
myFirst = original.myFirst;
myLast = original.myLast;
myArray = new Item[myCapacity];
memcpy(myArray, original.myArray, myCapacity * sizeof(Item));
}

ArrayQueue::~ArrayQueue() {
delete[] myArray;
myArray = NULL;
mySize = myFirst = myLast = 0;
}
//And here is my Test
//it fails on this assert( q2.getLast() == 5 );
//Test changing capacity on a queue with items that
//start at the beginning of the array
ArrayQueue q2(5);
for (int i = 0; i < 5; i++){
q2.append(i+1);
}
assert( q2.getCapacity() == 5 );
assert( q2.getSize() == 5 );
assert( !q2.isEmpty() );
assert( q2.isFull() );
assert( q2.getFirst() == 1 );
assert( q2.getLast() == 5 );

//increase the capacity
q2.setCapacity(10);
assert( q2.getCapacity() == 10 );
assert( q2.getSize() == 5 );
assert( !q2.isEmpty() );
assert( !q2.isFull() );
assert( q2.getFirst() == 1 );
assert( q2.getLast() == 5 );
cout << " 2a" << flush; 
Last edited on
Please don't double-post. It wastes people's time.
sorry I thought I had deleted one of the posts
Topic archived. No new replies allowed.