GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Tech FAQs  >  Programming  >  C

 Print  |  
Question:  Finding Two least number in array.

Answer: How to find the least two numbers in a single dimension array in a single pass (traversing the elements only once)


March 03, 2008 09:33:43 #1
 amitprasad   Member Since: March 2008    Total Comments: 2 

RE: Finding Two least number in array.
 
It is very simple man. Just do like taht.

#define LEAST_COMPARE(index, least) list[index] < list[least]

for (index = 0;index < 10;index++)
{
    if (LEAST_COMPARE(index,least))
    {
        second_least = least;
        least = index;
    }
}
list is array of elements and lest and second_least gives the index to array.
     

 

Back To Question