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; I am writing a progam that finds the smallest and second smallest integer and also the largest and second to ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    40

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

    I am writing a progam that finds the smallest and second smallest integer and also the largest and second to largest integer in a list of 20 integer. The program is to read them from a file. I figured out how to find the first smallest and first largest number but i have no idea how to find the second smallest and second largest number. Can someone please help me.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int counter;
    	int num1;
    	int num2;
    	int largest = 0;
    	int smallest;
    	
    	for (counter = 0; counter < 20; counter++);
    	{
    		scanf("%d", &num1);
    		if(num1 < smallest)
    			smallest = num1;
    		
    	}
    	for (counter = 0; counter < 20; counter++)
    	{
    		scanf("%d", &num2);
    		if(num2 > largest)
    			largest = num2;
    	}
    	printf("\nSmallest: %d", smallest);
    	printf("\nLargest: %d", largest);   
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why should it be different than before? Keep track of the current two smallest numbers as you go through, and update as necessary.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    I still cant get it to work. I dont know whats wrong with it.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int counter;
    	int num1;
    	int num2;
    	int num3;
    	int num4;
    	int largest = 0;
    	int slargest = 0;
    	int ssmallest;
    	int smallest;
    	
    	for (counter = 0; counter < 20; counter++);
    	{
    		scanf("%d", &num1);
    		if(num1 < smallest)
    			smallest = num1;
    		
    	}
    	for (counter = 0; counter < 20; counter++)
    	{
    		scanf("%d", &num2);
    		if(num2 > largest)
    			largest = num2;
    	}
    	for (counter = 0; counter < 20; counter++)
    	{
    		scanf("%d", &num3);
    		if (num3 < ssmallest && ssmallest > smallest)
    			ssmallest = num3;
    	}
    	for (counter = 0; counter < 20; counter++)
    	{
    		scanf("%d", &num4);
    		if (num4 > slargest && slargest < largest)
    			slargest = num4;
    	}
    	printf("\nSmallest: %d", smallest);
    	printf("\nSecond Smallest: %d", ssmallest);
    	printf("\nLargest: %d", largest); 
    	printf("\nSecond largest: %d", slargest);  
    	return 0;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have to do it at the same time.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Think about what you are setting num1 and smallest to, in your first for loop. You are not using the counter variable at all which you should be. Also this can all be done within one for loop as well and not 4 separate loops. Keep track of your smallest and largest as tabstop mentioned and make use of temp variable to hold your values.

    Write out some pseudocode first on paper and map out the logic of what should be occuring.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    i wrote the algorithme and put it into one loop but i still cant get it to work. If you could give me some more advice i would greatly appreciate it.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int counter;
    	int num1;
    	int num2;
    	int num3;
    	int num4;
    	int largest;
    	int slargest;
    	int ssmallest;
    	int smallest;
    	
    	for (counter = 0; counter < 20; counter++);
    	{
    		scanf("%d", &num1);
    		if(num1 < smallest)
    			smallest = num1;
    		else if (largest > num1)
    			largest = num1;
    			
    		if (ssmallest < num1 && ssmallest > smallest)
    			ssmallest = num1;
    		else if (slargest > num1 && slargest < largest)
    			slargest = num1;
    	}
    	
    	printf("\nSmallest: %d", smallest);
    	printf("\nSecond Smallest: %d", ssmallest);
    	printf("\nLargest: %d", largest); 
    	printf("\nSecond largest: %d", slargest);  
    	return 0;
    }

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    At a quick glance, you should be setting

    Code:
    largest < num1  then
    
      largest = num1
    If the prior largest value was in a temporary variable, say largest_temp, then set

    Code:
    slargest = largest_temp;

    This is a bit if rough psedocode, but I hope you get the idea. Only replace your ssmallest or slargest only if the smallest or largest values were changed.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    im sorry can you give a little bit more advice on how to code it. I have very little idea of what you are talking about.

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Well you really do not need a temp variable. So how about this
    I would encourage you to set all your variables to 0 as well. Perhaps a while loop would be more preferred over a for loop for your user inputs.


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

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    for some reason my program stil doesnt work, it wont execute, it shows a problem. can you please tell me how to fix the problem.
    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int counter = 0;
    	int num1;
    	int largest = 0;
    	int slargest = 0;
    	int ssmallest = 0;
    	int smallest = 0;
    	
    	while (counter < 20);
    	{
    		scanf("%d", &num1);
    		if (num1 < smallest)
    		{
    			smallest = num1;
    			ssmallest = smallest;
    	    }
    		else if (largest > num1)
    		{
    			largest = num1;
    			slargest = largest;
    		}
    		counter++;
    	}
    	
    	printf("\nSmallest: %d", smallest);
    	printf("\nSecond Smallest: %d", ssmallest);
    	printf("\nLargest: %d", largest); 
    	printf("\nSecond largest: %d", slargest);  
    	return 0;
    }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should set 'smallest' to something large. That way when you enter a number, it has a chance to store it. Otherwise, if you enter numbers 1,2,3,4, it's just going to still have your initial zero there. Also, remove the 'else'. Also, you should be setting up your seconds before you set your ests. Otherwise:

    est = 5
    nd = est

    Means that nd = 5, so you're just copying the same number twice every time, instead of keeping track what used to be there. PLUS, consider this:

    est = 5
    nd = 0
    num = 3

    Num is not greater than the biggest, but is greater than your second, but you'll never actually store it, because the way your check was done.

    Quzah.
    Last edited by quzah; 05-29-2009 at 01:32 PM.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    quzah, totally true, in some tests I ran into that very issue.

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    Sorry i still cant get the program to work. Can you tell me where the problem is please.

    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)
    		{
    			smallest = num1;
    			ssmallest = smallest;
    	    }
    		if (num1 > largest)
    		{
    			largest = num1;
    			slargest = largest;
    		}
    		counter++;
    	}
    	
    	printf("\nSmallest: %d", smallest);
    	printf("\nSecond Smallest: %d", ssmallest);
    	printf("\nLargest: %d", largest); 
    	printf("\nSecond largest: %d", slargest);  
    	return 0;
    }

  14. #14
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Well remove that oddly placed semicolon after the while statement. You may want to still work out the logic of your variable assignments, as what quzah has stated,as your smallest may still to always being 100.

  15. #15
    Registered User ursula's Avatar
    Join Date
    May 2009
    Location
    Madrid, Spain
    Posts
    11
    what if you swap these lines around:
    Code:
    ssmallest = smallest;
    smallest = num1;
    Code:
    slargest = largest;
    largest = num1;
    you want slargest and ssmallest to save the values before you change largest and smallest respectively.

Page 1 of 2 12 LastLast
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