简体   繁体   中英

recursive C++ function that takes as input an array of integers n then prints the maximum and the minimum value in the array without using any loops

Hello I have a homework question I am stuck in...any hint or tips would be appreciated. the question is:

Write a short recursive C++ function that takes as input an array of integers of size n then prints the maximum and the minimum value in the array without using any loops.

I have the following so far:

/* Precondition: the function is given the array, first Index and last index, takes two int variable maxim, minim set to 0
                 also takes a bool isFirst. Not sure if I really need the bool isFirst to display the min and max values from the
                 function it self.
   Postcondition: the function displays the maximum and the minimum in the array.
*/
void findMaxMin(int A[], int firstIndex, int lastIndex, int &maxim, int &minim, bool isFirst)
{
    if (firstIndex == lastIndex) // Base case one element in the array
    {
        maxim = A[firstIndex];
        minim = A[firstIndex];
        return;
    }
else
{
    int myMin = 0;
    int myMax = 0;

findMaxMin(A, firstIndex, (lastIndex-1), myMin, myMax,  false);

        if(A[lastIndex] < myMin)
            myMin = A[lastIndex];

        if (A[lastIndex] > myMax)
            myMax = A[lastIndex];

            maxim = myMax;
            minim = myMin;

cout<<"mymin now is: "<< myMin<<endl;
cout<< "mymax now is: "<<myMax<<endl;

        //if (isFirst)
        //    cout<< " my min is" << minim << " my max is"<< maxim<< endl;


}
}

however this is not working as it should be.. not sure where the problem is.

the divide and conquer method attempt 1: this works only problem is that it prints the minimum and maximum on every recursive call it makes but the code seems to be correct. Solution to the cout problem I found was to print from the main but the question specifies that the print be made from inside the function; but could not find a way to print from the function only the last comparison; it always prints on every recursive call it makes

void MaxMin(int A[], int firstIndex, int lastIndex, int &maxim, int &minim)
{

    if(firstIndex == lastIndex) // base case one element
    {
        maxim = A[firstIndex];
        minim = A[firstIndex];
        return;
    }
    else if(firstIndex == lastIndex -1) // base case two element
    {
        if(A[firstIndex] < A[lastIndex])
        {
            maxim = A[lastIndex]; 
            minim = A[firstIndex];
        }
        else
        {
            maxim = A[firstIndex]; 
            minim = A[lastIndex];
        }
     }

    else // there are more than two elements in the array
    {
        int mid = (firstIndex+lastIndex)/2;

        MaxMin(A,firstIndex,mid,maxim,minim);

        int maxim1 =0;
        int minim1 =0;

        MaxMin(A,mid+1,lastIndex, maxim1, minim1);

        if(maxim < maxim1)
        {
            maxim = maxim1;
        }

        if(minim > minim1)
        {
            minim = minim1;
        }
    }

//this is printed on every recursive call.

cout<<"my minimum now is "<<minim<<endl;
cout<<" my maximum now is "<<maxim<<endl;

}

The problem is in that function declaration int &maxim is before int &minim and in the recursive call you put minimum value first. So you need to call:

findMaxMin(A, firstIndex, (lastIndex-1), myMax, myMin, false);

The way you structured your code also firstIndex can only be 0. So you can remove this parameter and replace with 0. Also isFirst is not needed. You can simplify further if you use directly minim , maxim instead of myMin , myMax .

As a hint and not a definete solution. Any loop of the form

for(int loopIndex, loopContinueCondition(loopIndex), loopIncrement(loopIndex))

can be converted to a recursion of the form

ResultType my_for(int loopIndex, ResultType partialResult, ...) {
    ... //normal loop body
    newPartialResult = ...;
    if (loopContinueCondition(loopIndex)) {
        loopIncrement(loopIndex);
        return my_for(loopIndex, newPartialResult, ...);
    } else
        return newPartialResult;
}

where ... represents the additional data normally available in the loop body.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM