# C program to find maximum and minimum elements in array using recursio

Page 1 of 1

## 8 Replies - 396 Views - Last Post: 09 February 2018 - 09:11 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=409203&amp;s=c4314ac09c51eb3c2c1d399a79481afb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 akane96

Reputation: 0
• Posts: 58
• Joined: 21-March 17

# C program to find maximum and minimum elements in array using recursio

Posted 08 February 2018 - 03:02 PM

C program to find maximum and minimum elements in array using recursion
Hello can someone help me please I found this program that help to find the maximum and minimum elements in a array . But my problem is I don't understand the Recursive functions to find maximum and minimum element in a given array. Can someone please help me with that , I'm a beginner in C programming language. So can someone please try to give me explanation about each line of the recursive function that allows to find the maximum and the minimum please. And sorry for my bad english i'm a french native .
```#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array

/* Function declarations */
int maximum(int array[], int index, int len);
int minimum(int array[], int index, int len);

int main()
{
int array[MAX_SIZE], N, max, min;
int i;

/* Input size and elements of array */
printf("Enter size of the array: ");
scanf("%d", &N);
printf("Enter %d elements in array: ", N);
for(i=0; i<N; i++)
{
scanf("%d", &array[i]);
}

max = maximum(array, 0, N);
min = minimum(array, 0, N);

printf("Minimum element in array = %d\n", min);
printf("Maximum element in array = %d\n", max);

return 0;
}

/**
* Recursive function to find maximum element in the given array.
*/
int maximum(int array[], int index, int len)
{
int max;

/*
* Only last and second last element are left
*/
if(index >= len-2)
{
if(array[index] > array[index + 1])
return array[index];
else
return array[index + 1];
}

/*
* Recursively call maximum to find maximum element in
* right side of the array from current index.
*/
max = maximum(array, index + 1, len);

/*
* Compare the current array element with maximum
* element on its right side
*/
if(array[index] > max)
return array[index];
else
return max;
}

/**
* Recursive function to find minimum element in the array.
*/
int minimum(int array[], int index, int len)
{
int min;

if(index >= len-2)
{
if(array[index] < array[index + 1])
return array[index];
else
return array[index + 1];
}

min = minimum(array, index + 1, len);

if(array[index] < min)
return array[index];
else
return min;
}

```

Is This A Good Question/Topic? 0

## Replies To: C program to find maximum and minimum elements in array using recursio

### #2 Skydiver

• Code herder

Reputation: 6164
• Posts: 21,261
• Joined: 05-May 12

## Re: C program to find maximum and minimum elements in array using recursio

Posted 08 February 2018 - 06:44 PM

By finding code instead of trying to solve the problem yourself, you are not aiding your learning about recursive functions.

One way to think of recursion is you are looking for some treasure in a square grid city. One approach is to just have a search pattern that goes and checks every block from the northwest corner to the southeast corner working either a zigzag pattern or always going row by row, column by column. An alternative is to divide up the city into quadrants, and then each quadrant into even smaller quadrants, making smaller and smaller quadrants. The subdivision is a recursive approach.

### #3 Skydiver

• Code herder

Reputation: 6164
• Posts: 21,261
• Joined: 05-May 12

## Re: C program to find maximum and minimum elements in array using recursio

Posted 08 February 2018 - 07:33 PM

Anyway, if you took time to actually read the code that you found, the comments in there explain exactly what is happening.

### #4 baavgai

• Dreaming Coder

Reputation: 7163
• Posts: 14,929
• Joined: 16-October 07

## Re: C program to find maximum and minimum elements in array using recursio

Posted 09 February 2018 - 05:33 AM

As an aside, for C, this is rather sloppy code. There is an extra variable: the kind of thing you might need in Java, but not C.

So, a start:
```#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array

/* Function declarations */
int maximum(int *, int);
int minimum(int *, int);

int main() {
int array[MAX_SIZE];

printf("Minimum element in array = %d\n", minimum(array, size));
printf("Maximum element in array = %d\n", maximum(array, size);)/>;

return 0;
}

```

Now, let's just walk it:
```maximum({1,6,3,5,4}, 5)
maximum({6,3,5,4}, 4)
maximum({3,5,4}, 3)
maximum({5,4}, 2)
maximum({4}, 1)
return 4
5>4 return 5
5>3 return 5
6>5 return 6
6>1 return 6

```

Hope this helps.

### #5 akane96

Reputation: 0
• Posts: 58
• Joined: 21-March 17

