0

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;
}
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • 2
    You declare quickSelect with one set of parameters, then define a function with a different set of parameters. `int quickSelect(int, vector* arr);` vs `int quickSelect(int k, vector &arr)`. One of these things is not like the other. – n. m. could be an AI Apr 09 '21 at 05:20
  • 3
    In general one almost never needs a pointer to a container. – Some programmer dude Apr 09 '21 at 05:21
  • So I fixed what @n.'pronouns'm. recommended. It got rid of the error however when I go to print the values of the vector in my quickSelect function using the for loop at the top to see if the values got passed, I get a bunch of garbage, and hence the function wont work properly. How can I fix that issue? – Heisenberg57 Apr 09 '21 at 05:31
  • @Heisenberg57 Your code has lot's of _undefined behavior_, accessing the vector variable out of bounds. Hence to tell ,why your code behaves like it does, is pretty futile IMO. – πάντα ῥεῖ Apr 09 '21 at 05:35
  • Also, you use external space for your partitioning, which is not in the spirit of quickselect. See [wikipedia](https://en.wikipedia.org/wiki/Quickselect#:~:text=In%20computer%20science%2C%20quickselect%20is,element%20in%20an%20unordered%20list.&text=Quickselect%20uses%20the%20same%20overall,or%20greater%20than%20the%20pivot.) for inspiration. – BitTickler Apr 09 '21 at 05:38

0 Answers0