473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Find the highest n second highest numbers in an array within a single iteration.

For example a[]={0,100,5,2,3,200}
Ouput: {200,100}
Feb 12 '11 #1
3 4647
You might try this solution which has an array of 2 integers to contain the answer. While traversing the a[] , if the value is greater than the 1 in your answer's array, replace it in the answer's array. And then compare values in the answer to decide which is the greatest. Repeat this until a[] traversing is done and you will get the answer.

Here's the guide code.


Expand|Select|Wrap|Line Numbers
  1. int answer[2] = {0,0} ;
  2. int a[]={0,100,5,2,3,200}
  3.  
  4. for(int i=0 ; i < a. length; i++)
  5. {
  6.  if(answer[1] < a[i])
  7.  {
  8.     answer[1] = a[i];
  9.     if(answer[0] < answer[1])
  10.     {
  11.        int temp = answer[1];
  12.        answer[1] = answer[0];
  13.        answer[0] = temp;
  14.     }
  15.  }
  16. }
  17.  
  18. return answer;
  19.  
hope this helps. I'm not sure with c syntax but the algo should be fine. :)
Feb 12 '11 #2
whodgson
542 512MB
I may be wrong but I don't think it is necessary to have a second array.
Using a loop to iterate once through the array do the following:
1) assign "hi" to first element array[0]
2)go to the next element in the array[i=1] and assign it to "hi" iff it is larger than "hi". Look at element array[i-1] (array[0]) and assign it to "hinext" iff it is less in value to "hi"
3)go to 3rd element array[2] and assign it to "hi" iff it is larger than "hi"
4)look at element i-1 (in this casee array[1] and assign it to "hinext" iff it is larger than "hinext" and smaller than "hi"
5) after exiting the loop (at completion of one iteration print "hi" and "hinext"
iff=="if and only if"
Feb 15 '11 #3
johny10151981
1,059 1GB
Anyway, Can I ask why you two are doing homework for some one who even dont like to ask question in a proper way?
Feb 15 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

21
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...
7
by: Jan | last post by:
Hi there, Is there a fast way to get the highest value from an array? I've got the array strStorage(intCounter) I tried something but it all and's to nothing If someone good helpme, TIA
4
by: tathagata | last post by:
I want second highest value in a table without using subquery. please send this answer .It will be very helpfull.
3
by: lostncland | last post by:
I am working on this program. The array has 10 scores. (there is more to this program.) Does the last "for" section make sense? /*Defines a global constant called N throughout the file. ...
1
by: faizan qazi | last post by:
Hello Greetings Guide Me To Find Second Highest Number In Array
5
by: konaravikumar | last post by:
writing a query to get the second highest value in atable by using select statement
21
by: farzadaumixer | last post by:
hi umm i need help in writing a program in c(or c++) here are the details: i want it to recieve "x" numbers of student id's and the average of the student in their last term and i want it to...
2
by: tuananh87vn | last post by:
Hi ! can anyone help me with the following topic: Find All Duplicates in a List of Numbers - Array implementation - -InitializeTree() -AddNode() -Add into...
4
by: raylopez99 | last post by:
I would like to know if there's a quick "Linq" way to find the index of an array having a particular value. I can do this the long way by sequential iteration, but would like to know if there's a...
1
by: ozchadl | last post by:
I wish to find the 6 highest amounts in my Mysql database. The database name is employment The table name is jobs The columns / fields are: 'Job title' 'Company Name' 'Employment dates' 'Pay'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.