Thread: Finding the smallest value

  1. #16
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    yeah actually i have changed the array sorting by replacing this element array[0] with the smallest one
    i didn't mean to
    and i guess it like itsme86 code
    and he is right in "That only works because the smallest value in the array happens to be the first element"
    i tried this
    so
    "Assign the first element of array to minimum_value" i guess i didn't get that
    Last edited by St0rM-MaN; 06-15-2007 at 01:59 PM.

  2. #17
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Code:
    #include<stdio.h>
    int main()
    {
    
    int x;
    int smallest;
    int array[]={33,33,4563,2,545645,64566,55456,4456,645645,445,65,54,65,456454};
        for(smallest = array[0], x = 1;x < 14;++x)
          {       
                if(array[x] < smallest)
                   {
                        smallest = array[x];
                    }
         }
       printf("The Smallest Value In The Array Is &#37;d",smallest);
    
    return 0;
    }
    this works
    thanks itsme86
    i was wrong in x>14 it should be x<14 but i didn't see this
    Last edited by St0rM-MaN; 06-15-2007 at 02:34 PM.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > 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.

    It's simpler to have a single piece of code that handles both the empty and non-empty cases. That's why standard library functions which act on arrays are always written to be able to handle empty arrays - so the user isn't forced to do a special check every time they use them. This is also why dynamic arrays of length 0 are explicitly allowed. Now if the OP knows that the length of the array is nonzero (for example, if it's a static array, for which zero length isn't allowed), that's fine, but it wasn't specified, and if someone else comes along and reuses the code someplace where the same guarantee doesn't hold anymore, it won't work properly.

  4. #19
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    . That's why standard library functions which act on arrays are always written to be able to handle empty arrays
    Try passing an "empty array" to strlen() and tell me what happens.
    If you understand what you're doing, you're not learning anything.

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > Try passing an "empty array" to strlen() and tell me what happens.
    You mean a C string with just a single '\0'? Because strlen() acts on C strings, not general arrays, and it assumes that the trailing '\0' is there. For a valid example, try calling something like qsort() or bsearch() with an empty array.

    Edit: Or any of memcpy(), memmove(), memcmp(), memchr(), or memset(), since these act on character arrays, not C strings, and the length of the array is passed as an argument.
    Last edited by robatino; 06-15-2007 at 03:55 PM.

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