My code is below and having a problem to compile the maxSort. Can someone edit m
ID: 3753815 • Letter: M
Question
My code is below and having a problem to compile the maxSort. Can someone edit my code?
#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)); //?
}
// function to sort
void maxSort(int nums[], int length)
{
int maxIndex = getIndexOfMax (nums, length);
if(length==1)
return;
if (maxIndex != nums[length - 1])
swap(nums[maxIndex], nums[length - 1]);
maxSort(nums, length - 1);
}
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-1);
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
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
//Code
#include <iostream>
using namespace std;
// function to return the index of maximum element using recursion
// this will iterate from last element to first element, recursively using length-1
// as the index
int getIndexOfMax(int nums[], int length)
{
//checking for base condition
if(length>1){
//checking if current element is greater than all previous elements
if(nums[length-1]>=nums[getIndexOfMax(nums,length-1)]){
//true, returning index of current element
return length-1;
}else{
//false, moving to check the previous element in the array
return getIndexOfMax(nums,length-1);
}
}
return 0;
}
// function to sort an array of numbers using above method, recursively
void maxSort(int nums[], int length)
{
//checking for base condition
if(length<=0)
return;
//finding the index of maximum value in current array (unsorted part)
int maxIndex = getIndexOfMax (nums, length);
//swapping elements at the found index and at the index length-1
swap(nums[maxIndex], nums[length - 1]);
//sorting previous set of array elements similarly
maxSort(nums, length - 1);
}
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;
}
/*OUTPUT*/
The index of the maximum value in an array: 7
The array's maxSort are: -40 -10 10 20 30 40 50 60 90
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.