Thread: Find the Largest and Smallest Number

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    13

    Unhappy 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;

    }


    PLEASE HELP ME!!! This is one crazy labor day weekend.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    So what exactly is the problem? I don't seen any code for your attempt at picking the largest of the three...

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    I don't know how to use IF statements. I keep on deleting them because they don't work.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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
    What's so hard about that?

    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
    Last edited by twomers; 09-03-2006 at 05:52 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    we have not gone over && so we are not allowed to use them in our program.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Are you allowed to use else if statements?

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    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.
    Last edited by Nightsky; 09-03-2006 at 06:03 PM.

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Some teachers need to quit their jobs and try something else. What are you using for a textbook?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    I'm using C how to program. The 4th Edition.

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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.


    Just say you read ahead and learnt about else's

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    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.

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    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 &&

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    Okay, I thank you for your time.

  15. #15
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  2. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 07:56 AM
  3. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM
  4. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 08:58 PM
  5. Replies: 2
    Last Post: 09-05-2004, 07:13 AM