Guidance Needed With Bubble Sort

Pages: 12
Hi everyone,

My current assignment is to take the source code given to us from the author and to manipulate it with a bubble sort. I have experience doing this but I'm a bit puzzled trying to incorporate my way of doing this into another person's work.

The source code given to us has the user enter 5 integers, then it will print them back out and ask the user to delete one of them. It will then re-print the list minus the deleted number and I need to sort them in numerical order. It does the same thing for an array of 5 strings after that.

My current code is this (everything I added doesn't have the commented line numbers):

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
#include <iostream>                                       //Line 1

#include <string>                                         //Line 2
#include "arrayListType.h"                                //Line 3

using namespace std;                                      //Line 4

// Creating the bubble sort template
template<class bubble>
void myBubbleSort(bubble a[], int n)
{
    bool flag = 0;
    do
    {
        flag = 0;
        for(int i = 0; i < (n - 1); i++)
        {
            if(a[i] > a[i+1])
            {
                bubble elm;
                elm = a[i];
                a[i] = a[i + 1];
                a[i + 1] = elm;
                flag = 1;
            }
        }
    }
	while(flag);
}

int main()                                                //Line 5
{                                                         //Line 6
    arrayListType<int> intList(100);                      //Line 7
    arrayListType<string> stringList;                     //Line 8

    int number;                                           //Line 9

    cout << "List 10: Enter 5 integers: ";                //Line 10

    for (int counter = 0; counter < 5; counter++)         //Line 11
    {                                                     //Line 12
        cin >> number;                                    //Line 13
        intList.insertAt(counter, number);                //Line 14
    }                                                     //Line 15

    cout << endl;                                         //Line 16
    cout<<"List 19: The list you entered is: ";           //Line 17
    intList.print();                                      //Line 18
					                  //Line 19
    cout << "Line 20: Enter the item to be deleted: ";    //Line 20
    cin >> number;                                        //Line 21
    intList.remove(number);                               //Line 22
    cout << "Line 23: After removing " << number
         << ", the list is:" << endl;			  //Line 23


    // Sorting the integers in numerical order
    myBubbleSort(intList, 5);

    cout << "\n";

    // Printing out the integers in numerical order
    for(int i = 0; i < 5; i++)
    {
        cout << intList[i];
    }

	cout << "\n";

						          //Line 25

    string str;                                           //Line 26

    cout << "Line 27: Enter 5 strings: ";                 //Line 27

    for (int counter = 0; counter < 5; counter++)         //Line 28
    {                                                     //Line 29
        cin >> str;                                       //Line 30
        stringList.insertAt(counter, str);                //Line 31
    }                                                     //Line 32

    cout << endl;                                         //Line 33
    cout << "Line 34: The list you entered is: " << endl; //Line 34
    stringList.print();                                   //Line 35
    cout << endl;                                         //Line 36

    cout << "Line 37: Enter the string to be deleted: ";  //Line 37
    cin >> str;                                           //Line 38
    stringList.remove(str);                               //Line 39
    cout << "Line 40: After removing " << str
         << ", the list is:" << endl;                     //Line 40
				                          //Line 42


    // Sorting the strings in alphabetical order
    myBubbleSort(stringList, 5);

    cout << "\n";

    // Printing out the strings in alphabetical order
    for(int i = 0; i < 5; i++)
    {
        cout << stringList[i];
    }

	cout << "\n";

    return 0;                                             //Line 43
}                                                         //Line 44



The errors I'm getting are because my bubble sort arguments don't match the array's the author is creating here and I'm a bit stuck as to how I would make these work together. Any help would be great, thanks!
Last edited on
Your function takes a raw array of the template arg type, but the arrayListType is a custom data structure. If that data structure has a method to get a pointer to the first element, and arrayListType guarantees that it's items are stored contiguously in memory, then you could pass in the pointer to arg 1. Otherwise, you could change your function to accept an arrayListType<bubble>, instead of a raw array. I'm not sure what your instructor expects you to do.
Here is his header file with all of his arrayListType definitions. I've been messing around with my bubble sort function to accept his data structures to no avail. It's very long so I'll split it in two:

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
#ifndef H_arrayListType
#define H_arrayListType

//***********************************************************
// Author: D.S. Malik
//
// This class specifies the members to implement the basic
// properties of array-based lists.
//***********************************************************

#include <iostream>
#include <cassert>

using namespace std;

template <class elemType>
class arrayListType
{
public:
    const arrayListType<elemType>& operator=
                         (const arrayListType<elemType>&);
      //Overloads the assignment operator
    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.
    bool isItemAtEqual(int location, const elemType& item) const;
      //Function to determine whether the item is the same 
      //as the item in the list at the position specified by
      //Postcondition: Returns true if the list[location] 
      //    is the same as the item; otherwise, 
      //               returns false.
   void insertAt(int location, const elemType& insertItem);
      //Function to insert an item in the list at the 
      //position specified by location. The item to be inserted 
      //is passed as a parameter to the function.
      //Postcondition: Starting at location, the elements of the
      //    list are shifted down, list[location] = insertItem;,
      //    and length++;. If the list is full or location is
      //    out of range, an appropriate message is displayed.
   void insertEnd(const elemType& insertItem);
      //Function to insert an item at the end of the list. 
      //The parameter insertItem specifies the item to be inserted.
      //Postcondition: list[length] = insertItem; and length++;
      //    If the list is full, an appropriate message is 
      //    displayed.
    void removeAt(int location);
      //Function to remove the item from the list at the 
      //position specified by location 
      //Postcondition: The list element at list[location] is removed
      //    and length is decremented by 1. If location is out of 
      //    range,an appropriate message is displayed.
    void retrieveAt(int location, elemType& retItem) const;
      //Function to retrieve the element from the list at the  
      //position specified by location. 
      //Postcondition: retItem = list[location] 
      //    If location is out of range, an appropriate message is
      //    displayed.
    void replaceAt(int location, const elemType& repItem);
      //Function to replace the elements in the list at the 
      //position specified by location. The item to be replaced 
      //is specified by the parameter repItem.
      //Postcondition: list[location] = repItem 
      //    If location is out of range, an appropriate message is
      //    displayed.
    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) const;
      //Function to search the list for a given item. 
      //Postcondition: If the item is found, returns the location 
      //    in the array where the item is found; otherwise,
      //    returns -1.
    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.
    void remove(const elemType& removeItem);
      //Function to remove an item from the list. The parameter 
      //removeItem specifies the item to be removed.
      //Postcondition: If removeItem is found in the list,
      //      it is removed from the list and length is 
      //      decremented by one.

    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
};




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
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>
bool arrayListType<elemType>::isItemAtEqual
                            (int location, const elemType& item) const
{
    return (list[location] == item);
}

