Thread: smallest number in given sequence

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    66

    smallest number in given sequence

    I am trying to find smallest number for every given sequence

    Here is program I wrote but it is not showing me the as I want

    I am stuck with function

    Code:
     #include <stdio.h>
    
    void smallest_list ( int *p );
    
    
    void smallest_list ( int *p )
    {
        int smallest = *p; 
        
        p++;
        
        for ( int i = 1; i < 9; i++)
        {
            if ( smallest > *p ) // check smallest is greater than next 
            {
                smallest = *p;   // yes update smallest 
                
                p++;
            }
        }
        
        printf("\nsmallest = %d ", *p); // print smallest number in list
    }
    
    
    int main(void)
    {
        int buffer[9] = { 6, 2, 8, 5, 3, 9, 7, 1, 4 };
        
        smallest_list ( buffer );
    
    
      return 0;
    }
    smallest = 8

    I have doubt in loop, anyone can help me out

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    Maybe like this:
    Code:
    #include <stdio.h> 
    void smallest_list (int *p, size_t size);
     
     
    void smallest_list (int *p, size_t size)
    {
        int smallest = p[0]; 
         
        for ( int i = 1; i < size; i++)
        {
             // check if current is smaller than previous smallest
            if ( p[i] < smallest) 
            {
                smallest = p[i];   // yes update smallest 
            }
        }
         
        printf("\nsmallest = %d ", smallest); 
    }
     
     
    int main(void)
    {
        #define N  9
        
        int buffer[N] = { 6, 2, 8, 5, 3, 9, 7, 1, 4 };
         
        smallest_list (buffer, N);
     
     
      return 0;
    }

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Please compare this version to yours:
    Code:
    #include <stdio.h>
    
    // Avoid "Magic Numbers"  Use a #define instead!
    #define DIM 9
    
    // The function should do one thing, and one thing only
    int smallest_list ( int *p, int size );
    
    // Pass the dimension of the array
    int smallest_list ( int *p, int size )
    {
        int smallest = p[0];
    
    //    p++;  NO!
    
        // Arrays are referenced 0 - 8, NOT 1 - 8!!!
        for ( int i = 0; i < size; i++)
        {
            if ( smallest > p[i] ) // check smallest is greater than next
            {
                smallest = p[i];   // yes update smallest
    
    //            p++;
            }
        }
    
        return smallest;
    }
    
    
    int main(void)
    {
        int result = 0;
        int buffer[DIM] = { 6, 2, 8, 5, 3, 9, 7, 1, 4 };
    
        // Return the result of the function
        result = smallest_list ( buffer, DIM );
    
        // The printf() should be called here
        printf("\nsmallest = %d\n", result); // print smallest number in list
    
        return 0;
    }
    Last edited by rstanley; 12-11-2021 at 09:55 AM.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    No one showed you what was wrong with your original code. Here are the minimal changes that make your original code work.
    Code:
    #include <stdio.h>
     
    void smallest_list(int *p)
    {
        int smallest = *p; 
        p++;
     
        for (int i = 1; i < 9; i++)
        {
            if ( smallest > *p ) // check smallest is greater than next 
            {
                smallest = *p;   // yes update smallest 
            }
     
    //!! increment p outside the if statement !!//
            p++;
        }
     
    //!! print smallest (not *p) !!//
        printf("smallest = %d\n", smallest); // print smallest number in list
    }
     
    int main()
    {
        int a[9] = { 6, 2, 8, 5, 3, 9, 7, 1, 4 };
        smallest_list(a);
        return 0;
    }
    It makes more sense to pass in the size of the array, though.
    Code:
    #include <stdio.h>
     
    void print_smallest_value(int *p, int size)
    {
        if (size <= 0)
        {
            printf("no elements in array\n");
            return;
        }
     
        int smallest = *p++;
     
        while (--size > 0)
        {
            if (*p < smallest)
                smallest = *p;
            p++;
        }
     
        printf("smallest = %d\n", smallest);
    }
     
    int main()
    {
        int a[] = { 6, 2, 8, 5, 3, 9, 7 };
        print_smallest_value(a, sizeof a / sizeof a[0]);
     
        int b[] = { 93, 37, 32, 65, 78, 54, 23, 43, 56, 76, 45, 51 };
        print_smallest_value(b, sizeof b / sizeof b[0]);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    john.c:

    Except that your minimal correction still skips the first element of the array:
    Code:
    for (int i = 0; i < 9; i++)
    
    NOT
    
    for (int i = 1; i < 9; i++)

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    @rstanley, please look again (first element is dealt with on line 5)
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    May 2021
    Posts
    66
    Thanks to all for showing different approach

    Quote Originally Posted by rstanley View Post
    john.c:

    Except that your minimal correction still skips the first element of the array:
    Code:
    for (int i = 0; i < 9; i++)
    
    NOT
    
    for (int i = 1; i < 9; i++)
    we assume that first number in the array is smallest number. That's why we are not comparing. It's not good idea to compare same number in twice

    @john thanks for correction
    Last edited by Rahul11; 12-11-2021 at 11:58 AM.

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by john.c View Post
    @rstanley, please look again (first element is dealt with on line 5)
    Yes, I understand in this case, but I recommend no beginner user start a for loop that way! Better to set "smallest" to the first element, and scan the entire array anyway.

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by rstanley View Post
    Better to set "smallest" to the first element, and scan the entire array anyway.
    Why?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by john.c View Post
    Why?
    Experienced programmers can "Break the rules" so to speak. They know exactly what they are doing. Look at the number of "goto"s in the Linux kernel source! We usually teach beginners to avoid "goto" in most all places.

    However, when teaching beginners, I emphasize certain "rules", such as ALWAYS NULLing a pointer immediately after freeing allocated memory, to avoid later de-referencing the freed pointer, or a double freeing of the memory.

    The same with getting into the habit of always setting a loop inializer in a for loop to zero.

    I just did this again, in another posting.

  11. #11
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    @rstanley, I don't care about your pointless pedagogical theories. You said that I skipped the first element of the array. You were wrong. Stop whining.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I want to plus largest number and smallest number
    By Doffer in forum C Programming
    Replies: 4
    Last Post: 10-28-2019, 03:05 PM
  2. find the smallest number:
    By jocdrew21 in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2013, 01:56 PM
  3. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 07:56 AM
  4. smallest digit of a number
    By lakestar in forum C Programming
    Replies: 1
    Last Post: 11-11-2007, 08:57 PM
  5. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM

Tags for this Thread