 |
|
 |
Home : C/C++
Name : Max Min Recursive. Description : Finds the maximum and minium element in a list recursively and beautifully.
Code :#include <stdio.h>
int num[10];
main() {
int i,c,max,min,n;
printf ("Enter no . integers:\n");
scanf("%d",&n);
printf("Enter %d numbers in the array\n",n);
for (i=0;i<n;i=i++) {
scanf("%d",&c);
num[i]=c;
}
max=0;min=0;
maxmin (0,n-1,&max,&min);
printf ("\n The max element is %d and min is %d ",max,min);
}
maxmin (int i,int j,int *max,int *min)
{ int max1,min1,max2,min2,mid;
if (i==j) {*max=num[i];
*min=num[i];}
else if (i==j-1)
{ if (num[i]<num[j])
{
*max=num[j];*min=num[i];
}
else
{
*max=num[i];*min=num[j];
}
}
else
{ mid=(i+j)/2;
maxmin(i,mid,&max2,&min2);
maxmin (mid+1,j,&max1,&min1);
if (max2<max1) *max=max1; else *max=max2;
if (min2>min1) *min=min1; else *min=min2;
}
}
|
|
 |