1. Home >
  2. Apps >
  3. Groups >

problem in c++ code

Question asked by Manish Goyal in #Coffee Room on Nov 1, 2009
Manish Goyal
Manish Goyal · Nov 1, 2009
Rank A2 - PRO
helo guys...I have a problem in following c++ code...I want to find largest and smallest element in an array..using recurssion..please help me
#include<iostream.h>
#include<conio.h>
int max1,min1;
void maxmin(int a[],int,int,int*,int*);
int main()
{
int i,*a,n,max,min;
cout<<"Enter the no of elements"<<endl;
cin>>n;
a=new int[n];
cout<<"Enter elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
maxmin(a,0,n-1,&max,&min);
cout<<"Maximum element"<<max<<endl;
cout<<"Minimum element"<<min<<endl;
getch();
return 0;
}
void maxmin(int a[],int start,int finish,int *max,int *min)
{
int mid;
    if(start==finish)
    {
        *max=*min=a[start];
    }
    else
    if(start==finish-1)
    {
        if(a[start]>a[finish])
        {
        *max=a[start];
        *min=a[finish];
        }
    else
        {
        *max=a[finish];
        *min=a[start];
        }
    }
else
mid=(start+finish)/2;
maxmin(a,start,mid,*&max,*&min);
maxmin(a,mid+1,finish,&max1,&min1);
if(max1>*max)
{
*max=max1;
}
if(*min>min1)
{
*min=min1;
}
}
Posted in: #Coffee Room
Manish Goyal
Manish Goyal · Nov 3, 2009
Rank A2 - PRO
Hello ceans I have solved this problem.there was small mistake..I hope most of you had tried this ..this code is for those who are unable to solve this..
#include
#include
int max1,min1;
void maxmin(int a[],int,int,int*,int*);
int main()
{
int i,*a,n,max,min;
cout<<"Enter the no of elements"<>n;
a=new int[n];
cout<<"Enter elements"<>a[i];
}
maxmin(a,0,n-1,&max,&min);
cout<<"Maximum element"<a[finish])
        {
        *max=a[start];
        *min=a[finish];
        }
    else
        {
        *max=a[finish];
        *min=a[start];
        }
    }
else
{
mid=(start+finish)/2;
maxmin(a,start,mid,*&max,*&min);
maxmin(a,mid+1,finish,&max1,&min1);
if(max1>*max)
{
*max=max1;
}
if(*min>min1)
{
*min=min1;
}
}
}

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up