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 20 5051
Rajesh wrote: 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.
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]?
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
On 27 Jan 2006 03:26:05 -0800, in comp.lang.c , "Rajesh"
<ra***********@gmail.com> wrote: 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
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
max1 = max2 = a[0];
for(i = 1; i< n; i++)
{
if(a[i] > max1)
{
max2 = max1;
max1 = a[i];
}
else if((a[i] > max2 && a[i] < max1) || max1==max2)
{
max2 = a;
}
}
max1 will contain the largest element and max2 will contain the second
largest element
Rajesh wrote: max1 = max2 = a[0];
for(i = 1; i< n; i++) { if(a[i] > max1) { max2 = max1; max1 = a[i]; } else if((a[i] > max2 && a[i] < 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.
Rajesh wrote: max1 = max2 = a[0];
for(i = 1; i< n; i++) { if(a[i] > max1) { max2 = max1; max1 = a[i]; } else if((a[i] > max2 && a[i] < max1) || max1==max2)
silly.
The condition "&& a[i] < max1) || max1==max2" is not required.
{ max2 = a;
ITYM a[i];
} }
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
Rajesh wrote: 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/> ma*******@gmail.com wrote: 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].
Mark McIntyre wrote: 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.
On 27 Jan 2006 11:03:41 -0800, in comp.lang.c , "tedu"
<tu@zeitbombe.org> wrote: Mark McIntyre wrote: 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.
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
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mark McIntyre wrote: On 27 Jan 2006 11:03:41 -0800, in comp.lang.c , "tedu" <tu@zeitbombe.org> wrote:
Mark McIntyre wrote: 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.
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 :-)
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Rajesh wrote: max1 = max2 = a[0];
for(i = 1; i< n; i++) { if(a[i] > max1) { max2 = max1; max1 = a[i]; } else if((a[i] > max2 && a[i] < 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};
--
Thad
Martin Ambuhl wrote: ma*******@gmail.com wrote:
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.
--
Thad
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;
}
#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;
}
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.
Lemor wrote: #include<iostream>
[and more obscurantist crap]
If you want C++, go to <news:comp.lang.c++>. 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.
Lemor wrote: 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."
"Lemor" <le*******@gmail.com> writes: 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.
Please read <http://cfaj.freeshell.org/google/>.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lawrence |
last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by
the second field in each row?
Also, when I use sort() on a two dimensional array, it seems to work a
lot like...
|
by: Code4u |
last post by:
I need to write an algorithm that sheds the outliers in a large data
set, for example, I might want to ignore the smallest 2% of values and
find the next smallest. Boost has a nth_element...
|
by: Jaspreet |
last post by:
I was working on some database application and had this small task of
getting the second highes marks in a class. I was able to do that using
subqueries.
Just thinking what is a good way of...
|
by: tony collier |
last post by:
i have created a 7-dimensional array which has 19.5
million elements.
when i try to create 8-dimensional array i get out of bounds system
exception. does anyone know if this limitation is due...
|
by: thomasp |
last post by:
For those who gave advice on the shortfalls of my first attempt at writing a
vb.net class, Thank You.
I hope that I was able to apply some of your advice to this larger atempt.
At first I didn' t...
|
by: ramu |
last post by:
Hi,
I have, suppose 1000 numbers, in a file. I have to find out 5
largest numbers among them without sorting. Can you please give me an
efficient idea to do this? My idea is to put those numbers...
|
by: HEMH6 |
last post by:
Who can help solve this problem???
Finding the Largest Value
(a) write a function, largest(), that returns the largest value in a signed integer array. The array and its size are passed as...
|
by: bele_harshad2006 |
last post by:
how can i pick up largest no from 5 rows by 5 column matrix????
|
by: ravi |
last post by:
Can anybody tell me a method to
Use only N + O(log n) comparisons to find the second largest (or
smallest) element in a list of N elements.
Thnx in advance
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |