Thread: Finding the smallest value......HARD!!!

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    32

    Finding the smallest value......HARD!!!

    To find the smallest value in a number of values may seem easy, but I think it is not that easy after all.

    I've an array of float values. (Stored in Values[j])

    I'm supposed to find the smallest value among all the values. After, finding the smallest value, I''m to find the second smallest value, next the third smallest value....etc

    Code:
    j=0;
    Smallest = Value[0];
    for (j=0; j< MaxArrayValue ; j++){
    	if (Value[j+1] < Smallest ){
    	Smallest = Value [j+1];
    	}//End If
    }//End For
    In the above codes, I'm able to get the smalest values....but to find the second smallest values and rest of the values will not be easy at all......

    Note: I'm not arranging the values in ascending order, I'm getting the smallest values out so that I could perform some calculations without affecting the "already selected smallest value".

    _____

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    It'll be much easier if you sort the array (by implementing some sorting algo) and then you know where your greatest and least numbers are stored.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    32
    I cannot sort the values.......that is why I'm so traumatized and being constantly haunted by this...........

    If I sort the values, I'll not be able to proceed on with the coding.....

    _____

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    so why dont you copy the array in a "buffer" array, sort it and use it....and for your program keep using the "real" not sorted one?
    This forum is the best one I've ever seen. Great ppl, great coders

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    32
    Hmm....I've already thought about this, but I'm doing my codes dynamically....which means I may need to calculate the codes n times...

    If I calculate the codes 10 times, I must copy the codes 10 times...

    If I calculate the codes n times, I must copy the codes n times...
    How am I going to create n number of variables to copy the values of the array..??

    _____

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Maybe you should explain what the actual goal of this thing is, because I'm not following the problem at all.
    If you understand what you're doing, you're not learning anything.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why is this hard? You first find the smallest value. Then you say you're stuck on finding the "second smallest"? Well how do you yourself pick out the "second smallest" from a list? I'll tell you:

    First you find the smallest, and make a note of it.
    Then you go through the list and find the smallest of those, that is bigger than the one you made a note of.

    See, it's not hard at all.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Professional Bit Wrangler
    Join Date
    Mar 2005
    Posts
    6

    Simple solution

    1) Build a index array, filled with values from 0 to whatever your max index is

    2) Sort the array, using the index number to dereference the value. (ie you'll only be changing the values in the index array, the float array order remains unchanged). Use whatever sort routine suits your fancy.

    3) Step through the index array, fetching values from your float array.

    Simple enough?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's an easier way: Keep track of both lowest and second-lowest on the first pass through the array. Faster than sorting can ever hope to be. Also much easier with much less code. One single for loop and a run through the array does it all.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Professional Bit Wrangler
    Join Date
    Mar 2005
    Posts
    6
    Quzah:

    Yes, but what if there are several duplicate values...

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by AssistMe
    I'm supposed to find the smallest value among all the values. After, finding the smallest value, I''m to find the second smallest value, next the third smallest value....etc
    Was there something here you didn't quite understand?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    32
    Quote Originally Posted by Dave_Sinkula
    Was there something here you didn't quite understand?
    Nope.....I understand the sorting...but the thing I'm asking here is different from that.....I've X number of features....and after each set of calculations, I'm supposed to find the smallest calculated values of each features......In the next round of calculations, I'm supposed to ommit the selected features and find the smallest values again.....(in other words, find next samllest value.....)

    _____

  13. #13
    Professional Bit Wrangler
    Join Date
    Mar 2005
    Posts
    6
    Ok, this is getting silly. Here is what you want. Just create two same sized arrays, one with the float values, the other ints initialized to 0. Then repeatedly call this function:

    Code:
    int FindSmallest(float* Value, int* doneArray, int MaxArrayValue)
    {
        int j;
        int Smallest = -1;
    
        for (j = 0; j < MaxArrayValue; j++)
        {
            if (!doneArray[j])
            {
                if ((Smallest == -1) || (Value[j] < Value[Smallest]))
                {
                    Smallest = j;
                }//End If
            }// end if doneArray
        }//End For
        if (Smallest != -1)
            doneArray[Smallest] = 1;
        return (Smallest);
    }
    Hope that helps.
    (code is untested/off the top of my head. Caveat Emptor!)

    The Peachinator

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Frankly, that's a stupid way to do it. Do what I said in the first place:
    Code:
    int x;
    int smallest = INT_MAX, second = INT_MAX;
    int smallestindex = -1, secondindex = -1;
    
    for( x = 0; x < number; x++ )
        if( array[x] < smallest )
        {
            second = smallest;
            secondindex = smallestindex;
    
            smallest = array[x];
            smallestindex = x;
        }
    Wow, that was hard.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Professional Bit Wrangler
    Join Date
    Mar 2005
    Posts
    6
    As I said before, your approach fails if there are any duplicate values in the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. largest and smallest
    By eldemonio in forum C Programming
    Replies: 9
    Last Post: 10-15-2007, 02:00 PM
  2. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM
  3. which hard drive to buy ? UATA or SATA
    By gemini_shooter in forum Tech Board
    Replies: 10
    Last Post: 08-24-2005, 08:16 PM
  4. Strange hard disk
    By GanglyLamb in forum Tech Board
    Replies: 20
    Last Post: 03-01-2003, 04:05 AM
  5. Failing Hard Drive
    By RoD in forum Tech Board
    Replies: 10
    Last Post: 02-18-2003, 08:08 AM