This program is designed to allocate dynamic memory using pointers and sort them ascending and descending. The problem is it will not sort the pointers by value. For example if I enter -1 2 3 -6 8. The ascending list will be -1 2 3 -6 8, and descending list will be 8 -6 3 2 -1. I would like it to be -6 -1 2 3 8 and vice-versa for the ascending and descending list respectively. I'm sure its something simple, but I just cannot seem to straighten the problem out.
If you scroll down to where my print function is, that is where i pass in "pointerArray" which is a double pointer. I also have "dataArray" being passed in, but it is unused for now. I want to sort "pointerArray" seperately from "dataArray."
Please help, and ask questions if I need to be more clear.
Code:
#include<iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
#include <new>
using std::bad_alloc;
#include<cstdlib>
#define int_array 1
#define pointer2int_array 2
// Prototype declarations
int getSize();
void* getArray( int &howMuch, int typeOf );
void populateArray( int num_of, int* ptr );
void printArray( int num_of, int** ptr, int*ptr2 );
/////////////////////////// -- main -- ///////////////////////////////////////////////////////
int main()
{
int SIZE = getSize();
int* dataArray = ( int*) getArray(SIZE, int_array );
int** pointerArray = ( int**)getArray(SIZE, pointer2int_array );
for(int i=0; i<SIZE; i++)
pointerArray[i] = &dataArray[i];
populateArray(SIZE, dataArray);
printArray(SIZE, pointerArray, dataArray); // sort and print array
delete [] dataArray;
delete [] pointerArray;
cout << endl << endl;
return ( 0 );
}
//////////////////////////////// -- end main -- ////////////////////////////////////////////////
/***************************************************************************************************
*
* Function Name: getSize
*
* Purpose: requests an integer from user
*
* Input Parameters: none
*
* Output parameters: none
*
* Return Value: retuns the user entered integer howMuch
*
***************************************************************************************************/
int getSize()
{
int howMuch;
cout << "enter the size of the array : ";
cin >> howMuch;
return howMuch;
}
/***************************************************************************************************
*
* Function Name: getArray
*
* Purpose: dynamically allocates either an array of ints or an array of
* int pointers
*
* Input Parameters: an int howMuch - the size of array to allocate
* an int typeOf indicating the type of array to allocate
*
* Output parameters: none
*
* Return Value: returns a pointer to an int array or an array of int pointers
*
***************************************************************************************************/
void* getArray( int &howMuch, int typeOf )
{
try
{
switch ( typeOf )
{
case int_array:
{
int* p = new int [ howMuch ];
return( p );
}
case pointer2int_array:
{
int** p = new int* [ howMuch ];
return( p );
}
default:
{
cerr << " problem allocating array \n\n";
exit( -666 );
}
}
}
catch ( bad_alloc &memoryAllocationException )
{
cerr << memoryAllocationException.what() << endl;
exit( 666 );
}
}
/***************************************************************************************************
*
* Function Name : populateArray
*
* Purpose : To give values to the array of ints or the array of int pointers, and
* sort the array in ascending value.
* Input Parameters : an int num_of keeps track of the size of the array
* an int* ptr to pass in the dataArray variable
*
* Output parameters : none
*
* Return Value : none
*
***************************************************************************************************/
void populateArray( int num_of, int* ptr )
{
cout << "Enter the values for the array: ";
for(int i=0; i<num_of; i++)
cin >> ptr[i];
}
/***************************************************************************************************
*
* Function Name : printArray
*
* Purpose : sorts and prints out array of values in ascending, descending, and original order.
*
*
* Input Parameters : an int num_of that counts the number of values in the array
* an int** ptr to pass in the pointer values
* Output parameters : none
*
* Return Value : none
*
***************************************************************************************************/
void printArray( int num_of, int** ptr, int* ptr2 ) \\ sort & print
{
for(int i=0; i<num_of; i++ )
{
int smallestIndex = i;
for(int location = i; location <num_of; location++ )
if( ptr[location] > ptr[smallestIndex])
smallestIndex = location;
int *temp = ptr[smallestIndex];
ptr[smallestIndex] = ptr[i];
ptr[i] = temp;
}
cout << "\nDescending List: ";
for ( int i = 0 ; i < num_of; i++ ) // print out values
cout << *ptr[i] << " ";
}