# Thread: Find largest and second largest number (help)

1. ## 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. 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?

3. 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. 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. 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.

6. 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. 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.