# Thread: Print the 2 greater numbers of 10 numbers.

1. ## Print the 2 greater numbers of 10 numbers.

Code:
```/*C Program must enter 10 numbers and print the 2 greatest ones using only while and if.
i.e.

1 2 3 4 5 6 7 8 9 10

greatest numbers are: 10 and 9.

This is what I've done so far:

*/

#include <stdio.h>
#include <conio.h>

main()
{
int num, greater, greater2, counter;
counter = 0;

printf("\Enter number and I will tell you which\n");
printf("ones are the two largest numbers of ten: ");
scanf("%d", &num);

greater = num;

counter ++;

while (counter < 10){
printf("\nEnter number and I will tell you which\n");
printf("ones are the two largest numbers of ten: ");
scanf("%d", &num);

if (num > greater) {
greater2 = num;
greater = greater;
}
else{
greater2 = greater;
}
{
counter ++;
}
}
printf("\n\nThe two largest numbers are: %d and %d\n",  greater2, greater);
getch();
return 0;
}

/* Any help????? */```

2. I think in your if statement you want greater2( second largest?) to be assigned greater's value and greater is then assigned num.

Your unconditional else statement is going to mess you up. If greater is 25 and greater2 is 15 and I enter 20, what is your program going to do?

3. Originally Posted by joelro79
Code:
```/*C Program must enter 10 numbers and print the 2 greatest ones using only while and if.
i.e.

1 2 3 4 5 6 7 8 9 10

greatest numbers are: 10 and 9.

This is what I've done so far:

*/

#include <stdio.h>
#include <conio.h>

main()
{
int num, greater, greater2, counter;
counter = 0;

printf("\Enter number and I will tell you which\n");
printf("ones are the two largest numbers of ten: ");
scanf("%d", &num);

greater = num;

counter ++;

while (counter < 10){
printf("\nEnter number and I will tell you which\n");
printf("ones are the two largest numbers of ten: ");
scanf("%d", &num);

if (num > greater) {
greater2 = num;
greater = greater;
}
else{
greater2 = greater;
}
{
counter ++;
}
}
printf("\n\nThe two largest numbers are: %d and %d\n",  greater2, greater);
getch();
return 0;
}

/* Any help????? */```
Your immediate problem seems to be the lines marked in red.

Also consider how you will account for negative numbers (eg maybe set greater/greater2 to num on the first time round the while loop) You may not need the else statement either.

4. Assume greater is the largest number and greater 2 is the second largest. Each new number will fit into 1 of 3 categories :

1) larger than both greater and greater2
2) in between greater and greater 2
3) smaller than both greater and greater2

Figure out how to update greater and greater2 in all 3 cases. I'll help with case 3 - you want to do nothing. The other two are up to you.

5. Originally Posted by KCfromNC
Assume greater is the largest number and greater 2 is the second largest. Each new number will fit into 1 of 3 categories :

1) larger than both greater and greater2
2) in between greater and greater 2
3) smaller than both greater and greater2

Figure out how to update greater and greater2 in all 3 cases. I'll help with case 3 - you want to do nothing. The other two are up to you.
I'm wondering if professors are teaching their students to write out psudocode like this anymore? It seems to me that psudocode and flow charts are the greatest tools in helping someone work out problems like this. Do the paperwork first, and then the code is so much easier. Figure out how the computer is going to think through your instructions, and then type them out correctly, and you only have to write your code once. It saves a lot of headache and time in the long run.

Here is an example of some psudocode that is related to, but does not solve your problem. Say you are comparing three numbers to find the greatest:

Code:
```get num1
get num2
get num3

if num1 is greater than num2 and num3,
num1 is greatest.
else if num2 is greater than num1 and num3
num2 is greatest.
else
num3 is greatest```
Apply similar logic and thinking to the situations KCfromNC proposed BEFORE you build your code and you should solve the problem in no time flat.

6. The logic for doing 3 numbers specifically doesn't necessarily work for 10 numbers.
Code:
```for each number
if new number bigger than highest number
second highest = highest
highest = new
elseif new number bigger than second highest
second highest = new```
But that will work for any number of numbers.

Quzah.

7. Originally Posted by quzah
The logic for doing 3 numbers specifically doesn't necessarily work for 10 numbers.
Quzah.
I understand this. I said it was similar and related, not that it would solve his problem. I pruposly did not want to hand him the answer to his homework. I thought it would be better for him to think it through for himself. My example was just an example of psudocode.