template <class elemType>
void arrayListType<elemType>::insertAt
                  (int location, const elemType& insertItem)
{
    if (location < 0 || location >= maxSize)
        cerr << "The position of the item to be inserted "
             << "is out of range" << endl;
    else
        if (length >= maxSize)  //list is full
            cerr << "Cannot insert in a full list" << endl;
        else
        {
            for (int i = length; i > location; i--)
                 list[i] = list[i - 1];   //move the elements down

            list[location] = insertItem;  //insert the item at the 
                                          //specified position

            length++;     //increment the length
    }
} //end insertAt

template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem)
{

    if (length >= maxSize)  //the list is full
        cerr << "Cannot insert in a full list" << endl;
    else
    {
         list[length] = insertItem;   //insert the item at the end
         length++;   //increment the length
    }
} //end insertEnd

template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
    if (location < 0 || location >= length)
        cerr << "The location of the item to be removed "
             << "is out of range" << endl;
    else
    {
        for (int i = location; i < length - 1; i++)
            list[i] = list[i+1];

        length--;
    }
} //end removeAt

template <class elemType>
void arrayListType<elemType>::retrieveAt
                             (int location, elemType& retItem) const
{
    if (location < 0 || location >= length)
        cerr << "The location of the item to be retrieved is "
             << "out of range." << endl;
    else
        retItem = list[location];
} //end retrieveAt


template <class elemType>
void arrayListType<elemType>::replaceAt
                          (int location, const elemType& repItem)
{
    if (location < 0 || location >= length)
        cerr << "The location of the item to be replaced is "
             << "out of range." << endl;
    else
        list[location] = repItem;

} //end replaceAt

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

