Find the largest two values in an Array

#include <iostream>
using namespace std;

void f(int y[],int x[],int N)
{
int i,t;

y[0]=x[0];
y[1]=x[1];

if (y[0]<y[1])
{t=y[0];y[0]=y[1];y[1]=t;}

for (i=2;i<N;i++)
{
if (x[i]>y[0])
{y[1]=y[0];y[0]=x[i];}
else
if (x[i]>y[1])
y[1]=x[i];

}

}
main()
{

int i,N,y[2];
int x[]={1,6,8,5,4,3,6,7,4,0,8,15,22,77,8,22,35,21};
N = sizeof(x)/sizeof(int);
f(y,x,N);
printf(“%d %d\n”,y[0],y[1]);

system(“pause”);
return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: