Thread: Finding the smallest value

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    Finding the smallest value

    I'm running a loop where I'm trying to find the smallest value in an array. Does anyone know of a way in which I can assign the first value in the first pass to the variable that is supposed to hold the smallest value in such a way that I don't have to add a conditional statement saying that if i = 0 to automatically assign it. I also don't want to initialize the variable to some ridiculously large number. For example:

    Code:
    int minimum_value = 10000000 // I don't want to do this
    .
    .
    .
    .
    .
    for(i=0;array[i]<elements;i++)
       {
         if(i==0) minimum_value = array[i]; // I don't want to have to do this
         if(array[i] < minimum_value) minimum_value = array[i];
        }
    .
    .
    .
    .
    .

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Assign the first element of array to minimum_value and start the loop with i=1.

  3. #3
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    maybe something like this
    Code:
    int x;
    int g;
    int smallest;
    
    smallest=0;
    
     for(x=0;x>(elements numbers);x++)
          {
                if(array[x]<smallest||array[x]==smallest) /*because it's equals to zero */ 
                    {
                        smallest=array[x];
                     }
           }
    
    printf("The Smallest Value In The Array Is &#37;d",smallest);
    Last edited by St0rM-MaN; 06-15-2007 at 02:55 AM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I would say that's wrong, because you're assuming the minimum element is less than 0, which may or may not be the case (probably not a good assumption for a general solution).

    OnionKnight has the right idea.

  5. #5
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by MacGyver View Post
    I would say that's wrong, because you're assuming the minimum element is less than 0, which may or may not be the case (probably not a good assumption for a general solution).

    OnionKnight has the right idea.
    i have just modified it

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If I give you an int array of size 5 with the following numbers, what would your code print?

    Code:
    1, 2, 3, 4, 5
    Assuming you fix the > to be <, it would print 0.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Initializing the variable to a "ridiculously large" number is actually the right thing to do, to handle the case where the array is empty (0 elements). Instead of hardcoding the value, you should get it from <limits.h> - for example, if i is an int, use INT_MAX.

  8. #8
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I did something similar with my terrain map generator. It had 144,000 values to check and I wanted to know the current peaks and valleys. For the valleys, you'd have a value that is much beyond what would be expected above. The mountains go a bit above 22,000 feet so I set it to 99,999 feet for the minimum (it's like any mountain (except Olympus Mons) would be that high). The same goes for the peak - I set it to a value well below what I intended on - -9999 feet. This way, if I scan within an area, the values will always be what I expect for minimums and maximums. The only other alternative is to just use the first element in the array as the minimums and maximums then as the array is scanned, they are set accordingly. Both methods would produce the expected results regardless of where you start in the array.

    @robatino: How can an array be empty? Defining one as "sample[0]" seems rather meaningless.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  9. #9
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    1. What is the return value of the function if the array is empty ? This is an important choice and might influence the used algorithm greatly.

    2. Another simple idea is to sort the array ascending in which case the smallest number is the first one.

    3. In case the array is not empty, the following does what you want:

    Code:
    int i;
    int smallestNumber = myArray[0];
    for (i = 1; i < myArraySize; i++)
      if (myArray[i] < smallestNumber)
        smallestNumber = myArray[i];
    Additionally, I usually save the index of the smallest number and not the number itself, in which case the algorithm looks like:
    might influence the used algorithm greatly.

    2. Another simple idea is to sort the array ascending in which case the smallest number is the first one.

    3. In case the array is not empty, the following does what you want:

    Code:
    int i;
    int smallestIndex = 0;
    for (i = 1; i < myArraySize; i++)
      if (myArray[i] < myArray[smallestIndex])
        smallestIndex = i;
    Obviously, with the last algorithm, the smallest number is myArray[smallestIndex].

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > @robatino: How can an array be empty? Defining one as "sample[0]" seems rather meaningless.
    If it's a dynamic array with size determined at runtime, it's perfectly plausible that it could be empty. For example, if a set is partitioned into subsets, then some of the subsets may be empty even if the original set isn't. In this case, an algorithm that computes the minimum by taking the minimum of the minimums for each of the subsets will work only if the minimum handles empty sets properly.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The first reply in this thread had the easiest/most efficient answer, and you guys are still going at it...

    There's no need to find some out of range value as the initializer. Like OnionKnight said, just initialize it with the first element in the array. If that first element happens to be the smallest number, great. Otherwise, it will be found while iterating through the array.

    You should never be trying to find the smallest number in an empty array. You should have a special check prior to that logic to see if the array is empty. But that's not what the OP was asking about.
    Last edited by itsme86; 06-15-2007 at 09:50 AM.
    If you understand what you're doing, you're not learning anything.

  12. #12
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by MacGyver View Post
    If I give you an int array of size 5 with the following numbers, what would your code print?

    Code:
    1, 2, 3, 4, 5
    Assuming you fix the > to be <, it would print 0.
    yeah thats right i've tried that
    but i didn't get that point
    Assign the first element of array to minimum_value and start the loop with i=1.
    -snip-
    assign the first element of the array to minimum_value
    -snip-
    you mean for example like this
    Code:
    for(i=1;i>elements number;i++)
        {
            if(array[x]<array[0])
              {
                 array[0]=array[x];
              }
        }
    sorry but i don't have a compiler right now

  13. #13
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    lol
    Code:
    #include<stdio.h>
    int main()
    {
    
    int x;
    
    int array[]={20,33,4563,445645,545645,64566,55456,4456,645645,445,65,54,65,456454};
    
         for(x=1;x<14;x++)
          {
                if(array[x]>array[0])
                    {
                        array[0]=array[x];
                     }
           }
    
       printf("The Smallest Value In The Array Is &#37;d",array[0]);
    
    return 0;
    }
    it works
    and i tried to work on my indentation
    but why it didn't work when i used the first example
    maybe because i assumed that the smallest value wouldn't be smaller than 0??
    and i guess that OnionKnight's idea is some kind of sorting right ?
    Last edited by St0rM-MaN; 06-15-2007 at 02:33 PM.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That only works because the smallest value in the array happens to be the first element. You don't want to have to change the values in your array.

    Change your for loop to:
    Code:
    for(smallest = array[0], x = 1;x < 14;++x)
      if(array[x] < smallest)
        smallest = array[x];
    If you understand what you're doing, you're not learning anything.

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    No, I believe he means:

    Code:
    int i = array[0];
    
    for(int j=1;j<arraySize;j++)
    {
       if (i > array[j])
       {
          i = j;
       }
    }
    or something similar.

    edit: or what itsme86 said for more compact
    Last edited by markcole; 06-15-2007 at 01:52 PM. Reason: itsme86's code is better

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-14-2009, 01:39 PM
  2. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  3. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM
  4. Finding the lowest number of a certain input
    By theorbb in forum C Programming
    Replies: 26
    Last Post: 04-17-2006, 02:36 AM
  5. Finding the smallest value......HARD!!!
    By AssistMe in forum C Programming
    Replies: 21
    Last Post: 03-10-2005, 06:23 AM