Hey guys, we are supposed to be doing this program using a container class to store (x, y) values in and I already wrote this program using a 2D array, but did not turn it in. Others used the 2D array as well and our professor commented that that was "interesting". Guess we were not supposed to use a 2D. My question is, how to do this with a 1D array because you have two separate member functions called getXElement() and getYElement()?
Also we have not learned vectors and cannot call std library stuff other than basic knowledge
He showed us the idea of the array like this:
[xy], [xy], [xy] but how would the array know you are calling an individual x or y.
Instructions:
You are to implement a 'List' class to handle a list with general operations. That means you can insert and delete any element anywhere in the list. Your list will be an array. Your general list will store X Y coordinates, i.e. pairs of numbers in the array. The list has no order, except for the order you insert or delete. The methods you are to implement are as given:
Constructor methods (two, default and copy constructor, a list to a newly defined list, ie 'List listA(listB)' )
empty returns true or false if list is empty or not.
first makes current position at the beginning of the list
last makes current position at the end of a list.
prev places current position at the previous element in the list
next places current position at the next element in the list
getPos returns current position or where you are in the list
setPos(int) places current position in a certain position in the list
insertBefore(int,int) inserts a new element before the current position
insertAfter(int, int) inserts a new element after the current position
get X Element returns the one element that current position is pointing to
get Y Element returns the one element that current position is pointing to
size returns the size of the list (number of elements in list)
replace(int,int) replace the current element with a new value X Y value
erase deletes the current element
clear makes the list an empty list
overload the operators: (at least) << output, ==, !=, + ,+=, { = (assignment) maybe? }
You are to implement the List class using an array. For now the array can be 20 in size.
You will need to use the 'ElementType' for 'typing' your data.
You will need to use CAPACITY constant for the size of the array, in case we need to change the array size.
Invariants: The elements in the list are to be left justified with in the array. ‘pos’ will point to a valid location in the array (ie where data is located. On insert, ‘pos’ should point to the element just inserted on insert operations. ‘pos’ should never be outside of element list.
main program to be used with the class:
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
|
int main()
{
List a, b;
int endit;
//inserting values in list a
for (int i=1;i<=10;i++)
{
a.insertAfter(i, i * 2);
}
//printing list a
cout << "List a : " << endl;
cout << " " << a << endl;
cout << "Number of elements in a - " <<a.size()<<endl;
//inserting values in list b
for (int i=1;i<=10;i++)
{
b.insertBefore(i, i * 2);
}
//printing list b
cout << "List b : " << endl;
cout << " " << b << endl;
cout << "Number of elements in b - " <<b.size()<<endl;
//comparing list a and list b
if (a == b)
{
cout << "a == b: List a & b are equal" << endl;
}
else
{
cout << "a == b: List a & b are NOT equal" << endl;
}
//finding the first element in each list
a.first();
b.first();
cout<<"First element in list a: (" << a.getXElement() << "," << a.getYElement()<<")" <<endl;
cout<<"First element in List b: (" << b.getXElement() << "," << b.getYElement()<<")" <<endl;
//finding the last element in each list
a.last();
b.last();
cout << "Last element in list a: (" <<a.getXElement() << "," << a.getYElement()<<")" <<endl;
cout << "Last element in list b: (" <<b.getXElement() << "," << b.getYElement()<<")" <<endl;
cout << endl << endl << " Start of new stuff" << endl;
//erasing list b
b.clear();
cout << "b.erase(): Empty List b: " << b << endl;
//comparing list a and b again as not equal
if (a != b)
{
cout << "a != b: List a & b are not equal" << endl;
}
else
{
cout << "a != b: List a & b are equal" << endl;
}
//inserting new values into list b
for (int i=1;i<=10;i++)
{
b.insertBefore(i, -i*2);
}
cout << "List b with neg y's" << endl;
cout<< "List b: "<< b <<endl;
//performing more calculations
a.setPos(5);
b.first();
for (int i=1;i<8;i++)
{
a.erase();
b.replace(i, i*3);
b.next();
}
cout << "Modified Object 'a' (erase 3) " << endl;
cout << "List a: " << a << endl;
cout << "Modified Object 'b' (replace 3)" << endl;
cout << "List b: " << b << endl;
//copying list b into c
List c(b);
cout << "Copy Constructor c(b)" << endl;
cout << "List b : " << b << endl;
cout << "List c : " << c << endl;
//comparing c and b
if (c == b)
cout << "List c & b are equal" << endl;
else
cout << "List c & b are Not equal" << endl;
//list c and e
List e;
e = c;
cout << "Object 'c' assigned to Object 'e':" << endl;
cout << "List c : " << c << endl;
cout << "List e : " << e << endl;
List d;
d = a;
d.first();
endit = d.size()/2;
for (int i = 1; i < endit; d.next(), i++)
{
d.insertBefore(d.getXElement()*2, d.getYElement()*2);
d.next();
}
cout << "Results after some inserts at front of d " << endl;
cout << "List d : " << d << endl;
a.first();
endit = a.size();
for (int i = 1; i < endit; a.next(), i++)
{
a.replace(a.getPos()-a.getXElement(), a.getPos()-a.getYElement());
a.next();
}
cout << "Results after adding pos & flipping signs on list a" << endl;
cout << "List a : " << a << endl;
List alist(b);
alist.clear();
for (int i=1;i<=5;i++)
alist.insertAfter(i, i-1);
alist.first();
cout << "New List alist with positions above: " << endl;
for (int i=1;i<=5;i++)
{
cout << setw(8) << alist.getPos();
alist.next();
}
cout << endl;
alist.first();
for (int i=1;i<=5;i++)
{
cout << setw(6) << alist.getXElement() << "," << alist.getYElement();
alist.next();
}
cout << endl;
cout << endl << " check out boundary conditions" << endl;
List sq;
cout << "number of elements in empty sq list = " << sq.size() << endl;
cout << " print empty list sq: " << sq << endl;
a.first();
sq.erase(); sq.erase();
cout << "First element in list a: (" << a.getXElement() << "," << a.getYElement()<<")"<< endl;
sq.setPos(5);
cout << "empty sq values " << sq << endl;
sq.insertBefore(333, 444);
cout << "sq list: " << sq << endl;
sq.next(); sq.next();
cout << "sq.getElement() = " << sq.getXElement() << "," << sq.getYElement() << endl;
cout << "sq list = " << sq << endl;
sq.prev(); sq.prev();
cout << "sq.getElement()= " << sq.getXElement() << "," << sq.getYElement() << endl;
cout<< "sq list = "<< sq << endl;
|