Home > Blockchain >  Recursive function to get the minimum maximum and sum of an array of integers
Recursive function to get the minimum maximum and sum of an array of integers

Time:10-08

Hello I'm struggling a little bit, I can't really figure out how to initialize my *min *max and *sum variables so that it doesn't keep reinitializing in the loop

I did this and I'd like to get help regarding what changes I should make

void min_max_sum_rec(int arr[],int length,int *min,int *max,int *sum){

    if(length==1){
        return;
    }
    else{
        if(arr[0]<*min){
            *min = arr[0];
        }
        if(arr[0]>*max){
            *max = arr[0];
        }
        sum =arr[0];
    }
    min_max_sum_rec(arr 1,length-1,min,max,sum);
}

for reference my professor did this:

void min_max_sum(int arr[],int length,int *min,int *max,int *sum){

    if(length== 1){
        *min=*max=*sum=*arr;
         return;
    }
    min_max_sum(arr 1,length-1,min,max,sum);

    *sum =arr[0];
    *min = (*min<arr[0]?*min:arr[0]);
    *max = (*max>arr[0]?*max:arr[0]);
}

but I don't really understand since *min *sum and *max are not initialized and also can't understand the if statement.

Wouldn't it make it so that when we only have an array with one integer *max *min and *sum would have the same value so all we did before would be meaningless ?

CodePudding user response:

I assume this is your professors code:

void min_max_sum(int arr[],int length,int *min,int *max,int *sum){

    if(length== 1){
        *min=*max=*sum=*arr;
         return;
    }
    min_max_sum(arr 1,length-1,min,max,sum);

    *sum =arr[0];
    *min = (*min<arr[0]?*min:arr[0]);
    *max = (*max>arr[0]?*max:arr[0]);
}

and it's fine. But the documentation must clearly state that the function is only allowed to be called with values of n being greater than zero.

You ask:

Wouldn't it make it so that when we only have an array with one integer *max *min and *sum would have the same value so all we did before would be meaningless ?

Well, no... because we did not do anything before!

Notice that all the comparision for max/min and the sum calculation is after the recursion ends.

In other words - the code keeps making recursive calls with shorter and shorter array until there is only one element. The value of that element (which is the last element of the original array) is then used for initializing max/min/sum. As the recursive calls return it's checked whether a previous element is larger/smaller in order to update max/min and also updates sum.

It's a nice and simple implementation but....

This task should never be solved using recursion

You should ask your professor for an assignment where recursion actually makes sense.

CodePudding user response:

The function can look the following way

void min_max_sum_rec( const int a[], size_t n ,int *min, int *max, int *sum )
{
    if ( n )
    {
        min_max_sum_rec( a   1, n - 1, min, max, sum );

        if ( n == 1 ) 
        {
            *min = a[0];
            *max = a[0];
        }
        else
        {       
            if ( a[0] < *min ) *min = a[0];   
            if ( *max < a[0] ) *max = a[0];
        }
        *sum  =  a[0];
    }
    else
    {
        *min = *max = *sum = 0;
    }
}

That is the function at first calls itself until it gets an empty sub-array. In this case it initializers the objects *min, *max, and sum and returns to the caller.

As for your function implementation then at least this if statement

if(length==1){
    return;
}

does not make a sense.

Also this modified if statement

if(length== 1){
    *min=*max=*sum=*arr;
     return;
}

is general incorrect because the use can pass to the function an argument for the parameter length equal to 0.

And this call

min_max_sum_rec(arr 1,length-1,min,max,sum);

shall be within the else statement.

Note: In the initial code I had a typo using sum instead of *sum.

  • Related