Find the Largest and Smallest Number

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-03-2006
Nightsky
Find the Largest and Smallest Number
I just started C a few days ago in class and I've been working on a problem for well over 2 hours and still cannot figure it out. I am extremely noobish in this subject, so I was hoping someone can help me with this. I ran searches for about 30 minutes but I still did not understand the programs that some wrote because it is far more advance than I am right now.

The problem states:

Write a program that inputs three different integers from the keyboard, then prints the sum, the product, the average, the smallest and the largest of these numbers. Use only the single-selection form of the if statement.

I've gotten the sum, the product, and average to work but the smallest and the largest is making me go crazy. The teacher wants us to use the IF statements. something like this:

if ( num1 <= num2 ) {
printf( "%d is less than or equal to %d\n", num1, num2 );

This is what I got so far:

#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 );

system ("pause");
return 0;

}

• 09-03-2006
kermit
So what exactly is the problem? I don't seen any code for your attempt at picking the largest of the three...
• 09-03-2006
Nightsky
I don't know how to use IF statements. I keep on deleting them because they don't work.
• 09-03-2006
twomers
Long way:

Code:

```if ( x>y && x>z ) x is biggest else if ( y>x && y>z ) y is biggest else if ( z>x && z>y ) z is biggest```

EDIT - if you don't know how to use them, look at the tutorials on http://www.cprogramming.com

Use something similar for the smallest. But you proved above that you do know how to use the if statments -

Code:

```if ( num1 <= num2 ) { printf( "%d is less than or equal to %d\n", num1, num2 );```
... ?

PS try to use code tags
• 09-03-2006
Nightsky
we have not gone over && so we are not allowed to use them in our program.
• 09-03-2006
kermit
Are you allowed to use else if statements?
• 09-03-2006
twomers
Code:

```if ( x is bigger than y ) {   if ( x is bigger than z )   {     etc.   } }```
>> we have not gone over && so we are not allowed to use them in our program.
Are you just assuming that?
• 09-03-2006
Nightsky
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.

we have not gone over else either. so i have no clue what it is.

i'm reading the link you gave me so hopefully it'll give me more insight.
• 09-03-2006
kermit
Some teachers need to quit their jobs and try something else. What are you using for a textbook?
• 09-03-2006
Nightsky
I'm using C how to program. The 4th Edition.
• 09-03-2006
twomers
I think you should perhaps stick with this site if that's what your teacher is like.

else is exactly what is says!

Code:

```if (something)... do one thing else ... do something different```
else if is the same,

Code:

```if something ... do operation one else if something2 ... do operation two else if something3 ... do operation three else ... do this```
the something should be in ()'s, the the ... do operations should be proper code.

• 09-03-2006
Nightsky
if ( x > y ){
printf( "Largest: %d", x);
}

I got that to compare the two numbers. But for three numbers. I have no clue how to do it the way he wants it. The searches I did pulled up the same things u guys gave me which was using && and else.
• 09-03-2006
kermit
Use some nested if statements like twomers showed above.

Code:

```if(a > c) {     if(a > b) {     puts("a is greatest!");     }   }```
Just go through all of your variables in that way. Its a kludgy way of doing it, but it gets the job done, with no else or &&
• 09-03-2006
Nightsky
Okay, I thank you for your time.
• 09-03-2006
kermit
If you give it a try, and still can't get it, come back and post your best attempt. Someone will likely steer you in the right direction.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last