My program does not sort the lelements:

Here is what I get:
1
2
3
4
5
6
7
8
C:\Dev-Cpp\Chapter9>newarraylisttype
Line 8: Enter numbers ending with -999
45 7 12 98 5 -999
Line 15: The list before sorting:
45 7 12 98 5

Line 19: The list after sorting:
45 7 12 98 5


Here is the coding:
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
template <class elemType>
class arrayListType
public:

bool isEmpty() const;
//Function to determine whether the list is empty
//Postcondition: Returns true if the list is empty;
// otherwise, returns false.
bool isFull() const;
//Function to determine whether the list is full.
//Postcondition: Returns true if the list is full;
// otherwise, returns false.
int listSize() const;
//Function to determine the number of elements in the list
//Postcondition: Returns the value of length.
int maxListSize() const;
//Function to determine the size of the list.
//Postcondition: Returns the value of maxSize.
void print() const;
//Function to output the elements of the list
//Postcondition: Elements of the list are output on the
// standard output device.
void clearList();
//Function to remove all the elements from the list.
//After this operation, the size of the list is zero.
//Postcondition: length = 0;
int seqSearch(const elemType &item);

void insert(const elemType& insertItem);
//Function to insert the item specified by the parameter
//insertItem at the end of the list. However, first the
//list is searched to see whether the item to be inserted
//is already in the list.
//Postcondition: list[length] = insertItem and length++
// If the item is already in the list or the list
// is full, an appropriate message is displayed.

arrayListType(int size = 100);
//constructor
//Creates an array of the size specified by the
//parameter size. The default array size is 100.
//Postcondition: The list points to the array, length = 0,
// and maxSize = size
arrayListType(const arrayListType<elemType>& otherList);
//copy constructor
~arrayListType();
//destructor
//Deallocates the memory occupied by the array.

protected:
elemType *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
};
  
template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
return (length == 0);
}
template <class elemType>
bool arrayListType<elemType>::isFull() const
{
return (length == maxSize);
}

template <class elemType>
int arrayListType<elemType>::listSize() const
{
return length;
}
template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
return maxSize;
}

template <class elemType>
void arrayListType<elemType>::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}

template <class elemType>
void arrayListType<elemType>::clearList()
{
length = 0;
} //end clearList

template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
if (size < 0)
{
cerr << "The array size must be positive. Creating "
<< "an array of size 100. " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new elemType[maxSize];
assert(list != NULL);
}
template <class elemType>
arrayListType<elemType>::~arrayListType()
{
delete [] list;
}
template <class elemType>
arrayListType<elemType>::arrayListType
(const arrayListType<elemType>& otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize]; //create the array
assert(list != NULL); //terminate if unable to allocate
//memory space
for (int j = 0; j < length; j++) //copy otherList
list [j] = otherList.list[j];
} //end copy constructor

template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator=
(const arrayListType<elemType>& otherList)
{
if (this != &otherList) //avoid self-assignment
{
delete [] list;
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize]; //create the array
assert(list != NULL); //if unable to allocate memory
//space, terminate the program
for (int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
return *this;
}
template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item) 
{
int loc;
bool found = false;
for (loc = 0; loc < length; loc++)
if (list[loc] == item)
{
found = true;
break;
}
if (found)
return loc;
else
return -1;
} //end seqSearch

template<class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem)
{
int loc;
if (length == 0)
cerr << "Cannot delete from an empty list." << endl;
else
{
loc = seqSearch(removeItem);
if (loc != -1)
removeAt(loc);
else
cout << "The item to be deleted is not in the list."
<< endl;
}
} //end remove
template <class elemType>
void arrayListType<elemType>::insert(const elemType& insertItem)
{
int loc;
if (length == 0) //list is empty
list[length++] = insertItem; //insert the item and
//increment the length
else if (length == maxSize)
cerr << "Cannot insert in a full list." << endl;
else
{
loc = seqSearch(insertItem);
if (loc == -1) //the item to be inserted
//does not exist in the list
list[length++] = insertItem;
else
cerr << "the item to be inserted is already in "
<< "the list. No duplicates are allowed." << endl;
}
} //end insert
template<class elemType>
class orderedArrayListType : public arrayListType<elemType>
{
public:
      void insertOrd(const elemType&);
      int binarySearch(const elemType &item);
      void selectionSort();
      
