Question: Could you please check what is wrong with this program? The output should be answer is: 4 kSmall answer is: 4 answer is: 11 kSmall

Could you please check what is wrong with this program?

The output should be

answer is: 4 kSmall answer is: 4 answer is: 11 kSmall answer is: 11

-----

#include

#include

#include

using namespace std;

int const ARRAY_SIZE = 6;

int pivotIndex;

int p;

int kSmall(int k, int anArray[], int first, int last)

{

if (k < pivotIndex - first + 1)

return kSmall(k, anArray, first, pivotIndex - 1);

else if (k == pivotIndex - first + 1)

return p;

else

return kSmall(k - (pivotIndex - first + 1), anArray, pivotIndex + 1, last);

}

int main()

{

int testArray1[] = {6,3,1,2,4,5};

int position = 4;

int pivotIndex=4;

//int p=pivotIndex;

cout << "answer is: 4" << endl << "kSmall answer is: " << kSmall(position, testArray1, 0, ARRAY_SIZE -1) << endl;

int testArray2[] = {20,11,12,-45,7,18};

position = 3;

pivotIndex=12;

cout << "answer is: 11" << endl << "kSmall answer is: " << kSmall(position, testArray2, 0, ARRAY_SIZE -1) << endl;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!