Thread: Largest / Smallest (5 integers)

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Antigonish, NS
    Posts
    3

    Largest / Smallest (5 integers)

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    int a,b,c,d,e;
    
    printf( "Enter number for 'a':\n" );
    scanf( "%d", &a );
    printf( "Enter number for 'b':\n" );
    scanf( "%d", &b );
    printf( "Enter number for 'c':\n" );
    scanf( "%d", &c );
    printf( "Enter number for 'd':\n" );
    scanf( "%d", &d );
    printf( "Enter number for 'e':\n" );
    scanf( "%d", &e );
    
    if(a > b && a > c && a > d && a > e);
    printf( "Letter 'a' is the largest number\n", a);
    if(a < b && a < c && a < d && a < e);
    printf( "Letter 'a' is the smallest number\n", a);
    if(b > a && b > c && b > d && b > e);
    printf( "Letter 'b' is the largest number\n", b);
    if(b < a && b < c && b < d && b < e);
    printf( "Letter 'b' is the smallest number\n", b);
    if(c > a && c > b && c > d && c > e);
    printf( "Letter 'c' is the largest number\n", c);
    if(c < a && c < b && c < d && c < e);
    printf( "Letter 'c' is the smallest number\n", c);
    if(d > a && d > b && d > c && d > e);
    printf( "Letter 'd' is the largest number\n", d);
    if(d < a && d < b && d < c && d < e);
    printf( "Letter 'd' is the smallest number\n", d);
    if(e > a && e > b && e > c && e > d);
    printf( "Letter 'e' is the largest number\n", e);
    if(e < a && e < b && e < c && e < d);
    printf( "Letter 'e' is the smallest number\n", e);
    
    return 0;
    }
    This just prints all the printf's. How do I get this to determine which number is largest and which is smallest?

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You have semicolons after your if statements. Don't.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You may want to study up on loops and arrays.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Also, have a read of this FAQ and this FAQ for insight as to why using scanf in the manner you do above is not very wise.

    Additionally, read up on the usage of printf. You are passing a variable, but no format specifier.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    The reason it is printing all the lines instead of just one is because you have a semicolon at the end of each if statement. This is the same as doing:
    Code:
    if(expression)
        ;
    which means: if the expression is true, do nothing.
    So it will check each expression in your if statements, do nothing, then do the printf immediately afterwards.

    Remove the extra semicolons and it will only do one of the printf statements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  2. Determine the largest and the smallest
    By JC33 in forum C++ Programming
    Replies: 6
    Last Post: 12-16-2007, 05:51 AM
  3. largest and smallest
    By eldemonio in forum C Programming
    Replies: 9
    Last Post: 10-15-2007, 02:00 PM
  4. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM
  5. Replies: 16
    Last Post: 09-21-2004, 11:08 PM