Beginner Programming Help

This is a discussion on Beginner Programming Help within the C Programming forums, part of the General Programming Boards category; I'm trying to write a program that gives me the largest of three inputted integers. I've been trying to make ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    Question Beginner Programming Help

    I'm trying to write a program that gives me the largest of three inputted integers. I've been trying to make different functions with arguments and comparisons and none of them seem to be consistently giving me the largest integer. Some of the things i've been trying are

    if(a > b > c)
    max = a;
    if (b > a > c)
    max = b;
    if (c > a > b)
    max = c;

    and another one

    max = a;
    if (max < b)
    max = b;
    if (max < c)
    max = c;
    return max;

    and another one

    if (x > y > z)
    return x;
    else if (y > x > z)
    return y;
    else
    return z;

    I've tried all of these and whenever I put in any 3 integers, it usually always gives me the first or last entered integer back, not the largest one. If anybody can help, it would be a really big help. Thanks in advance.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You can't string logical comparisons together like that.

    To understand why, you have to understand exactly what those operators are doing.

    Every time you do something like

    a < b

    the result is 1 if the expression is true and 0 if the expression is false.

    If you, in turn, try to do

    a < b < c

    then a < b will evaluate to either a 1 or a 0 (true or false), and then that 1 or 0 will be compared with c.

    What you really want to do is use the logical AND operator which is &&

    a < b && b < c // a is less than b AND b is less than c


    however, in this case, there is a better way of accomplishing what you want:

    Code:
    if( a > b )
        if( a > c )
            max = a;
        else
            max = c;
    else
        if( b > c )
            max = b;
        else
            max = c;
    or, if you want to learn the ?: operator, it can also be done like this

    max = ( a > b ? ( a > c ? a : c ) : ( b > c ? b : c ) );

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    it works now!! thanks so much for the help and thanks for the C lesson!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Best Free Beginner Online Books/Tutorials?
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2004, 05:52 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21