      orderedArrayListType(int size = 100);
private:
        void swap(int first, int second);
        int minLocation(int first, int last);
        elemType *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
}; 
template <class elemType>
orderedArrayListType<elemType>::orderedArrayListType(int size)
{
if (size < 0)
{
cerr << "The array size must be positive. Creating "
<< "an array of size 100. " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new elemType[maxSize];
assert(list != NULL);
}
template<class elemType>
int orderedArrayListType<elemType>::minLocation(int first, int last)
{
    int loc, minIndex;
    minIndex = first;
    
    for(loc = first + 1; loc <= last; loc++)
        if(list[loc] < list[minIndex])
           minIndex = loc;
    return minIndex;
} //end minLocation
template<class elemType>
void orderedArrayListType<elemType>::swap(int first, int second)
{
     elemType temp;
     temp = list[first];
     list[first] = list[second];
     list[second] = temp;
}  //end swap 
               

template<class elemType>
void orderedArrayListType<elemType>::selectionSort()
{
int minIndex;
int length;
for (int loc = 0; loc < length - 1; loc++)
{
minIndex = minLocation(loc, length - 1);
swap(loc, minIndex);
}
}
    //**************************************************************
// Author: D.S. Malik
//
// This program illustrates how to use selection sort in a
// program.
//**************************************************************
#include <iostream> //Line 1
//#include "arrayListType.h" //Line 2
using namespace std; //Line 3
int main() //Line 4
{ //Line 5
  orderedArrayListType<int> list; //Line 6
int num; //Line 7
cout << "Line 8: Enter numbers ending with -999"
<< endl; //Line 8
cin >> num; //Line 9
while (num != -999) //Line 10
{ //Line 11
list.insert(num); //Line 12
cin >> num; //Line 13
} //Line 14
cout << "Line 15: The list before sorting:" << endl; //Line 15
list.print(); //Line 16
cout << endl; //Line 17
list.selectionSort(); //Line 18
cout << "Line 19: The list after sorting:" << endl; //Line 19
list.print(); //Line 20
cout << endl; //Line 21
return 0; //Line 22
}
      

That cannot be the code since it cannot compile.

The problem is line 208: length shadows the member of arrayListType and is only used in member functions of orderedArrayListType like selectionSort() where it is always 0. So remove line 208/209

The constructor of orderedArrayListType should call the base class constructor instead of replicate the code. Like so:
1
2
3
4
template <class elemType>
orderedArrayListType<elemType>::orderedArrayListType(int size) : arrayListType{size}
{
}
Hello Bopaki,

Really. 287 lines of code all run together and you expect someone to read this?

The "#include" lines need to be the first lines of the program. Should anything in the class definition and functions need these header files they do not see them because you put the "#include"s just before "main".

You define the functions "remove" and "operator =", but you did not forward declare then in the class. This is producing errors for me and needs fixed.

Hope that helps,

Andy

P.S. Which is easier to read? What you have or this:
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
template <class elemType>
class arrayListType
{ // <--- This was missing.
	public:

		bool isEmpty() const;
		//Function to determine whether the list is empty
		//Postcondition: Returns true if the list is empty;
		// otherwise, returns false.

		bool isFull() const;
		//Function to determine whether the list is full.
		//Postcondition: Returns true if the list is full;
		// otherwise, returns false.

		int listSize() const;
		//Function to determine the number of elements in the list
		//Postcondition: Returns the value of length.

		int maxListSize() const;
		//Function to determine the size of the list.
		//Postcondition: Returns the value of maxSize.

		void print() const;
		//Function to output the elements of the list
		//Postcondition: Elements of the list are output on the
		// standard output device.

		void clearList();
		//Function to remove all the elements from the list.
		//After this operation, the size of the list is zero.
		//Postcondition: length = 0;

		int seqSearch(const elemType &item);

		void insert(const elemType& insertItem);
		//Function to insert the item specified by the parameter
		//insertItem at the end of the list. However, first the
		//list is searched to see whether the item to be inserted
		//is already in the list.
		//Postcondition: list[length] = insertItem and length++
		// If the item is already in the list or the list
		// is full, an appropriate message is displayed.

		arrayListType(int size = 100);
		//constructor
		//Creates an array of the size specified by the
		//parameter size. The default array size is 100.
		//Postcondition: The list points to the array, length = 0,
		// and maxSize = size

		arrayListType(const arrayListType<elemType>& otherList);

		//copy constructor
		~arrayListType();
		//destructor
		//Deallocates the memory occupied by the array.

	protected:
		elemType *list; //array to hold the list elements
		int length; //to store the length of the list
		int maxSize; //to store the maximum size of the list
};

The blank lines and indenting help.

Lines 208/209 have been removed as coder777 indicated.
I have modified my constructor to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class elemType>
orderedArrayListType<elemType>::orderedArrayListType(int size)
{
if (size < 0)
{
cerr << "The array size must be positive. Creating "
<< "an array of size 100. " << endl;
    length = size;
    for(int i = 0; i < size; i++)
    list[i] = 0;
}

list = new elemType[size];
assert(list != NULL);
}

the list still does not get sorted.
Bopaki, you should have better indentation. I overlooked that list is also shadowed. Remove line 207 as well.

I have modified my constructor to this:
Again. This is wrong. It is important that you understand why you need to call the constructor of the base class: Only in the constructor of the base class the member variables are properly initialized. No where else.

Do not define the member variables of the base class again in your inherited class! All variables in the inherited class are solely used in the inherited class. That means in that case the member functions of the inhertited class do not use the base class variables which remain unchanged.

Currently when you call insert(...) the variable list of the base class is modified.
When you call selectionSort() the variable list of the inherited class will be modified.
Thus when you call print() the untouched base class variable list is used and it appears as if no sorting is done.
I got it working eventually!!!!
Here is the correct output:
1
2
3
4
5
6
7
8
C:\Dev-Cpp\Chapter9>arrayListType
Line 3: Enter numbers ending with -999
45 67 89 12 3 -999
Line 8: The list before sorting:
45 67 89 12 3

Line 12: The list after sorting:
3 12 45 67 89

Thank you to both of you coder777 and Handy Andy
Topic archived. No new replies allowed.