## Re: C program to find maximum and minimum elements in array using recursio

Posted 09 February 2018 - 07:07 AM

Skydiver, on 08 February 2018 - 06:44 PM, said:

By finding code instead of trying to solve the problem yourself, you are not aiding your learning about recursive functions.

One way to think of recursion is you are looking for some treasure in a square grid city. ...

I agree with you. I wasn't searching for solution for my exercises. Actually it's all in a website ( codewin.org) where you can learn lessons and pratice at the same time and got solutions all included . And thank you for the explanation.

This post has been edited by Skydiver: 09 February 2018 - 08:42 AM
Reason for edit:: Trimmed quote.

### #6 akane96

Reputation: 0
• Posts: 58
• Joined: 21-March 17

## Re: C program to find maximum and minimum elements in array using recursio

Posted 09 February 2018 - 07:18 AM

baavgai, on 09 February 2018 - 05:33 AM, said:

As an aside, for C, this is rather sloppy code. There is an extra variable: the kind of thing you might need in Java, but not C.

So, a start:
```#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array

/* Function declarations */
int maximum(int *, int);
int minimum(int *, int);

int main() {
int array[MAX_SIZE];

printf("Minimum element in array = %d\n", minimum(array, size));
printf("Maximum element in array = %d\n", maximum(array, size);)/>/>;

return 0;
}

```

Now, let's just walk it:
```maximum({1,6,3,5,4}, 5)
maximum({6,3,5,4}, 4)
maximum({3,5,4}, 3)
maximum({5,4}, 2)
maximum({4}, 1)
return 4
5>4 return 5
5>3 return 5
6>5 return 6
6>1 return 6

```

Hope this helps.

Yes it did . Thanks a lot. So those lines in the code mean :
```if(index >= len-2)
44
{
45
if(array[index] > array[index + 1])
46
return array[index];
47
else
48
return array[index + 1];
49
}
50

```

You start the comparaison with two element of the array and you go on until you reach len-2 . But my question for that part is why do we use len-2 and why not len-1 ?

### #7 baavgai

• Dreaming Coder

Reputation: 7163
• Posts: 14,929
• Joined: 16-October 07

## Re: C program to find maximum and minimum elements in array using recursio

Posted 09 February 2018 - 08:33 AM

Best answer I have is, as noted, the code sucks.

If the logic works the way the programmer expected, then index == len-2 would also make sense. This reveals a bug in that the mess fails on a list of size one.

The idea here appears to be that if you only have two items left in your list, you return the largest. This is rather inane. The real solution is that the last item is always returned as the largest. Tests, like the ones already present in this function, will resolve if that is correct. I suspect this mess failed with an infinite loop and the author just threw in that redundant check to make it work without considering the root cause.

If you are really interested in how this trainwreck works, put a load of printf statements in it and feed it some test data.

### #8 Skydiver

• Code herder

Reputation: 6164
• Posts: 21,261
• Joined: 05-May 12

## Re: C program to find maximum and minimum elements in array using recursio

Posted 09 February 2018 - 08:45 AM

+1. Regarding train wreck.

If the requirement is only to use recursion, and there is no restriction on clobbering the data, I recommend implementing quicksort() to satisfy the recursion requirement, and then look at the first item and the last item to find the minimum and maximum.

### #9 akane96

Reputation: 0
• Posts: 58
• Joined: 21-March 17

## Re: C program to find maximum and minimum elements in array using recursio

Posted 09 February 2018 - 09:11 AM

baavgai, on 09 February 2018 - 08:33 AM, said:

Best answer I have is, as noted, the code sucks.

If the logic works the way the programmer expected, then index == len-2 would also make sense. This reveals a bug in that the mess fails on a list of size one.

The idea here appears to be that if you only have two items left in your list, you return the largest. This is rather inane. The real solution is that the last item is always returned as the largest. Tests, like the ones already present in this function, will resolve if that is correct. I suspect this mess failed with an infinite loop and the author just threw in that redundant check to make it work without considering the root cause.

If you are really interested in how this trainwreck works, put a load of printf statements in it and feed it some test data.

Okay thanks a lot

Skydiver, on 09 February 2018 - 08:45 AM, said:

+1. Regarding train wreck.

If the requirement is only to use recursion, and there is no restriction on clobbering the data, I recommend implementing quicksort() to satisfy the recursion requirement, and then look at the first item and the last item to find the minimum and maximum.

I will try what you say hope it's gonna work. Thank you for your advices. Have a nice evening