Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want to finish this C++ code: Here is the code: ksmallSolution.hpp: #ifndef KSMALL_SOLUTION_HPP #define KSMALL_SOLUTION_HPP void displayArray(int arraySize, int anArray[]); int ksmallPartition(int startIndex, int

I want to finish this C++ code:

Here is the code:

ksmallSolution.hpp:

#ifndef KSMALL_SOLUTION_HPP

#define KSMALL_SOLUTION_HPP

void displayArray(int arraySize, int anArray[]);

int ksmallPartition(int startIndex, int endIndex, int anArray[]);

#endif

ksmallSolution.cpp:

#include

#include

#include "ksmallSolution.hpp"

using namespace std;

int ksmallPartition(int startIndex, int endIndex, int anArray[]) {

int pivotIndex = 0;

// YOUR CODE HERE

return pivotIndex;

}

void displayArray(int arraySize, int anArray[]) {

cout << setfill('0');

for(int i=0; i

cout << setw(3) << i << " ";

}

cout << endl << setfill(' ');

for(int i=0; i

cout << setw(3) << anArray[i] << " ";

}

cout << endl;

}

Main.cpp:

#include

#include "ksmallSolution.hpp"

using namespace std;

int main() {

cout << "Test 01: odd number elements" << endl;

int test01[] = {2, 4, 6, 5, 8};

int testSize = sizeof(test01)/sizeof(test01[0]);

cout << "pivotIndex:" << ksmallPartition(0, testSize-1, test01) << endl;

displayArray(testSize, test01);

cout << endl;

cout << "Test 02: even number elements" << endl;

int test02[] = {8, 7, 56, 78, 4, 6, 2, 5};

testSize = sizeof(test02)/sizeof(test01[0]);

displayArray(testSize, test02);

cout << "pivotIndex:" << ksmallPartition(0, testSize-1, test02) << endl;

displayArray(testSize, test02);

cout << endl;

cout << "Test 03: pivot already correct" << endl;

int test03[] = {2 , 7 , 56, 78, 4, 6, 8, 5 , 8};

testSize = sizeof(test03)/sizeof(test01[0]);

displayArray(testSize, test03);

cout << "pivotIndex:" << ksmallPartition(0, testSize-1, test03) << endl;

displayArray(testSize, test03);

cout << endl;

cout << "Test 04: empty array" << endl;

int test04[] = {};

testSize = 0;

displayArray(testSize, test04);

cout << "pivotIndex:" << ksmallPartition(0, testSize-1, test04) << endl;

displayArray(testSize, test04);

int test05[] = {487, 658, 418, 564, 282, 804, 41, 300, 929, 467, 997, 215, 70, 477, 294, 954, 188, 49, 320, 673, 439, 590, 12, 992, 787, 318, 926, 636, 920, 272, 368, 726, 134, 288, 862, 65, 881, 417, 916, 101, 901, 583, 964, 331, 146, 792, 548, 532, 86, 133, 530, 643, 511, 816, 969, 394, 602, 231, 860, 107, 326, 530, 441, 13, 736, 245, 387, 587, 178, 322, 964, 389, 976, 412, 745, 801, 270, 852, 853, 878};

testSize = sizeof(test05)/sizeof(test05[0]);

cout << "Your Solution Code is: " << ksmallPartition(0, testSize-1, test05) << endl;

}

You are to alter the ksmallSolution.cpp file. You are to alter ONLY the function ksmallPartition where it state "//Your Code Here".

The main file performs four tests. When the ksmallPartition function is correctly working, the test should result in the output similar to:

Test 01: odd number elements 000 001 002 003 004 4 6 2 5 8 pivotIndex:1 000 001 002 003 004 2 4 6 5 8 Test 02: even number elements 000 001 002 003 004 005 006 007 8 7 56 78 4 6 2 5 pivotIndex:5 000 001 002 003 004 005 006 007 6 7 5 2 4 8 78 56 Test 03: pivot already correct 000 001 002 003 004 005 006 007 008 2 7 56 78 4 6 8 5 8 pivotIndex:0 000 001 002 003 004 005 006 007 008 2 7 56 78 4 6 8 5 8 Test 03: empty array pivotIndex:-1

After ksmallPartition has run on the array, it should return a pivotIndex value. The following must be true:

  • The element originally at index position 0 (the pivot value) is now at index position pivotIndex.
  • All elements at index positions less than the pivotIndex are less than or equal to the pivot value.
  • All elements at index positions greater than the pivotIndex are greater than the pivot value.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

ISBN: 0764549634, 9780764549632

More Books

Students also viewed these Databases questions

Question

4. Not sure? Ask them.

Answered: 1 week ago