Thread: Find largest and second largest number (help)

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    Find largest and second largest number (help)

    Hi,

    We were given an assignment recently (not a graded assignment, just something to do if we wanted to study) and it was to write a program without using any arrays, that will take input of a max of 10 numbers, and then at the end, print out the largest and second largest number.

    I have tried quite a few different ways of doing this and I just can't figure out what I'm doing wrong. It grabs and prints the largest successfully but no matter what I do, it always messes up on printing the second largest.

    Code:
    #include <stdio.h> 
    int main() 
    {
    int counter = 1; 
    int largest = 0; 
    int number; 
    int slargest = 0; 
    
    while(counter <= 10){
    	printf("Please enter a positive integer: "); 
    	scanf("%d", &number); 
    	
    	if(number >= 0){
    		if(number > largest){
    			largest = number;
    		}
    		if((number >= slargest)&& (number < largest)){
    			slargest = number;
    		}
    	}
    	counter++;
    }
    
    printf("The largest is: %d\n", largest); 
    printf("The second largest is: %d\n", slargest); 
    
    }
    What am I doing wrong? Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, suppose you have a largest number stored. Now, if the user enters a number larger than the current largest, you store this new number as the largest number... but shouldnt the current largest number now be stored as the new second largest number?
    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
    Jan 2006
    Posts
    5
    Ahh I see exactly what ya mean. If the number that should be the slargest comes before the largest number, it's completely doing away with the slargest instead of storing it somewhere.

    Working on it now..

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    ARGHH! I know it's something simple I'm doing wrong..

    I know this doesn't work, but I've re-structured it to get a better idea of what I'm going to do..

    Code:
    #include <stdio.h> 
    int main() 
    {
    	int counter = 1; 
    	int largest = 0; 
    	int number; 
    	int slargest = 0; 
    
    	while(counter <= 10){
    		printf("Please enter a positive integer: "); 
    		scanf("%d", &number); 
    		counter++;
    		if(number > largest){
    			largest = number;
    			slargest = number;
    			}
    	}
    
    	printf("The largest is: %d\n", largest); 
    	printf("The second largest is: %d\n", slargest); 
    
    }
    If I can only figure out a way of storing it so I'm not overriding it everytime.

    Btw, if you didn't know by now, I'm new to C =/

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You still need to compare if the number is greater than the second largest number.

    Btw, if you didn't know by now, I'm new to C
    This is the C++ programming forum, so be warned that you may be given C++ specific advice that wont work in C.
    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

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Ahh yes, I'm aware. I wasn't very clear in the way I said that. I meant all of the C languages (C#, C++ and C). Sorry.

    I'm a new IT/Web Design student so I really wasn't prepared for any programming classes (other than java and vb) when I decided to go back to college (3rd time). Ended up with a C++ class in my 2nd term, so I'm still learning.

    (Still working on it, heh)

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Yay, I think I got it! Emphasis on the word 'think'...

    Code:
    #include <stdio.h> 
    int main() 
    {
    	int counter = 1; 
    	int largest = 0;
    	int slargest = 0; 
    	int num;
    	while(counter <= 5){
    		printf("Please enter a positive integer: "); 
    		scanf("%d", &num); 
    		counter++;		
    			if(num > largest){
    				slargest = largest;
    				largest = num;
    			}
    			else if (num > slargest){
                slargest = num;
    			}
    	}					
    	printf("The largest is: %d\n", largest); 
    	printf("The second largest is: %d\n", slargest); 
    }
    Thanks a ton for the help.

Popular pages Recent additions subscribe to a feed