I am trying to finish a program to perform the quickselect algorithm and am using vectors since the elements are randomly generated. When I run my code as it is right now I get the error:
undefined reference to quickSelect(int, std::vector<int, std::allocator<int> >*)
When I remove all "&" and "*" symbols from the vector declarations and reference, and try to print the values of my vector in my quickSelect function, I get a bunch of random values that are not accurate. When I print the values of the vector in the main function I get proper values. This leads me to believe that there is something going on when I am passing the vector to the function. How can I have the proper values pass on to the function quickSelect?
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <vector>
using namespace std;
int k, l, p, i, x, j, res;
int val;
int Pivot1 = rand()%50;
int Pivot2 = rand()%50;
int quickSelect(int, vector<int>* arr);
vector<int> arr;
int main(){
/*
cout << "p: ";
cin >> p;
cout << "k: ";
cin >> k;
cout << "l: ";
cin >> l;
*/
l = 50;
vector<int> arr(l);
srand(time(0));
for (i=0; i<l; i++){
x = (rand()%50);
arr[i] = x;
}
for (i=0; i<l; i++){
for (j=i; j<l; j++){
if (j != i){
if (arr[i] == arr[j]){
val = true;
}
}
if (val == true){
arr[i] = arr[i]+1;
}
}
val = false;
}
quickSelect(2, &arr);
}
int quickSelect(int k, vector<int> &arr){
static int counter = 0;
counter++;
vector<int> small;
vector<int> large;
int Pivot1 = arr[rand()%50];
l = 50;
for (i=0; i<l; i++){
cout << &arr[i] << endl;
}
// cout << arr[0] << endl;
// cout << arr[16] << " " << "life" << endl;
for(int i=0; i<l; i++){
if (arr[i] < Pivot1) {
small.push_back(arr[i]);
}
if (arr[i] > Pivot1) {
large.push_back(arr[i]);
}
}
if(k <= large.size()) {
return quickSelect(k, large);
} else if (k > large.size() + 1){
return quickSelect((k - large.size() - 1), small);
} else {
return Pivot1;
}
return 0;
}