Home > Enterprise >  find maximum and minimum value from array using recurrsion
find maximum and minimum value from array using recurrsion

Time:10-03

I wrote a code to find the maximum and second maximum using recursion:

struct max_min
{
        int max;
        int min;
}obj;

int find_l_m(int a[], int size)
{
        int next_element;

        if (size == 1)
                return a[0];

        next_element = find_l_m(a, size-1);

        if (a[size-1] > next_element) {
                obj.max = a[size-1];
                obj.min = next_element;

                return a[size-1];
        } else {
                obj.max = next_element;
                obj.min = a[size-1];

                return next_element;
        }

}

int main()
{
        int a[] = {1, 11, 4, 9, 6, 7, 8, 10};

        find_l_m(a, sizeof(a) / sizeof(a[0]));
        printf("%d\n%d\n", obj.max, obj.min);
}

I have couple of query:

  1. Can this be made more simple, should be able to return max and second max without using struct object.
  2. How above code can be changed to find maximum (that it does) and minimum value from array

CodePudding user response:

Can this be made more simple, should be able to return max and second max without using struct object.

You could factor out the update of a min-max structure with a single value into a separate function. Then, you could probably make your recursive function into a one-liner.

How above code can be changed to find maximum (that it does) and minimum value from array

Well, you would need to:

  • return a struct max_min.
  • properly initialize your struct max_min (e.g. for the case of an empty array; defining the semantics appropriately).

CodePudding user response:

recursion is good (assuming that it is good for anything) in finding max or min.

typedef struct 
{
        int max;
        int min;
}obj;

int max_min(int minmax, int arr[], size_t size)
{
    if(size == 1) return *arr;
    else 
    {
        int result = max_min(minmax, arr   1, size - 1);
        if(minmax) return result <= *arr ? *arr : result;
        else return result >= *arr ? *arr : result;
    }
}

obj test(int *arr, size_t size)
{
    return (obj){max_min(1, arr, size), max_min(0, arr, size)};
}

int main(void)
{
    int arr[] = {1,4,9,3,2,-4,0,2,3};
    obj o = test(arr, sizeof(arr)/sizeof(arr[0]));

    printf("max = %d min = %d\n", o.max, o.min);
}

https://godbolt.org/z/zGrYMYjx7

CodePudding user response:

To answer your first question:

Can this be made more simple, should be able to return max and second max without using struct object.

There are a couple of ways to return two (or more) values from one function in C. The first one, as you noted, is using a struct, this way:

// Define a struct holding two integers 
struct Pair_of_int {
    int a;
    int b;
 };


// Define a function returning a `PairOfInt`
struct Pair_of_int my_function(int[] a, int size) {
     struct Pair_of_int result;
     // Do some work
     result.a = /*...*/;
     result.b = /*...*/;
     return result;
 }

This is how you would call such a function in your main function :

struct Pair_of_int pair = my_function(...);
printf("First %d, second %d", pair.a, pair.b);

Note that this is not exactly what you have done in the code you've shown. In your code you have a global instance of your struct max_min that gets updated by the function. What you want is to use the structure as the return value of the function, as shown above.

The other way is to use pointers to return multiple values. Here if you want two ints you have to define your function to accept two pointers to int as arguments and use them to store the values.

void my_function(int[]a, int size, int* out_a, int* out_b) {
    // Do some work
    *out_a = /*...*/;
    *out_b = /*...*/;
}

Here as you can see the function returns nothing. Instead it uses the two pointers out_a and out_b to store the result. This is how you would use it in the main function :

int a; int b;
my_function(array, size, &a, &b);
printf("First %d, second %d", a, b);

You create two variables a and b that will hold the results and pass their address to my_function with the & operator.

How above code can be changed to find maximum (that it does) and minimum value from array

This is shouldn't be too hard if you get the first case working correctly. You have to keep track of the two values separately. You might have to add some more arguments to your function for it to work recursively.

  •  Tags:  
  • c
  • Related