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 <iomanip>
#include <iostream>
#include <string>
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;
}
The solution of the above problem can be given as below:



Output Screen:

/*Sourrce code */
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int const ARRAY_SIZE = 6;
/*Function Prototype */
int partition(int Array[], int left, int right);
/*Swap Function */
void swap(int *firstNumber, int *secondNumber)
{
int temp = *firstNumber;
*firstNumber = *secondNumber;
*secondNumber = temp;
}
/*Partition Function */
int partition(int Array[], int left, int right)
{
int x = Array[right], i = left - 1;
for (int j = left; j <= right - 1; j++)
{
if (Array[j] < x)
{
i++;
swap(&Array[j], &Array[i]);
}
}
swap(&Array[i+1], &Array[right]);
return (i+1);
}
int kSmall(int k, int anArray[], int first, int last)
{
/*Mistake - II Bound Condition */
if (k > 0 && k <= ARRAY_SIZE){
/*Mistake - I : PivotElement is never calculated */
int pivotIndex = partition(anArray,first,last);
/*return the pivotElement because that is present
* at its sorted position */
if (k - 1 == pivotIndex )
return anArray[pivotIndex];
/*if position is less than pivot then recurse in left subArray */
if (pivotIndex > k-1)
return kSmall(k,anArray, first, pivotIndex-1);
/*if position is greater than pivot then recurse in right subArray */
else
return kSmall(k , anArray, pivotIndex + 1, last);
}
/*If out of bound position is being entered */
return INT_MAX;
}
int main()
{
int testArray1[] = {6,3,1,2,4,5};
/*Which smallest number to find */
int position = 4;
cout << "answer is: 4" << endl << "kSmall answer is: "
<< kSmall(position, testArray1, 0, ARRAY_SIZE - 1) << endl;
int testArray2[] = {20,11,12,-45,7,18};
/*Which smallest number to find */
position = 3;
cout << "answer is: 11" << endl << "kSmall answer is: "
<< kSmall(position, testArray2, 0, ARRAY_SIZE - 1) << endl;
}
-----------------------------------------------------------------------------------------------
Please like the post and comment for any query related to the problem.
Thanks for liking the post !
Could you please check what is wrong with this program? The output should be answer is:...
The code below is already completed using C++. Could you please explain why the input is 3 : 2 : 3 : 6 : 4 : -1 : 3 : 1 : 0 :? Please let me know really short explanation. #include <iostream> using namespace std ; int binarySearch(const int array[], int numElems, int value) { int first = 0, // First array element last = numElems - 1, // Last array element middle, // Midpoint of search position =...
////****what am i doing wrong? im trying to have this program with constructors convert inches to feet too I don't know what im doing wrong. (the program is suppose to take to sets of numbers feet and inches and compare them to see if they are equal , greater, less than, or not equal using operator overload #include <stdio.h> #include <string.h> #include <iostream> #include <iomanip> #include <cmath> using namespace std; //class declaration class FeetInches { private: int feet; ...
/* * Program5 for Arrays * * This program illustrates how to use a sequential search to * find the position of the first apparance of a number in an array * * TODO#6: change the name to your name and date to the current date * * Created by Li Ma, April 17 2019 */ #include <iostream> using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() {...
Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...
1. What is wrong with the following C++ program? #include <iostream> int main() { a = 4; b = 6; cout << a << "+" << b << "=" << a+b; return 0; 2. What is wrong with the following C++ program? What was its intended output? #include <iostream> using namespace std; int main() { cout << "What is larger? e pi or pi e?" << endl; double ans1 = exp(pi); double ans2 = pi exp(1.); cout << "epi is...
I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() { int var1 = 20 ; int var2 = 30 ; int* ptr1 ; int* ptr2 ; int* temp ; ptr1 = &var1 ; ptr2 = &var2 ; cout << *ptr1 << endl ;...
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
* This program illustrates how to use a sequential search to find the position of the first apparance of a number in an array TODO#6: change the name to your name and date to the current date * * Created by John Doe, April 17 2019 */ #include using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() { //TODO#1: declare an integer array named intList with size of...
Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8, Part 3 * * Author: your name * z-ID: your z-ID * Date: due date of assignment * * This program builds, sorts and prints lists using the quicksort and * merge sort algorithms. */ #include <iostream> #include <iomanip> #include <vector> #include <string> #include "sorts.h" #include "quicksort.h" #include "mergesort.h" using std::cout; using std::fixed; using std::left; using std::setprecision; using std::string; using std::vector; // Data files...