I have the following code to find first and second largest elements of an array:
using namespace std;
#include<iostream>
#include<conio.h>
#include<cstdio>
#include<string.h>
int* create(int);
int maximum(int *,int);
int second_max(int *,int);
void freememory(int*);
int main(void)
{
int *p,size;
cout<<"Enter size of the array:";
cin>>size;
p=create(size);
cout<<"\nMaximum elemnt in the array is:"<<maximum(p,size);
cout<<"\nSecond maximum element in the array is:"<<second_max(p,size);
freememory(p);
cin.get();
return 0;
}
int* create(int s)
{
int *a;
a=new int[s];
cout<<"Enter elements of array:";
for(int i=0;i<s;i++)
{
cin>>*(a+i);
cout<<"\n";
}
return a;
}
int maximum(int *a,int s)
{
cout<<"\nFinding maximum element in array:";
int max;
max=a[0];
for(int i=0;i<s;i++)
{
if(*(a+i)>max)
max= *(a+i);
}
return max;
}
int second_max(int *a,int s)
{
cout<<"\nFinding second maximum element in array:";
int max,smax;
max=a[0];
for(int i=0;i<s;i++)
{
if(*(a+i)>max)
max= *(a+i);
}
smax=a[0];
for(int i=0;i<s;i++)
{
if((*(a+i)>=smax) && (*(a+i)!=max))
smax=*(a+i);
}
return smax;
}
void freememory(int *a)
{
cout<<"\nFreeing memory now:";
delete []a;
}
The above code compiles fine but when executing does not produce the desired output.. Please help,,I am using dev c++ as compiler..