Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi. Can someone help me to fix my code? My code is below. I finished the code of

ID: 3753787 • Letter: H

Question

Hi. Can someone help me to fix my code? My code is below. I finished the code of part one, however, the code of part two only can use swap and getIndexOfMax which I did in part one. Here are the guidelines.  

*****************************

#include <iostream>

using namespace std;

// function to return maximum element using recursion

int getIndexOfMax(int nums[], int length)

{

if (length == 1)

return nums[0];

return max (nums[length - 1], getIndexOfMax (nums, length - 1)); //?

}

void maxSort(int nums[], int length)

{

int maxIndex = getIndexOfMax (nums, length);

if (maxIndex != nums[length - 1])

swap(nums[maxIndex], nums[length - 1]);

maxSort(nums, length);

}

int main()

{

int nums[] = { 10, 40, 50, 60, -40, 30, 20, 90, -10 };

int length = sizeof (nums) / sizeof (nums[0]);

cout << "The index of the maximum value in an array: " << getIndexOfMax (nums, length) << " ";

maxSort(nums,length);

cout << "The array's maxSort are: ";

for (int i = 0; i < length; i++)

cout << nums[i] << " ";

return 0;

}

1) Write code for a recursive function called int getlndexOfMax(int numsll, int length) that returns the index of the maximum value in an array. Assume that length is1. The base case will be when length equals 1. No loops or helper functions allowed. 2) Write the code for a recursive function maxSort(int nums], int length) that calls getlndexOfMax on the array, swaps the element at that index with the element at index length-1, then recursively calls itself on a smaller subarray. The base case will be when length equals 1. No loops allowed. The only helper functions you'll call are swap (unless you code the swap inline) and getlndexOfMax.

Explanation / Answer

Solution:

you haven't coded the max function in the code and along with that in the maxSort we have to call the subarrays recursively, so the maxSort in the maxSort function will be called as

maxSort(nums, length - 1);

and there will be a base case as well

if(length==1)

return;

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote