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