template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item) const
{
    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>::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>
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>
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; 
}


#endif 
your

1
2
3
    arrayListType<int> intList(100);                   
    arrayListType<string> stringList; 


You are passing into the bubble sort function your user defined types

arrayListType

intList and stringList are types arrayListType, they are not int or string.

Atleast that is what I think.
sorry to cut myself off.
This is what your passing in.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

void myBubbleSort(arrayListType a[], int n)
{
    bool flag = 0;
    do
    {
        flag = 0;
        for(int i = 0; i < (n - 1); i++)
        {
            if(a[i] > a[i+1])
            {
                arrayListType elm;//here your just declaring another object with the//default constructor
                

                elm = a[i]; 
                a[i] = a[i + 1];
                a[i + 1] = elm;
                flag = 1;
            }
        }
    }
	while(flag);
}



I think this is what you want it to look like
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


void myBubbleSort(int a[], int n)
{
    bool flag = 0;
    do
    {
        flag = 0;
        for(int i = 0; i < (n - 1); i++)
        {
            if(a[i] > a[i+1])
            {
                int elm;
                

                elm = a[i];
                a[i] = a[i + 1];
                a[i + 1] = elm;
                flag = 1;
            }
        }
    }
	while(flag);
}


[/code]
Last edited on
In order to work with an object like that you have to overload the subscript operator []. Your using the object as an array without overloading the subscript operator, I don't think you meant to do that on purpose. What you are trying to do is work with the array inside the object, you are not doing that.

I could be completely wrong though.
I revised your code, its messy but look at what I did.
I put the bubble sort function in your class and removed the global function.

check like 121 for change in this part, ill post the rest in another post

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
#ifndef H_arrayListType
#define H_arrayListType

//***********************************************************
// Author: D.S. Malik
//
// This class specifies the members to implement the basic
// properties of array-based lists.
//***********************************************************

#include <iostream>
#include <cassert>
#include <string>

using namespace std;

template <class elemType>
class arrayListType
{
public:
    const arrayListType<elemType>& operator=
                         (const arrayListType<elemType>&);
      //Overloads the assignment operator
    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.
    bool isItemAtEqual(int location, const elemType& item) const;
      //Function to determine whether the item is the same 
      //as the item in the list at the position specified by
      //Postcondition: Returns true if the list[location] 
      //    is the same as the item; otherwise, 
      //               returns false.
   void insertAt(int location, const elemType& insertItem);
      //Function to insert an item in the list at the 
      //position specified by location. The item to be inserted 
      //is passed as a parameter to the function.
      //Postcondition: Starting at location, the elements of the
      //    list are shifted down, list[location] = insertItem;,
      //    and length++;. If the list is full or location is
      //    out of range, an appropriate message is displayed.
   void insertEnd(const elemType& insertItem);
      //Function to insert an item at the end of the list. 
      //The parameter insertItem specifies the item to be inserted.
      //Postcondition: list[length] = insertItem; and length++;
      //    If the list is full, an appropriate message is 
      //    displayed.
    void removeAt(int location);
      //Function to remove the item from the list at the 
      //position specified by location 
      //Postcondition: The list element at list[location] is removed
      //    and length is decremented by 1. If location is out of 
      //    range,an appropriate message is displayed.
    void retrieveAt(int location, elemType& retItem) const;
      //Function to retrieve the element from the list at the  
      //position specified by location. 
      //Postcondition: retItem = list[location] 
      //    If location is out of range, an appropriate message is
      //    displayed.
    void replaceAt(int location, const elemType& repItem);
      //Function to replace the elements in the list at the 
      //position specified by location. The item to be replaced 
      //is specified by the parameter repItem.
      //Postcondition: list[location] = repItem 
      //    If location is out of range, an appropriate message is
      //    displayed.
    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) const;
      //Function to search the list for a given item. 
      //Postcondition: If the item is found, returns the location 
      //    in the array where the item is found; otherwise,
      //    returns -1.
    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.
    void remove(const elemType& removeItem);
      //Function to remove an item from the list. The parameter 
      //removeItem specifies the item to be removed.
      //Postcondition: If removeItem is found in the list,
      //      it is removed from the list and length is 
      //      decremented by one.

    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.



	void myBubbleSort( int n);

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
	elemType elm;     
};
Last edited on
Here is the second part. It is the first function in the code.

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


