Friday, 3 December 2010

47. Find the hight and second hight number by using RECURSION

/* Find the hight number by using recursion */

#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n,a[100],i,j;
printf("How many nmber you want to input: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d number",i+1);
scanf("%d",&a[i]);
}
int hight(int,int,int,int*);
j=hight(n,0,0,a);
printf("The hight number is %d",j);
return 0;
}

int hight(int n,int i,int m,int *a)
{
if(i>n) return m;
if(m<a[i])
m=a[i];
hight(n,i+1,m,a);
}


/* Find the second hight number by using recursion */

#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n,a[100],i;
printf("How many nmber you want to input: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d number",i+1);
scanf("%d",&a[i]);
}
int hight(int,int,int,int,int*);
hight(n,0,0,0,a);
printf("The second hight number is %d",a[1]);
return 0;
}

int hight(int n,int i,int j,int k,int *a)
{
if(i>n-1) return 0;

for(j=i+1;j<n;j++)
if(a[i]<a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
hight(n,i+1,j,k,a);
}


written by anrob.

No comments:

Post a Comment