CrazyEngineers
  • problem in c++ code

    Manish Goyal

    Manish Goyal

    @manish-r2Hoep
    Updated: Oct 22, 2024
    Views: 970
    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;
    }
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Manish Goyal

    MemberNov 3, 2009

    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<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;
    }
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register