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

The maximum-valued element of an integer -valued array can be recursively calcul

ID: 3817690 • Letter: T

Question

The maximum-valued  element of an integer -valued  array can be recursively calculated as follows:

If the array has a single element , that is its maximum (note that a zero-sized array has no maximum)

Otherwise, compare the first element with the maximum of the rest of the array -- whichever is larger is the maximum value .

Write an int  function named  max that accepts an integer  array , and the number of elements in the array and returns the largest value in the array . Assume the array has at least one element .

Needs to use recursion and be written in c++

Explanation / Answer

#include <iostream>

using namespace std;
int maxfun(int[],int);//declaration of function
int main()
{
int array[100];//array of 100 values taken, can be increased
int num,i,largest_num;//variable declaration.num is the no of elements to be added in the array and largest_num is the number that takes return value from the function.
cout << "Enter no Elements in an Array:" << endl;//taking input from the user ,asking for no of elements
cin>>num;
cout<<"Enter "<<num<<" numbers :";
for(i=0; i<num; i++)//loop started for input of array elements
{
cin>>array[i];
}
largest_num=maxfun(array,num);//function called in order to find the largest number in the array and input is taken in largest_num.
cout<<"largest number is:"<<largest_num;//ouput printed.
}

int maxfun(int array[],int num)//function definition
{
if(num==1)//now see if num is equal to 1 means only 1 element is present in the array,so we returned that one element.
return array[num-1];
else if(num==0) //now this is the check that will take us out from the recursion.what i am doing is starting from the last and comparing it with its neighbour.
//suppose we have an array 1,2,3....now comparing 3 with 2 since 3 is larger so we will interchange the position
//hence new array form will be 1,3,2...and also we will decrease the num by num--
//and hence recursion will be called, and at the end what we will get at 0 position is the largest number
//hence returning the largest position..
//now a case will be there that is what is last number is already small eg:-1,3,2
//in that case a check is already applied ,that is not to interchange the element of array and just decrement the num and again calling recursion.
return array[num];//returning element at 0 position only when num has a value of zero
else
{
if(array[num-1]>array[num-2])//here we are comparing as stated above..if last is largest interchange.
{
int temp=array[num-1];//temp is a variable used to interchange the elements between the array.
array[num-1]=array[num-2];
array[num-2]=temp;
num--; //after interchanging we are decrementing num and again calling function.
maxfun(array,num);//calling of our function :this is known as recursion calling of function by itself

}
else//now as mentioned above if element is not larger then not to interchange.
{
num--;//so just to decrement the count
maxfun(array,num);//and calling the function again

}
}


}

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