Finding smallest and second smallest integer and largest and second largest integer

This is a discussion on Finding smallest and second smallest integer and largest and second largest integer within the C Programming forums, part of the General Programming Boards category; If you have not noticed the tricky part here is the assignment of your ssmallest variable. You may never set ...

  1. #16
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    If you have not noticed the tricky part here is the assignment of your ssmallest variable. You may never set this if num1 is never less than smallest again, so you will not get back into your if block. So try this additional if condition:


    Code:
       if ((num1 < ssmallest) && (num1 > smallest))
       {
            ssmallest = num1;
        }

  2. #17
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    i did that and it still doesnt work. Can you please show me the code to run the program.
    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int counter = 0;
    	int num1;
    	int largest = 0;
    	int slargest = 0;
    	int ssmallest = 0;
    	int smallest = 100;
    	
    	while (counter < 20);
    	{
    		scanf("%d", &num1);
    		if (num1 < smallest)
    		{
    			ssmallest = smallest;
    			smallest = num1;		
    	    }
    		if (num1 > largest)
    		{
    			slargest = largest;
    			largest = num1;
    		}
    		counter++;
    	}
    	
    	printf("\nSmallest: %d", smallest);
    	printf("\nSecond Smallest: %d", ssmallest);
    	printf("\nLargest: %d", largest); 
    	printf("\nSecond largest: %d", slargest);  
    	return 0;
    }

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to be checking each possible condition. You need to check to see if it is smaller than the smallest, bigger than the biggest, larger than the smallest but smaller than the second smallest, smaller than the biggest but larger than the second biggest.


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

  4. #19
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You did not make the changes as needed in your last snippet of code. You are almost there. The big question here is if you are understanding the logic path of your if conditionals. Run through this on paper!


    Code:
    if (num1 < smallest)
                    {
                            smallest = num1;
                    }
                    if ((num1 < ssmallest) && (num1 > smallest))
                    {
                      ssmallest = num1;
                    }
                    else if (num1 > largest)
                    {
                            slargest = largest;
                            largest = num1;
                    }


    Output:


    Code:
    $ ./smallest_largest.exe 
    Input number:
    1
    Input number:
    2
    Input number:
    3
    Input number:
    4
    Input number:
    5
    Input number:
    6
    Input number:
    7
    Input number:
    8
    Input number:
    9
    Input number:
    11
    
    Smallest: 1
    Second Smallest: 2
    Largest: 11
    Second largest: 9

  5. #20
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by quzah View Post
    You should set 'smallest' to something large.
    no, you should set smallest (and largest) to the first value read in
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ಠ_ಠ View Post
    no, you should set smallest (and largest) to the first value read in
    Not really. If you do that, your code has to be something like this:
    Code:
    while not done
        if this is the first number read
            set them all to that
        else
            check all the other stuff
    There's not really any point in having an extra IF check every time you run through the loop, when you can just do:
    Code:
    largest = VAR_MIN, smallest = VAR_MAX
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    This is pretty much an academic exercise. If you need to set variables instantly, for testing/checking, then using if conditionals, is necessary. Or one could just have the user input their 20 or 100 numbers as required then sort them, then print out the first two and last two of the sorted array.

  8. #23
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by quzah View Post
    Not really. If you do that, your code has to be something like this:
    Code:
    while not done
        if this is the first number read
            set them all to that
        else
            check all the other stuff
    There's not really any point in having an extra IF check every time you run through the loop, when you can just do:
    Code:
    largest = VAR_MIN, smallest = VAR_MAX
    Quzah.
    It's also much more adaptable, it can handle really small maximum values and really large minimum values

    You would implement it as a priming read
    Code:
    if this is the first number read
      set them all to that
    while not done 
      check all the other stuff
      read
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21