second largest element in an array

R

Rajesh

Hello Everybody,

Can anybody help me to write a C program for finding the second largest
element in an array.

without using any sort algo. The array may conatin duplicate elements.
The algo should run in O(n) time.

Rajesh
 
N

Nudge

Rajesh said:
Can anybody help me to write a C program for finding the second
largest element in an array without using any sort algo. The
array may contain duplicate elements. The algo should run in O(n)
time.

Again, show us what you've come up with so far.
 
M

maraguida

Please define "second largest element in an array".
For the following arrays which is the second largest element?

a) int x[] = {1};
b) int x[] = {2, 2};
c) int x[] = {4, 4, 8, 8};

The answer to b) is x[0] or x[1]?
 
R

Rajesh

Answer to a) should be max1=1 and max2=1
Answer to b) should be max1=2 and max2=2
Answer to c) should be max1=8 and max2=4
 
M

Mark McIntyre

Hello Everybody,

Can anybody help me to write a C program for finding the second largest
element in an array.

Homework problem.
without using any sort algo. The array may conatin duplicate elements.

Show us your attempt so far.
The algo should run in O(n) time.

If you have problems with the algo, you shold ask in comp.programming
first.

I'd do this thusly by the way

system( sprintf (mstr,
"sort -n -u %s | tail -2 | head -1 > result.txt",
file_of_data);



Mark McIntyre
 
R

Rajesh

max1 = max2 = a[0];

for(i = 1; i< n; i++)
{
if(a > max1)
{
max2 = max1;
max1 = a;
}
else if((a > max2 && a < max1) || max1==max2)
{
max2 = a;
}
}

max1 will contain the largest element and max2 will contain the second
largest element
 
M

Mara Guida

Rajesh said:
max1 = max2 = a[0];

for(i = 1; i< n; i++)
{
if(a > max1)
{
max2 = max1;
max1 = a;
}
else if((a > max2 && a < max1) || max1==max2)
{
max2 = a;
}
}

max1 will contain the largest element and max2 will contain the second
largest element


After I completed your program (#include, int main(void), printf(),
....) and corrected the errors in it, it executed according to the
specifications.
 
S

Scorpio

Rajesh said:
max1 = max2 = a[0];

for(i = 1; i< n; i++)
{
if(a > max1)
{
max2 = max1;
max1 = a;
}
else if((a > max2 && a < max1) || max1==max2)


silly.
The condition "&& a < max1) || max1==max2" is not required.
{
max2 = a;

ITYM a;
}
}

max1 will contain the largest element and max2 will contain the second
largest element

You seem to be trying to quiz/test others here.
If you need any help in solving a problem, show your attempt and
c.l.c will help you solve it.


Sharath A.V
 
C

CBFalconer

Rajesh said:
Can anybody help me to write a C program for finding the second
largest element in an array. without using any sort algo. The
array may conatin duplicate elements. The algo should run in O(n)
time.

Yes, we can. However we charge 200 USD per hour for doing
homework, with payment in advance for at least 4 hours effort. If
you supply the name and email of your instructor so that we can
submit your homework directly we will cut the price to 100 USD per
hour.

If you fail to properly quote context in any replies, the price is
doubled.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
M

Martin Ambuhl

Please define "second largest element in an array".
For the following arrays which is the second largest element?

a) int x[] = {1};
b) int x[] = {2, 2};
c) int x[] = {4, 4, 8, 8};

The answer to b) is x[0] or x[1]?

Yes. The phrase "second largest element in an array" is not sufficient
to specify which of two equal elements is the "second largest."

Nor is it necessarily true that the answer to (c) is x[2] or x[3]; some
readings of "second largest element" would require the answer to (c) to
be x[0] or x[1].
 
T

tedu

Mark said:
system( sprintf (mstr,
"sort -n -u %s | tail -2 | head -1 > result.txt",
file_of_data);

converting sprintf's return to a char * is unlikely to work correctly
on most systems.
 
M

Mark McIntyre

converting sprintf's return to a char * is unlikely to work correctly
on most systems.

I agree, but then a) the entire answer was almost certainly not what
the OP wanted and b) if we posted 100% accurate code in response to
homework questions, nobody would ever learn.


Mark McIntyre
 
F

Flash Gordon

Mark said:
I agree, but then a) the entire answer was almost certainly not what
the OP wanted and b) if we posted 100% accurate code in response to
homework questions, nobody would ever learn.

You forgot c) on the Psychic Station 200 undefined behaviour does what
the user wants, so on the PS200 it will give the OP the largest two
elements :)
 
T

Thad Smith

Rajesh said:
max1 = max2 = a[0];

for(i = 1; i< n; i++)
{
if(a > max1)
{
max2 = max1;
max1 = a;
}
else if((a > max2 && a < max1) || max1==max2)
{
max2 = a;
}
}

max1 will contain the largest element and max2 will contain the second
largest element


Really? Try a[] = {5,4,3};
 
T

Thad Smith

Martin said:
Please define "second largest element in an array".
For the following arrays which is the second largest element?

a) int x[] = {1};
b) int x[] = {2, 2};
c) int x[] = {4, 4, 8, 8};

The answer to b) is x[0] or x[1]?

The answer to second largest is
a) none
b) none
c) 4.
 
L

Lemor

here's your porgram:

I compile my prorams in VC++. it'll surely work!

#include<iostream>

using namespace std;

int main()
{
int aray[100],n,temp=0,m;
cin>>n;

for(int j=0;j<n;j++)
{
cin>>aray[j];
}
for(j=0;j<n;j++)
{
if(aray[j] >= temp)
{
temp=aray[j];
m=j;
}
}
aray[m]=0;temp=0;

for(j=0;j<n;j++)
{
if(aray[j] >= temp)
{
temp=aray[j];
}
}
cout<<endl<<temp;
return 0;
}
 
L

Lemor

#include<iostream>

using namespace std;

int main()
{
int aray[100],n,temp=0,m;
cin>>n;

for(int j=0;j<n;j++)
{
cin>>aray[j];
}
for(j=0;j<n;j++)
{
if(aray[j] >= temp)
{
temp=aray[j];
m=j;
}
}
aray[m]=0;temp=0;

for(j=0;j<n;j++)
{
if(aray[j] >= temp)
{
temp=aray[j];
}
}
cout<<endl<<temp;
return 0;
}
 
L

Lemor

second largest element is the the second largest value in a given
array.

int x[] = {2, 2};

Both of then are largest numbers in the array given as both are same.
No one of them is second largest.
 
M

Martin Ambuhl

Lemor said:
#include<iostream>
[and more obscurantist crap]

If you want C++, go to < Just in case you're one of
those that posts C++-looking code and the does a stuck-pig "That's not
C++, it's <useless language name here>", whatever the hell your crap
was, it's off-topic and should be kept to yourself.
 
M

Martin Ambuhl

Lemor said:
here's your porgram:

I compile my prorams in VC++. it'll surely work!

#include<iostream>

More C++? Please go away. If you don't know the difference between C
and C++, you surely have no business offering "advice."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top