template <class elemType>
void arrayListType<elemType>::myBubbleSort( int n)
{
    bool flag = 0;
    do
    {
        flag = 0;
        for( int i = 0; i < (n - 1); i++)
        {
            if(list[i] > list[i+1])
            { 
				
                elm = list[i]; 
                list[i] = list[i + 1];
                list[i + 1] = elm;
                flag = 1;
            }
        }
    }
	while(flag);
}
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>
bool arrayListType<elemType>::isItemAtEqual
                            (int location, const elemType& item) const
{
    return (list[location] == item);
}

template <class elemType>
void arrayListType<elemType>::insertAt
                  (int location, const elemType& insertItem)
{
    if (location < 0 || location >= maxSize)
        cerr << "The position of the item to be inserted "
             << "is out of range" << endl;
    else
        if (length >= maxSize)  //list is full
            cerr << "Cannot insert in a full list" << endl;
        else
        {
            for (int i = length; i > location; i--)
                 list[i] = list[i - 1];   //move the elements down

            list[location] = insertItem;  //insert the item at the 
                                          //specified position

            length++;     //increment the length
    }
} //end insertAt

template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem)
{

    if (length >= maxSize)  //the list is full
        cerr << "Cannot insert in a full list" << endl;
    else
    {
         list[length] = insertItem;   //insert the item at the end
         length++;   //increment the length
    }
} //end insertEnd

template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
    if (location < 0 || location >= length)
        cerr << "The location of the item to be removed "
             << "is out of range" << endl;
    else
    {
        for (int i = location; i < length - 1; i++)
            list[i] = list[i+1];

        length--;
    }
} //end removeAt

template <class elemType>
void arrayListType<elemType>::retrieveAt
                             (int location, elemType& retItem) const
{
    if (location < 0 || location >= length)
        cerr << "The location of the item to be retrieved is "
             << "out of range." << endl;
    else
        retItem = list[location];
} //end retrieveAt


template <class elemType>
void arrayListType<elemType>::replaceAt
                          (int location, const elemType& repItem)
{
    if (location < 0 || location >= length)
        cerr << "The location of the item to be replaced is "
             << "out of range." << endl;
    else
        list[location] = repItem;

} //end replaceAt

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

template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item) const
{
    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>::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>
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>
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; 
}


#endif  




next ill post what main looks like
main:
Lines of code that changed
line 34, used myBubbleSort member function
line 40, removed for loop and used print function

line 71, used myBubbleSort member function
line 77, removed for loop and used print function



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
#include <iostream>                                       //Line 1
#include <string>                                         //Line 2
#include "arrayListType.h"                                //Line 3

using namespace std;                                      //Line 4

int main()                                                //Line 5
{                                                         //Line 6
    arrayListType<int> intList(100);                      //Line 7
    arrayListType<string> stringList;                     //Line 8

    int number;                                           //Line 9

    cout << "List 10: Enter 5 integers: ";                //Line 10

    for (int counter = 0; counter < 5; counter++)         //Line 11
    {                                                     //Line 12
        cin >> number;                                    //Line 13
        intList.insertAt(counter, number);                //Line 14
    }                                                     //Line 15

    cout << endl;                                         //Line 16
    cout<<"List 19: The list you entered is: ";           //Line 17
    intList.print();                                      //Line 18
					                  //Line 19
    cout << "Line 20: Enter the item to be deleted: ";    //Line 20
    cin >> number;                                        //Line 21
    intList.remove(number);                               //Line 22
    cout << "Line 23: After removing " << number
         << ", the list is:" << endl;			  //Line 23


    // Sorting the integers in numerical order
	intList.myBubbleSort(5);

    cout << "\n";

    // Printing out the integers in numerical order

		intList.print();
 

	cout << "\n";

						          //Line 25

    string str;                                           //Line 26

    cout << "Line 27: Enter 5 strings: ";                 //Line 27

    for (int counter = 0; counter < 5; counter++)         //Line 28
    {                                                     //Line 29
        cin >> str;                                       //Line 30
        stringList.insertAt(counter, str);                //Line 31
    }                                                     //Line 32

    cout << endl;                                         //Line 33
    cout << "Line 34: The list you entered is: " << endl; //Line 34
    stringList.print();                                   //Line 35
    cout << endl;                                         //Line 36

    cout << "Line 37: Enter the string to be deleted: ";  //Line 37
    cin >> str;                                           //Line 38
    stringList.remove(str);                               //Line 39
    cout << "Line 40: After removing " << str
         << ", the list is:" << endl;                     //Line 40
				                          //Line 42


    // Sorting the strings in alphabetical order
	stringList.myBubbleSort(5);

    cout << "\n";

    // Printing out the strings in alphabetical order

       stringList.print();


	cout << "\n";

    return 0;                                             //Line 43
}                                                         //Line 44
Gkneeus,

