C++ Program not giving complete output.

In this C++ program, I'm dealing with classes in a "general list" with x,y values, in the output, the x values are printing, but the y's stay as 0. I'm not sure if I'm filling the array correctly, but it's very close to working properly. Can anyone point me in the right direction? Thanks.

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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <iostream>
#include<iomanip>
using namespace std;
const int CAPACITY = 20;
class List
{
private:
int aryList[CAPACITY][2];
int arySize;
int pos;

public:
List();
List(List& aryList);
void clear();
void first();
void prev();
void next();
void last();
void replace(int, int);
void setPos(int);
void erase();
void insertAfter(int, int);
void insertBefore(int, int);
int size();
bool empty();
int getXElement();
int getYElement();
int getPos();
List operator= (List& aryListB);

friend List operator + (List& ListA, List& ListB);
};
ostream& operator <<(ostream& outs, List& aryList);
List operator + (List& ListA, List& ListB);
bool operator == (List aryList1, List aryList2);

List::List()
{
this->clear();
};
List::List(List& aryListB)
{
this->clear();
int orig_pos = aryListB.getPos();
aryListB.first();
for (int i = 0; i < aryListB.size(); aryListB.next(), i++) {
this->aryList[i][0] = aryListB.getXElement();
this->aryList[i][1] = aryListB.getYElement();
this->arySize++;
}
this->pos = orig_pos;
aryListB.setPos(orig_pos);
}

void List::clear()
{
arySize = 0;
pos = 0;
int i;
for (i = 0; i < CAPACITY; i++)
{
aryList[i][0] = 0;
aryList[i][1] = 0;
}
}

void List::first()
{
pos = 0;
}

void List::prev()
{
if (pos > 0)
pos--;
}

void List::next()
{
if (pos < (arySize - 1))
pos++;
}

void List::last()
{
if (arySize > 0)
pos = (arySize - 1);
else
pos = 0;
}

void List::replace(int a, int b)
{
aryList[pos][0] = a;
aryList[pos][1] = b;
}

void List::setPos(int newPos)
{
if (newPos >= 0 && newPos < arySize)
pos = newPos;
}

void List::erase()
{
if (arySize > 0)
{
for (int i = pos; i < arySize - 1; i++)
{
aryList[i][0] = aryList[i + 1][0];
aryList[i][1] = aryList[i + 1][1];
}
aryList[arySize - 1][0] = 0;
aryList[arySize - 1][1] = 0;
arySize -= 1;
}
}

void List::insertAfter(int a, int b)
{
if (pos >= CAPACITY - 1)
cout << "No insert made" << endl;
else {
for (int i = arySize; i > pos; i--) {
aryList[i][0] = aryList[i + 1][0];
aryList[i][1] = aryList[i + 1][1];
}
if (arySize == 0) {
aryList[pos][0] = a;
aryList[pos][1] = b;
}
else {
aryList[pos + 1][0] = a;
aryList[pos + 1][b] = b;
pos++;
}
arySize++;
}
}

void List::insertBefore(int a, int b)
{
if (pos == (int)CAPACITY - 1) {
aryList[pos][0] = a;
aryList[pos][1] = b;
}
else {
for (int i = arySize; i >= pos && i != 0; i--) {
aryList[i][0] = aryList[i + 1][0];
aryList[i][1] = aryList[i + 1][1];
}
aryList[pos][0] = a;
aryList[pos][1] = b;
if (arySize < CAPACITY)
arySize++;
}
}
int List::size()
{
return arySize;
}

bool List::empty()
{
return (size() == 0);
}

int List::getXElement()
{
if (size() <= 0)
return 0;
else
return aryList[pos][0];
}

int List::getYElement()
{
if (size() <= 0)
return 0;
else
return aryList[pos][1];
}

int List::getPos()
{
return pos;
}
ostream& operator <<(ostream& outs, List& aryList)
{
int orig_pos = aryList.getPos();
aryList.first();
outs << "[ ";
for (int i = 0; i < aryList.size(); aryList.next(), i++) {
outs << '[' << aryList.getXElement() << " " << aryList.getYElement() << "]";
}
outs << " ]";
aryList.setPos(orig_pos);
return outs;
}
List operator+(List& ListA, List& ListB)
{
List ListC(ListA);
int orig_pos = ListB.getPos();
ListC.first();
ListB.first();
for (int i = 0; i < ListB.size(); ListB.next(), ListC.next(), i++) {
ListC.replace(ListB.getXElement() + ListC.getXElement(), ListB.getYElement() + ListC.getYElement());
}
ListB.setPos(orig_pos);
return ListC;
}
bool operator == (List aryList1, List aryList2)
{
if (aryList1.size() == aryList2.size()) {
int orig_pos1 = aryList1.getPos();
int orig_pos2 = aryList2.getPos();
aryList1.first();
aryList2.first();
for (int i = 0; i < aryList1.size(); i++, aryList1.next(), aryList2.next()) {
if (aryList1.getXElement() != aryList2.getXElement() && aryList1.getYElement() != aryList2.getYElement()) {
aryList1.setPos(orig_pos1);
aryList2.setPos(orig_pos2);
return false;
}
}
aryList1.setPos(orig_pos1);
aryList2.setPos(orig_pos2);
return true;
}
return false;
}
bool operator != (List aryList1, List aryList2) {
if (aryList1 == aryList2)
return false;
else
return true;
}
List List::operator = (List& aryListB)
{
int orig_pos = aryListB.getPos();
aryListB.first();
first();
arySize = 0;
for (int i = 0; i < aryListB.size(); aryListB.next(), this->next(), i++) {
this->aryList[i][0] = aryListB.getXElement();
this->aryList[i][1] = aryListB.getYElement();
this->arySize++;
}
pos = orig_pos;
aryListB.setPos(orig_pos);
return aryListB;
}

