Question
Could you please what is wrong with this program? The output should be answer is: 4 kSmall answer is: 4 answer is: 11 kSmall answer
Could you please 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started