Thank you so much for guiding me through this. I was working all morning with trying to define my bubble sort in the header file like you did and I was slowly working my way through it when you polished everything off for me. Although what you gave me compiles and runs correctly, I incorporated it into my code but after sorting the objects it's repeating one of them.

http://i258.photobucket.com/albums/hh243/Jangoon/program_zps23c2d6ec.jpg

Like so, after deleting 8, 2 gets repeated in the output after sorting. As well as goodbye getting repeated after deleting hey.

I'm going to try and work through this in the header before checking back here to see if your code is revised at all. Again, thank you for the help!
That's weird I came across the same problem the first time i ran your program. then I took out the for loops and placed in the print functions everything was working fine after multiple tests so I didn't mention anything about that in my posts. it was late yesterday night and I was getting tired so I didn't check out the code if you need more help just post back here.
I'm not quite sure which for loops you're talking about. Do you mean replacing part of my bubble sort function with the print function that the author already has in the header?
you had to For loops on main. you had a for loop for each object you created to display the integers in numerical order, I removed the for loop and the code inside and just inserted the print function instead.
Just read this because your topic said "bubble sort".

...and I'm sorry to read that you are learning using one of Malik's books.

It's trash. You should find a better book. (Don't get anything by Schildt either.)


Your sort looks okay to me. If you are seeing duplicates, they are coming from somewhere else... (Sorry, tl;dr, hopefully Gkneeus was able to help you with the loops.)

A minor suggestion on the bubble sort:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template <class elemType>
void arrayListType<elemType>::myBubbleSort( int n)
{
    bool flag = 0;
    do
    {
        flag = 0;
        n--;
        for( int i = 0; i < n; i++)
        {
            if(list[i] > list[i+1])
            { 
				
                elm = list[i]; 
                list[i] = list[i + 1];
                list[i + 1] = elm;
                flag = 1;
            }
        }
    }
	while(flag);
}

This is a basic optimization for bubble sort, since you don't need to keep looking at elements that are already in their final place.

Hope this helps.
Gkneeus, I have already replaced those for loops with just the print function and I'm still getting the same output which is why I'm confused.

Duoas, I've even used your version of my bubble sort with the same result.

I'm using original source code from the author (who I agree, is awful by the way) so I even tried creating both the header and the cpp on this computer and pasting everything in to no avail.

http://i258.photobucket.com/albums/hh243/Jangoon/output_zpsfb2f4814.jpg

Like so.
I don't know if I have the time to dink with it ATM, but if I can I'll take a look at it.

The problem isn't in the bubble sort.

(It's probably in the list implementation.)
in your removeAt function your just copying a value.

1
2
        for (int i = location; i < length - 1; i++)
                list[i] = list[i + 1];
After messing around a bit with his removeAt function I got it to stop repeating a value when it prints out, but it's deleting the wrong value as well. Such as when I enter 50 40 30 20 10 and I tell it to remove 30, it's removing the 50 instead. I really hate manipulating other people's work but it's good practice I guess.
I guess I'm just not understanding how to change that removeAt function's for loop to make it do what I'm trying to do. I've tinkered with it for a few days and am still unable to make it print out the value I'm deleting without copying a value.

How would I go about doing this?
Pages: 12