### Pointer Array Help

I need to dynamically allocate the donations array in my code, and ask the user to input the value of the array.
Here is the original code
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````// This program shows the donations made to the United Cause // by the employees of CK Graphics, Inc. It displays // the donations in order from lowest to highest // and in the original order they were recieved. #include using namespace std; // Function prototypes void arrSelectSort(int *[], int); void showArray(const int [], int); void showArrPtr(int *[],int); int main() { const int NUM_DONATIONS = 15; // number of donations // An array containing the donation amounts. int donations[NUM_DONATIONS] = {5, 100, 5, 25, 10, 5, 25, 5, 5, 100, 10, 15, 10, 5, 10}; int *arrPtr[NUM_DONATIONS]; // an array of pointers to int. // each element of arrPtr is a pointer to int. // make each element point to an element in the donation array for (int count = 0; count < NUM_DONATIONS; count++) arrPtr[count] = &donations[count]; //sort the elements of the array to pointers arrSelectSort(arrPtr, NUM_DONATIONS); //Display the donations using the array of pointers. This //will display them in sorted order. cout << "The donations, sorted in ascending order are: \n"; showArrPtr(arrPtr, NUM_DONATIONS); //display the donations in their original order. cout << "The donations, in their original order, are: \n"; showArray(donations, NUM_DONATIONS); return 0; } //*************************************************************************** // Definition of function arrSelectSort. * // This function performs an acending order selection sort on * // arr, which is an array of pointers. Each element of arr * // points to an element of a second array. After the sort, * //arr will point to the elements of the second array in * //ascending order. * //**************************************************************************** void arrSelectSort(int *arr[], int size) { int startScan, minIndex; int *minElem; for (startScan=0; startScan < (size-1); startScan++) { minIndex = startScan; minElem = arr[startScan]; for (int index = startScan + 1; index < size; index++) { if (*(arr[index]) < *minElem) { minElem=arr[index]; minIndex = index; } } arr[minIndex] = arr[startScan]; arr[startScan] = minElem; } } //*************************************************************************** // Definition of function showArray. * // This function displays the contents of arr. size is the * // number of elements. * //**************************************************************************** void showArray(const int arr[], int size) { for (int count = 0; count < size; count++) cout << arr[count] << " "; cout << endl; } //*************************************************************************** // Definition of function showArrPtr * // This function displays the contents of the array pointed to * // by arr. size is the number of elements. * //**************************************************************************** void showArrPtr(int * arr[], int size) { for (int count = 0; count < size; count++) cout << *(arr[count]) << " "; cout << endl; }``````

Here is my code with changes
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104`` ``````// This program shows the donations made to the United Cause // by the employees of CK Graphics, Inc. It displays // the donations in order from lowest to highest // and in the original order they were recieved. #include #include using namespace std; // Function prototypes void arrSelectSort(int *[], int); void showArray(const int [], int); void showArrPtr(int *[],int); int main() { int numDon; cout << "How many numbers are in the array? " << endl; cin >> numDon; // An array containing the donation amounts. int *donations= new int [numDon]; for (int i=0;i> donations[i]; } // each element of arrPtr is a pointer to int. // make each element point to an element in the donation array //sort the elements of the array to pointers arrSelectSort(&donations, numDon); //Display the donations using the array of pointers. This //will display them in sorted order. cout << "The donations, sorted in decending order are: \n"; showArrPtr(&donations, numDon); //display the donations in their original order. cout << "The donations, in their original order, are: \n"; showArray(donations, numDon); return 0; } //*************************************************************************** // Definition of function arrSelectSort. * // This function performs an decending order selection sort on * // arr, which is an array of pointers. Each element of arr * // points to an element of a second array. After the sort, * //arr will point to the elements of the second array in * //ascending order. * //**************************************************************************** void arrSelectSort(int *arr[], int size) { int startScan, minIndex; int *minElem; for (startScan=0; startScan < (size-1); startScan++) { minIndex = startScan; minElem = arr[startScan]; for (int index = startScan + 1; index < size; index++) { if (*(arr[index]) > *minElem) { minElem=arr[index]; minIndex = index; } } arr[minIndex] = arr[startScan]; arr[startScan] = minElem; } } //*************************************************************************** // Definition of function showArray. * // This function displays the contents of arr. size is the * // number of elements. * //**************************************************************************** void showArray(const int arr[], int size) { for (int count = 0; count < size; count++) cout << arr[count] << " "; cout << endl; } //*************************************************************************** // Definition of function showArrPtr * // This function displays the contents of the array pointed to * // by arr. size is the number of elements. * //**************************************************************************** void showArrPtr(int * arr[], int size) { for (int count = 0; count < size; count++) cout << *(arr[count]) << " "; cout << endl; }``````

How can i make it pass the array
if you want to pass an array as a parameter of a function use this
 ``1234567891011`` ``````void my_function(int my_Array[size]); //you can also just write my_Array[] without size void myfunction(int my_Array[size]) //same is the line above { blablabla code and stuff } int main() { int A[size]; myfunction(A); }``````
Last edited on
It needed to be allocated dynamically. i was having troubles getting a pointer for it. i think i got it figured out now though
Topic archived. No new replies allowed.