Source Code:
//second greates among array
#include<iostream>
using namespace std;
int great (int a[],int n)
{
int i,greatest;
greatest=a[0];
for (i=0;i<n;i++)
{
if(a[i]>greatest)
greatest=a[i];
}
return greatest;
}
int second (int a[],int n)
{
int i,second,g;
g=great(a,n);
if(a[0]==g)
second=a[1];
else
second=a[0];
for(i=0;i<n;i++)
{
if(a[i]>second&&a[i]<g)
{
second=a[i];
}
}
return second;
}
int main()
{
int n,a[40],j;
cout<<"How many numbers? ";
cin>>n;
cout<<"Enter "<<n<<" numbers\n";
for(j=0;j<n;j++)
cin>>a[j];
cout<<"The greatest number is "<<great(a,n)<<endl;
cout<<"The second greatest number is "<<second(a,n)<<endl;
system("pause");
return 0;
}
No comments:
Post a Comment