Thread: smallest digit's

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    smallest digit's

    I can find smallest numbers in array of integer but How to find second, third, fourth, fifth smallest number i?
    Code:
    #include <stdio.h> 
    int main()
    {
        char array[5] = {4, 5, 9, 1, 2};
    	int smallest = array[0];
    	
    	int i= 1;
    	
    	for (i=1; i<5; i++)
    
    
            {         
    		  if (smallest > array[i])	
    		  {
    			  smallest = array[i];
    			  
    			   printf (" %d", smallest);	
    		  }
             		  
            }
           
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You may find this problem easier to solve if you sort the array, although depending on your exactly requirements you don't necessarily have to sort the array, or at least not fully.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by laserlight View Post
    you don't necessarily have to sort the array, or at least not fully.
    How to do it without sorting array


    find first smallest number in array
    find second smallest number in array
    find third smallest number in array
    find fourth smallest number in array
    find fifth smallest number in array

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vajra11
    How to do it without sorting array
    What are your exact requirements, i.e., can there be duplicate values in the array, and if so, what does "second smallest" mean if the smallest value appears twice?

    Are you only interested in finding say, the second smallest value, or the third smallest, etc, and that's it? Or do you want to in fact find the smallest, and the second smallest, and the third smallest, etc? If it is the latter, then sort the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I know that you haven't answered in a few days and have probably moved on, but I think that laserlight's suggestion of a sorting algorithm will be the best way.

    Even if you have finished your assignment, looking at sorting arrays now will make life easier for you later.

    There are a few different types, some quicker than others...

    I suggest that a good one to look at first is the bubble sort - Bubble sort - Wikipedia
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 10-25-2017, 06:12 AM
  2. How to display single digit int as four digit C++ Programming
    By Sloganathan in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2012, 11:30 AM
  3. smallest digit of a number
    By lakestar in forum C Programming
    Replies: 1
    Last Post: 11-11-2007, 08:57 PM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  5. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM

Tags for this Thread