# Thread: Find the Largest and Smallest Number

1. I tried this but it did not work.

if( x > y) {
if(x > z) {
printf("Largest: %d\n", x);
}
}

I did get the && to work if I were to use that though.

if ( x>y && x>z ){
printf( "Largest: %d\n", x);
}

if ( y>x && y>z ){
printf( "Largest: %d\n", y);
}

if ( z>y && z>x ){
printf( "Largest: %d\n", z);
}

2. Okay, I've got it done. Thanks for your help guys! I really appreciate it!

3. In your test, the first line checks if x is > y - if it is, it moves to the second line, and checks if x is greater than z - if it is, then it prints out that x is the largest, as obiviously, if there are three integers, and it is larger than the other two, than it is is largest. Now lets say x > y, so it checks if x > z, but finds that x is actually < z - the printf statement will not get executed, and obviously x is not the largest. Similarly, if you check if x > y, where x is not > y, then the nested if (x > z) does not even get checked. In this case also then, we see that x is not the largest. So then we need to write some more lines. We need to see if y > x and if so, then we need to see if y > z - if it is, great, print out a message telling the world that y is largest. Otherwise, if either two of those tests fail, then you know that y is not the largest. By this time, you know that by default z must be largest, but since you are not using an else statement, you will want to run the tests on z.

edit::: blech... all this typing and all too late.

4. I did the nesting and i got the program running like I wanted to. I really appreciate your help A LOT! I got two more programs to go and I will be done. Below is what I did, so hopefully it might help someone in the same boat as me in the future.

Code:
```#include <stdio.h>

int main()
{
int x, y, z;
int sum;
int average;
int product;

printf( "Input 3 different integers: ");
scanf( "%d%d%d", &x, &y, &z );

sum = x + y + z;
average = (x + y + z)/3;
product = x * y * z;

printf( "Sum is %d\n", sum );
printf( "Average is %d\n", average);
printf( "Product is %d\n", product );

if( x>y) {
if(x>z) {
printf("Largest: %d\n", x);
}
}

if( y>x) {
if(y>z) {
printf("Largest: %d\n", y);
}
}

if( z>y) {
if(z>x) {
printf("Largest: %d\n", z);
}
}

if( x<y) {
if(x<z) {
printf("Smallest: %d\n", x);
}
}

if( y<x) {
if(y<z) {
printf("Smallest: %d\n", y);
}
}

if( z<y) {
if(z<x) {
printf("Smallest: %d\n", z);
}
}

system ("pause");
return 0;

}```

5. Is the average supposed to be an int?

6. Yeah. Make it a float or a double or something. If it's not one of those, and if the average is 3.141, the average variable will only be 3.

>> system ("pause");
Read the FAQ about using cin.get() and cin.ignore() to avoid using system("pause") commands, unless you have to. The reason why you should try to avoid using system commands is because, apparently, some people like to hack into computers, and make batch files, called pause or cls or whatever, so when you system ("pause"), you inadvertently execute the bat file, and the most popular conclusion is that your entire hard drive is deleted. Not sure how likely that is to really happen, but it's something to keep in mind.

Also, learn to use code tags, as Dave_Sinkula suggested. And if you want your post to "help someone in the same boat as me in the future", use if else statments here on the boards, even if you aren't allowed to use them in your submission, or don't know how.

7. Yeah. Make it a float or a double or something. If it's not one of those, and if the average is 3.141, the average variable will only be 3.
That was my thought exactly, but perhaps they haven't learnt about floating point numbers yet.

so when you system ("pause"), you inadvertently execute the bat file
You could put the whole path in, which wouldn't be infalliable but would be slightly better (and less portable, but then it's unportable to begin with). The best idea would be to read the FAQ as suggested.  Here it is: http://faq.cprogramming.com/cgi-bin/...&id=1043284385 [/edit]

8. >> so when you system ("pause"), you inadvertently execute the bat file
We all make these mistakes though ... right dwks ... haha, I'd forgotten about that ... hope you like the new change ... however, not all of us consider the consequences :/

>> but perhaps they haven't learnt about floating point numbers yet.
floats are number which can have fractions, ie 3.14. You use the word float much like an int -

Code:
```float pi = 3.141;
printf ("PI: %f", pi);```
However, floating point arithmitic isn't ... perfect. 'There's something in the FAQ or the Tutorials about it if you're interested. I'd generally use doubles or something.

9. >> but perhaps they haven't learnt about floating point numbers yet.
floats are number which can have fractions, ie 3.14. You use the word float much like an int -
Evidently it doesn't matter how easy it is.
We cannot use what we have not learned in class.
If it hasn't been taught, they can't use it.

I'd generally use doubles or something.
Doubles are (generally) more accurate than floats. They used to be slower to use than floats (that's why floats exist) but it doesn't matter now.

10. >> >> We cannot use what we have not learned in class.
>> If it hasn't been taught, they can't use it.

I know. But because they haven't learnt it in class doesn't mean that they can't know it. That's why I put it down there. Just as a pointer to what you could/should do.

11. I guess you didn't see this?
we have not gone over && so we are not allowed to use them in our program.
Are you just assuming that?
no, i am not assuming. We cannot use what we have not learned in class. I think its a bunch of bs. My teacher all he does is talk about going to hooters and 69 numbers.

Evidently you can't just "know" something in that class.

12. I KNOW, and I did see it. I wasn't saying that the OP should use what I said for his class. I was saying that it's how it could/should be done. I understand the limitations of the teacher, but I don't see what the OP shouldn't know how it could/should be done, just because his teacher hasn't covered it yet!

Why not 'just "know"' it for himself, but not use it in class? I acknowledge I was unclear, so I clarified in my previous post.

13. Oh, I see. Sorry.