//A recursive program to find the second maximal element in an array.
#include <iostream.h>
#define SIZE 10
int static max1=0;
int static max2=0;
int max (int arr[SIZE],int s)//recursive function to find the second maximal
{
if(s>=0)//recursive condition
{
if (max1<arr[s])//put the value in max1?
{
if (max1>max2)
max2=max1;
max1=arr[s];
}
else//no, put it in max2
if (max2<arr[s])
{
if (max2>max1)
max1=max2;
max2=arr[s];
}
max(arr,s-1);//recursive calling
}
else
if(max1>max2)
return max2;
else
return max1;
}
void main()
{
int s=0;
int arr[SIZE]={14,98,50,2,24,61,88,13,55,7};
s=SIZE;
cout <<"The second maximum is:"<<max(arr,s-1)<<"\n";
}
** Edit **

New Topic/Question
Reply




MultiQuote





|