Home > OS >  segmentation error while using return statement returning recursive function
segmentation error while using return statement returning recursive function

Time:08-17

#include<stdio.h>

float small_array_recursion(const float[],int);

int main(){
    float array[]={-34,-89,3,9.67,45,12};

    int size=6;

    float value=small_array_recursion(array,size);

    printf("%s%.2f","smallest value in the array is ",value);
}

float small_array_recursion(const float array1[],int size){
    int len=size-1;

    static float min=3.36e9;

    if(len==0){
        return array1[0]<min?array1[0]:min;
    }
    else if(array1[len]<min){
        min=array1[len];
        return small_array_recursion(array1,len);
    }
    else
       return small_array_recursion(array1,len);
}

CodePudding user response:

  1. Write floats as floats, like 'float array[]={-34.0, -89.0, 3.0, 9.67, 45.0, 12.0};' (use decimal point)

  2. The code is correct, if you replace variable "max" by "min".

  3. For special case 'len==0', the following two lines need to be added

    if(array1[len]<min) min=array1[len];

to cover the case, if the first array element is the smallest one.

Then, the full, correct code is:

#include<stdio.h>

float small_array_recursion(const float[],int);

int main(){
    float array[]={-34., 89.,3.,9.67,45.,12.};

    int size=6;

    float value=small_array_recursion(array,size);

    printf("%s%.2f","smallest value in the array is ",value);
}

float small_array_recursion(const float array1[],int size){
    int len=size-1;

    static float min=3.36e9;

    if(len==0){
        if(array1[len]<min)
        min=array1[len];
        return min;
    }
    else if(array1[len]<min){
        min=array1[len];
        return small_array_recursion(array1,len);
    }
    else
       return small_array_recursion(array1,len);
}

Anyway, while the example is working. The return values of the function don't go anywhere when called from within the function. This is possible, because variable 'min' was declared as static. It might be a good exercise to achieve the recursion without variable "min" being static.

Yes, like commented by Fe2O3, this clearly is not a recursion problem, to find the minimum of an array. I agree. Well, it could be seen as an exercise where recursion can be applied.

Adapted code with recursion without a static variable "min".

#include<stdio.h>

float small_array_recursion(const float[],int,float);

int main(){
    float array[]={34., 89.,993.,9.67,45.,120.};
    int size=6;
    float min=3.36e9;
    float value=small_array_recursion(array,size,min);

    printf("%s%.2f\n","smallest value in the array is ",value);
}

float small_array_recursion(const float array1[], int size, float min){
    
    int len=size-1;
    float localmin;
    
    localmin=min<array1[len] ? min : array1[len];
    
    if(len>0) 
    {
        return small_array_recursion(array1, len, localmin);
    }
    else 
    {
        return localmin;
    }
}

CodePudding user response:

This is not well suited to recursion. If this were an 'interview question', attempting recursion would be a 'fail'.

And, unless there is a space constraint, double is more likely a better size for the floating point values.

#include <stdio.h>

int main() {
    // use spaces to make your values readable
    double array[] = { -34.0, -89.0, 3.0, 9.67, 45.0, 12.0 };

    int minInd = 0;
    for( int i = 1; i < sizeof array / sizeof array[0]; i   )
        if( array[ i ] < array[ minInd ] )
            minInd = i;

    printf( "Smallest value in the array is %.2f", array[ minInd ] );

    return 0;
}

Output:

Smallest value in the array is -89.00
  • Related