The purpose of my program is to:
- Collect the size that the user would like the array to be.
- Collect the user's integer input for each element.
- Use pointers to refer to the array and the array size (required as a pointer assignment).
- Call a function to use a selective sort to sort the array in ascending order (using pointers as parameters) while also displaying the result after each sorting iteration
- Call another function to find the median of the sorted array (using pointers as parameters) and to display the median.
However, despite using what I thought was the correct syntax for pointers, I keep getting undefined reference errors for both of my functions, resulting in "ID returned 1 exit status".
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <math.h>
using namespace std;
void medSelectiveSort(int*, int*);
int medDetermination(int*, int*);
int main()
{
int arraySize;
int *arraySizePtr = &arraySize;
int median;
cout << "How large would you like the array to be?" << endl;
cin >> arraySize;
if (arraySize <= 10)
cout << "Processing..." << endl;
else if (arraySize > 10)
{
cout << "That is too large. It will be reduced to 10." << endl;
arraySize = 10;
}
int arrayMedian[arraySize];
int *arrayMedianPtr = arrayMedian;
int inverseLoopCounter = 0;
for (arraySize; arraySize > 0; arraySize--)
{
cout << "Please enter element number " << inverseLoopCounter << "." << endl;
cin >> arrayMedian[inverseLoopCounter];
inverseLoopCounter++;
}
medSelectiveSort(arraySizePtr, arrayMedianPtr);
median = medDetermination(arraySizePtr, arrayMedianPtr);
cout << "The median is " << median << "." << endl;
return 0;
}
void medSelectiveSort(int arraySizePtr, int arrayMedianPtr[])
{
int startScan, minIndex, minValue;
int arraySizePtrPlaceHolder = arraySizePtr;
for (startScan = 0; startScan < (arraySizePtrPlaceHolder - 1); startScan++)
{
minIndex = startScan;
minValue = arrayMedianPtr[startScan];
for(int index = startScan + 1; index < arraySizePtrPlaceHolder; index++)
{
if (arrayMedianPtr[index] < minValue)
{
minValue = arrayMedianPtr[index];
minIndex = index;
}
}
arrayMedianPtr[minIndex] = arrayMedianPtr[startScan];
arrayMedianPtr[startScan] = minValue;
cout << "" << endl;
cout << "Sorting..." << endl;
for(int i = 0; i < arraySizePtrPlaceHolder; i++)
{
cout << arrayMedianPtr[i] << " ";
}
cout << endl;
}
cout << "" << endl;
cout << "This array of integers has been sorted in ascending order using the selection sort." << endl;
cout << "" << endl;
}
int medDetermination(int arraySizePtr, int arrayMedianPtr[])
{
int finalMedian;
int arraySizePtrPlaceHolder = arraySizePtr;
int medCalculator1;
int medCalculator2;
int medCalculator3;
if (arraySizePtrPlaceHolder % 2 == 0)
{
medCalculator1 = arraySizePtrPlaceHolder / 2;
medCalculator2 = medCalculator1 + 1;
finalMedian = arrayMedianPtr[medCalculator1] + arrayMedianPtr[medCalculator2] / 2;
}
else
{
medCalculator1 = arraySizePtrPlaceHolder % 2;
medCalculator2 = arraySizePtrPlaceHolder + medCalculator1;
medCalculator3 = medCalculator2 / 2;
finalMedian = arrayMedianPtr[medCalculator3];
}
return finalMedian;
}
Aucun commentaire:
Enregistrer un commentaire