comparing numbers

Discussion in 'C Programming' started by Tweaxor, Sep 23, 2003.

  1. Tweaxor

    Tweaxor Guest

    I stuck with exerise in a Learning C book that I got. If I have the
    numbers as input. How can I determine which of the three is the
    smallest number, largest number and the range. Or would it be simpler
    to just find the range of the three numbers.

    Thanks in advance :D
     
    Tweaxor, Sep 23, 2003
    #1
    1. Advertising

  2. Tweaxor wrote:

    > I stuck with exerise in a Learning C book that I got. If I have the
    > numbers as input. How can I determine which of the three is the
    > smallest number, largest number and the range. Or would it be simpler
    > to just find the range of the three numbers.
    >
    > Thanks in advance :D
    >


    #include <stdio.h>

    int min(int num1, int num2);
    int max(int num1, int num2);

    int main()
    {
    int int1, int2, int3;

    printf("Enter three integers, separated by spaces: ");
    scanf("%d%d%d", &int1, &int2, &int3);

    printf("The minimum is %d\n", min (int1, (min (int2, int3))));
    printf("The maximum is %d\n", max (int1, (max (int2, int3))));

    printf("The range is %d\n", max (int1, (max (int2, int3)))
    - min (int1, (min (int2, int3))));

    return 0;
    }

    int min(int num1, int num2)
    {
    if (num1 < num2)
    return num1;
    else
    return num2;
    }

    int max(int num1, int num2)
    {
    if (num1 > num2)
    return num1;
    else
    return num2;
    }
     
    Steve Zimmerman, Sep 23, 2003
    #2
    1. Advertising

  3. Steve Zimmerman wrote:

    > Tweaxor wrote:
    >
    >> I stuck with exerise in a Learning C book that I got. If I have the
    >> numbers as input. How can I determine which of the three is the
    >> smallest number, largest number and the range. Or would it be simpler
    >> to just find the range of the three numbers.
    >>
    >> Thanks in advance :D
    >>


    Homework solution snipped. Are you going to do this guy's day-job for him
    too if he ever graduates?

    > printf("The range is %d\n", max (int1, (max (int2, int3)))
    > - min (int1, (min (int2, int3))));


    Sloppy, but not sloppy enough to get the guy a low mark if that's what you
    had in mind.

    OP: now that Mr Zimmerman has trashed any value that might have remained in
    your homework assignment, see if you can work out ON YOUR OWN why the above
    printf assumes too much.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Sep 23, 2003
    #3
  4. On Tue, 23 Sep 2003 07:00:46 +0000 (UTC), in comp.lang.c , Richard
    Heathfield <> wrote:

    >Homework solution snipped. Are you going to do this guy's day-job for him
    >too if he ever graduates?


    As long as I also get his pay cheque... :)

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
     
    Mark McIntyre, Sep 23, 2003
    #4
  5. Tweaxor

    Mike Wahler Guest

    "Tweaxor" <> wrote in message
    news:...
    > I stuck with exerise in a Learning C book that I got. If I have the
    > numbers as input. How can I determine which of the three is the
    > smallest number, largest number


    Use the comparison operators: '<' means 'less than',
    '>' means 'greater than.

    if(a < b)
    printf("%d is less than %d\n", a, b);

    >and the range. Or would it be simpler
    > to just find the range of the three numbers.


    Not sure what you mean by 'range'. The range of
    numbers between the highest and lowest consists
    of all values between them (and including them if
    your definition of 'range' means 'inclusive').

    The range of possible values for a given numeric
    type can be obtained from a macro of <limits.h>:

    printf("Range of type 'int' is from %d to %d inclusive.\n",
    INT_MIN, INT_MAX);

    -Mike
     
    Mike Wahler, Sep 25, 2003
    #5
  6. Richard Heathfield <> spoke thus:

    >> printf("The range is %d\n", max (int1, (max (int2, int3)))
    >> - min (int1, (min (int2, int3))));


    > OP: now that Mr Zimmerman has trashed any value that might have remained in
    > your homework assignment, see if you can work out ON YOUR OWN why the above
    > printf assumes too much.


    I'm having a tough time figuring out what it assumes that's inadvisable. max
    and min always return integers, so the program will never crash. And as far
    as I can tell, it always produces correct results... so help me out here...

    --
    Christopher Benson-Manica | Jumonji giri, for honour.
    ataru(at)cyberspace.org |
     
    Christopher Benson-Manica, Sep 25, 2003
    #6
  7. Tweaxor

    Mike Wahler Guest

    "Christopher Benson-Manica" <> wrote in message
    news:bkv4bb$pvl$...
    > Richard Heathfield <> spoke thus:
    >
    > >> printf("The range is %d\n", max (int1, (max (int2, int3)))
    > >> - min (int1, (min (int2, int3))));

    >
    > > OP: now that Mr Zimmerman has trashed any value that might have remained

    in
    > > your homework assignment, see if you can work out ON YOUR OWN why the

    above
    > > printf assumes too much.

    >
    > I'm having a tough time figuring out what it assumes that's inadvisable.

    max
    > and min always return integers, so the program will never crash.


    It might not crash (actually nobody said it would, but it could),
    but will it always give accurate results even when 'int1', 'int2'
    and 'int3' each have a valid value? What output do you get from
    the following (at the risk of further annoying Richard :) ) :

    [compile with implementation where sizeof(int) >= 4]

    #include <limits.h>
    #include <stdio.h>

    int min(int lhs, int rhs)
    {
    return lhs < rhs ? lhs : rhs;
    }

    int max(int lhs, int rhs)
    {
    return lhs > rhs ? lhs : rhs;
    }

    int main()
    {

    int int1 = 500000000;
    int int2 = -1000000000;
    int int3 = -2000000000;

    double dmin = int3; /* used below */
    double dmax = int1; /* " " */

    printf("INT_MIN == %11d\n", INT_MIN);
    printf("INT_MAX == %11d\n", INT_MAX);

    printf("int1 == %11d\n"
    "int2 == %11d\n"
    "int3 == %11d\n",
    int1, int2, int3);

    printf("max(%11d, (max(%11d, %11d))) == %11d\n"
    "min(%11d, (min(%11d, %11d))) == %11d\n",
    int1, int2, int3, max(int1, (max(int2, int3))),
    int1, int2, int3, min(int1, (min(int2, int3))));

    printf("The range is %11d\n", max (int1, (max (int2, int3)))
    - min (int1, (min (int2, int3))));


    printf("%.0f - %.0f == %.0f\n", dmax, dmin, dmax - dmin);
    return 0;
    }

    -Mike


    .. And as far
    > as I can tell,


    How far did you test? :)

    >it always produces correct results...


    Not *always*.

    >so help me out here...


    See above.

    -Mike
     
    Mike Wahler, Sep 26, 2003
    #7
  8. Mike Wahler <> spoke thus:

    > How far did you test? :)


    Okay, I didn't ;) Boy, that was more subtle than I thought, but I probably
    still should have seen it... *sheepish*

    > Not *always*.


    Just put in the documentation not to use MAX_INT ;)

    --
    Christopher Benson-Manica | Jumonji giri, for honour.
    ataru(at)cyberspace.org |
     
    Christopher Benson-Manica, Sep 26, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. purifier

    Comparing two numbers

    purifier, Jan 16, 2005, in forum: C Programming
    Replies:
    79
    Views:
    1,570
    Dave Thompson
    Jan 24, 2005
  2. nw
    Replies:
    14
    Views:
    545
    Victor Bazarov
    May 2, 2007
  3. ruds
    Replies:
    3
    Views:
    470
  4. ruds

    Comparing two long numbers

    ruds, Jun 1, 2008, in forum: Java
    Replies:
    35
    Views:
    1,065
    Tom Anderson
    Jun 4, 2008
  5. Adam Majewski

    comparing floating point numbers

    Adam Majewski, Jan 29, 2012, in forum: C++
    Replies:
    12
    Views:
    457
    glen herrmannsfeldt
    Feb 13, 2012
Loading...

Share This Page