The assignment is to write a C++ function using "pointer notation" that will write out the elements of an array of int in reverse order.

The problem I'm having is with the sortAscend function and what the parameters should be to accept an array. I'm getting an error cause I'm passing an object and the function expects an int pointer.

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

    void sortAscend(int *list, int length);
        //Function to sort the array in Ascending order
        //Postcondition: Elements of the list are sorted 
        //                 in Ascending order

    void insertEnd(int insert);
        //Function to insert elements at the end of list
        //Postcondition: list[length] = insert; length++;

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

    ~arrayList();
        //destructor
        //Deallocate the memory occupied by the array.

private:
    int *list;        //array to hold the list elements
    int length;        //variable to store the length of the list
    int maxSize;    //variable to store the maximum size of the list
};
#include "arrayList.h"

using namespace std;

void arrayList::print() const
{
    for (int i = 0; i < length; i++)
        cout << list[i] << " ";
    cout << endl;
} //end print function

void arrayList::sortAscend(int *list, int length)
{
    for(int i = 0; i < length; i++)
    {
        for(int j = 0; j < length - i; j++)
        {
            if(list[i] > list[j + 1])
            {
                int temp = list[j]; //swap 
                list[j] = list[j + 1];
                list[j + 1] = temp;
            }

        }

    }

} //end sortAscend function

void arrayList::insertEnd(int insert)
{
    if (length >= maxSize) //the list is full
        cout << "Cannot insert in a full list." << endl;

    else
    {
        list[length] = insert; // insert the item at the end of the list
        length++; //increment the length
    } 
} //end function insert

arrayList::arrayList(int size)
{
    if (size <= 0)
    {    cout << "The array size must be positive. Creating "
             << "an array of the size 10." << endl;

        maxSize = 10;
    }
    else
        maxSize = size;

    length = 0;
    list = new int[maxSize];
} //end constructor

arrayList::~arrayList()
{
    delete [] list;
} //end destructor
#include <iostream>

#include "arrayList.h"

using namespace std;

int main()
{
    arrayList list(10);

    int number;

    cout << "Enter 10 integers: ";

    for (int count = 0; count < 10; count++)
    {
        cin >> number;
        list.insertEnd(number);
    }

    cout << "Before sorting, array[10]: ";

    list.print();

    cout << endl;

    list.sortAscend(*list, 10);

}

Since you are calling the function of the object (ie the member function) you don't need to pass anything to the function, since the object contains all that is needed to sort its elements. Just don't forget to update the length of the internal list and it should be fine.

Also write another function which checks whether the Object contains atleast one element before sorting so as to avoid crashes.

Something like:

void arrayList::sortAscend ()
{
    // here length and list are the member variables of the object
    // & and hence can be directly accessed
    for(int i = 0; i < length; i++) 
    {
        for(int j = 0; j < length - i; j++)
        {
            if(list[i] > list[j + 1])
            {
                int temp = list[j]; //swap
                list[j] = list[j + 1];
                list[j + 1] = temp;
            }            
        }        
    }    
} //end sortAscend function

Oh, and btw, your sorting logic is flawed. A simple fix would be:

for(int i = 0; i < length - 1; i++)
    {
        for(int j = i + 1; j < length; j++)
        {
            if(list[i] > list[j])
            {
                int temp = list[i]; //swap
                list[i] = list[j];
                list[j] = temp;
            }
        }
    }

Well from what I can dig out of my very shallow pool of knowledge it seems that in your prototype you are telling it that you want it to accept a pointer but when you call the function you are passing by value instead of by reference IE. &list

Thanks for the help. I just deleted the function with the two parameters. I got the code for that bubble sort algorithm straight out of the book so I am surprised it was flawed, but you were right cause when I compiled I get an error then I ran it your way and it worked great. Thanks again.

It would really be interesting to know which book you are referring.

A book with flawed algorithm of such a simple sort is not a book worth reading....

C++ Programming: Program Design Including Data Structures 3rd ed. by D.S. Malik

I am going to copy the algorithm again maybe I screwed it up.

void bubbleSort(elemType list[], int length)
{
       for (int i = 1; i < length; i++)
       {
              for (int j = 0; j < length - i; j++)
              {
                     if (list[j] > list[j + 1])
                     {
                             elemType temp = list[j];
                             list[j] = list[j + 1];
                             list[j + 1] = temp;
                     }
              }
       }
}

So I went back and looked at my first algorithm and I had in the if statement (list >
and it should have been (list[j] >

I like your algorithm because it is a little simpler and more clear. Thanks again for the help.

Reply to ya later.

This article has been dead for over six months. Start a new discussion instead.