int main()
{
List a,b; int endit;

for (int i=1;i<=10;i++)
a.insertAfter(i,i*2);
cout << "List a : " << endl;
cout << " " << a << endl;
cout << "Number of elements in a - " << a.size() << endl;

for (int i=1;i<=10;i++)
b.insertBefore(i,i*2);
cout << "List b : " << endl;
cout << " " << b << endl;
cout << "Number of elements in b - " << b.size() << endl;

if ( a == b )
cout << "a == b: List a & b are equal" << endl;
else
cout << "a == b: List a & b are Not equal" << endl;

a.first();
b.first();
cout << "First elmenet in list a " << a.getXElement() << "," << a.getYElement() << endl;
cout << "First elmenet in list b " << b.getXElement() << "," << b.getYElement() << endl;
a.last();
b.last();
cout << "Last elmenet in list a " << a.getXElement() << "," << a.getYElement() << endl;
cout << "Last elmenet in list b " << b.getXElement() << "," << b.getYElement() << endl;

cout << endl << endl << " Start of new stuff" << endl;

b.erase();
cout << "b.erase(): Print Empty List b: " << b << endl;

if ( a != b )
cout << "a != b: List a & b are not equal" << endl;
else
cout << "a != b: List a & b are equal" << endl;

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;

a.setPos(5);
b.first();
for ( int i=1; i<4; 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;

List c(b);
cout << "Copy Constructor c(b)" << endl;
cout << "List b : " << b << endl;
cout << "List c : " << c << endl;

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*5);
alist.first();
cout << "New List alist with positions printed 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;
sq.first();
sq.erase(); sq.erase();
cout << "First elmenet 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;
return 0;
}

Last edited on
Fix your code tags please.
http://www.cplusplus.com/articles/jEywvCM9/
Sorry about that. I followed the instructions on that link and when I post it with tags, it's saying it's too long.
I finally got it tagged. I apologize if it looks ugly in the format, it was the only way I could go around the too long error.
Fix line 135:
aryList[pos + 1][b] = b;

Presumably that's not what you meant.



Mopar75 wrote:
it was the only way I could go around the too long error

Not really. A better way would have been to ... start small and develop slowly, testing at every step along the way. Not present 384 lines of code and look for a needle in a haystack. Add one function at a time; printing out your "list" (ahem!) should be one of the earliest functions.
Last edited on
Thanks for the response, so line 135 should look like 134 in that regard? The output has changed a little and is adding some y values, but shouldn't the array have more y values in it? I'm still getting a heap of 0's
Line 135 should be
aryList[pos + 1][1] = b;
Put that in and some of your y values are non-zero.

As far as I can see, your array (I can't call it a "list" - sorry!) is initialised with zeros everywhere. They only become non-zero when you explicitly set them.

Why don't you simplify your code, taking out (for now) the functions that you don't need and testing a single, simple example that produces your problem.

At the moment you have presented a non-indented wall of code that I can't do any more than glance through.
I'll make some changes and see what happens. Yeah I'm sorry about the non indented format, the word count exceeded the limit if I had made it look like my code in Notepad++
I think I'm seeing the problem, the change on line 135 correct list a's output, but list b is staying 0,0 after 10,20. I'm not entirely sure what changes I need to make to get it filled because as it gets to the bottom, it's going to copy to c and eventually to d with the copy constructor.
Your insertAfter and insertBefore routines are wrong.

Particularly this bit for insertAfter (and similarly for insertBefore):
1
2
3
4
for (int i = arySize; i > pos; i--) {
aryList[i][0] = aryList[i + 1][0];
aryList[i][1] = aryList[i + 1][1];
}

You probably want i-1, not i+1 on the RHS (and also a preceding check on arySize, to make sure it will fit).

If you made your main() routine less verbose you would be able to fit more chars in a post. It would also help to know what your expected output was. Your code is difficult to read.
Last edited on
Yeah the main program was provided by the instructor and he doesn't want it changed, though he did have syntax errors that I found and fixed in it. That's the thing about him, you never know exactly what he wants, hence the mass confusion.
That did the trick, setting it to i-1 has it running I believe the way it was meant to. Thank you so much for your time.
Topic archived. No new replies allowed.