I have been trying to code a bubble sorting program in which the user inputs the array size and then the computer generates random numbers for all of the array's values. After that, the program asks the user if they want the array to be sorted in an ascending or descending fashion. After that, it runs through the bubble sort. I am getting the error:
Undefined symbols for architecture x86_64: "ArraySorter(int, int)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
I know it is a linker error, but I can't figure out what. Can anyone explain my error and how to fix it? Here is the code:
#include <iostream>
#include <ctime> // for time()
#include <cstdlib> // for srand() and rand()
#include <algorithm>// for swap()
using namespace std;
typedef bool (*pfPointer) (int, int);
void ArraySorter(int, int, pfPointer);
bool Ascending(int, int);
bool Descending(int, int);
int main() {
cout << "Enter the size of the array: ";
int nArraySize;
cin >> nArraySize;
int *pnArray = new int [nArraySize];
srand(static_cast<int>(time(0)));
cout << "Do you want your array to sort in ascending or descending order (a/d)? ";
char nUserSort;
cin >> nUserSort;
cout << '\n';
if (nUserSort == 'a')
ArraySorter(pnArray[nArraySize], nArraySize, Descending); //This allows the user to decide whether they want an ascending or descending function
else
ArraySorter(pnArray[nArraySize], nArraySize, Ascending);
//This prints out the array
for(int qqq = 0; qqq < nArraySize - 1; qqq++)
{
cout << pnArray[qqq] << " || ";
}
cout << pnArray[nArraySize - 1] << endl;
delete [] pnArray;
return 0;
}
void ArraySorter(int pnArray[], int nArraySize, pfPointer pComparison)
{
//Assigns a random number to each value in the array pnArray
for (int iii = 0; iii< nArraySize; iii++)
{
pnArray[iii] = rand();
}
// Will change the starting index of the array each time the loop runs through
for (int nStartIndex = 0; nStartIndex < nArraySize; nStartIndex++)
{
// Declares a variable to find the location of the smallest variable in the array
int nSmallestIndex = nStartIndex;
// Runs through each array value and finds the smallest one, then registers it
for (int nCurrentIndex = nSmallestIndex +1; nCurrentIndex < nArraySize; nCurrentIndex++)
{
// If the current array value is less than the starting array value, it will log it's location in the array in nSmallestIndex
if (pComparison(pnArray[nCurrentIndex] , pnArray[nSmallestIndex]))
{
nSmallestIndex = nCurrentIndex;
}
}
//Switches the smallest value with the starting value
swap(pnArray[nStartIndex], pnArray [nSmallestIndex]);
}
}
bool Ascending(int nValue1, int nValue2)
{
return nValue1 < nValue2; //Will sort the variables from smallest to greatest
}
bool Descending(int nValue1, int nValue2)
{
return nValue1 > nValue2; //Will sort the variables from greatest to smallest
}
P.S. I am running XCode 6.1.1
The function prototype of ArraySorter
takes (int, int, pfPointer)
, but the function definition takes (int [], int, pfPointer)
.
Change the prototype of ArraySorter
to take (int [], int, pfPointer)
, and the change the calls to ArraySorter
to ArraySorter(pnArray, nArraySize, Descending)
rather than ArraySorter(pnArray[nArraySize], nArraySize, Descending)
.