A very basic question

This is a discussion on A very basic question within the C Programming forums, part of the General Programming Boards category; I just started learning C a few days ago, and I bought a book called "C How to Program." As ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    11

    Unhappy A very basic question

    I just started learning C a few days ago, and I bought a book called "C How to Program." As it turns out it was intended to be a college textbook, but I've found it very helpful so far. Right now I'm working on an exercise in which you enter three numbers and the computer is supposed to determine, among other things, which is smallest and which is largest using a single statement if command.

    My first attempt went something like this:

    if ( x < y < z)
    printf( "smallest is %d", x );
    if ( x < z < y )
    printf( "smallest is %d", x );

    and so on for each variable. Obviously I used the same structure for finding the largest number. Clumsy, I admit, but I didn't have much to work with in the way of knowledge. As it turned out, this returned nothing. Absolutely nothing. My first question is why this happened. I guess maybe the if function doesn't handle multiple operators in that format, but it sure as heck didn't tell me that in the book.

    Next I tried another structure, one that I thought of earlier but didn't think would work:

    if ( x < y, x < z )
    printf(...);

    This sort of worked, but it always returned either two smallest and one largest numbers or two largest and one smallest numbers. I checked it out and found that whenever the second condition (the one after the comma) was true, it would satisfy the function and go on to the next line. In other words, the first condition doesn't matter at all.

    Now this may seem very elementary to you all, but it has stopped me dead in my tracks. Any help here would be greatly appreciated, but if you could Socratically lead me in the right direction rather than directly telling me the answer (if that's even possible for such a simple dilemma as this) I would be even more thankful.

    Ash

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    if (x < y < z) is
    evaluated as
    if ((x < y) < z) or if (x < (y < z)).
    What you want to do is use the && operator.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    11
    Nick,

    After I made my post I found the && solution on a C tutorial website, quite by accident. I've tried it, and as you know it works, but the thing that irks me is that I was given no knowledge of that from the book (which is a start from scratch textbook) before the exercise. Nevertheless, I thank you for your help. From the looks of things, I may be bothering you guys a lot with little stuff like this, cause I've already found another problem, which is either a typo in the book or another failure on the part of the author. Does anyone know of a C Board that is specifically geared for beginners? Thanks again.

    Ash

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Yes u r right it is a good book, I have a copy of the 2nd Edition. There is some good stuff in there.
    Last edited by kwigibo; 09-18-2001 at 12:16 AM.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I think that this is a beginning C board. I mean, once you get the syntax down, half of the nonsense here shouldn't be too tough to understand.

    Besides, we like answering questions.

  6. #6
    Unregistered
    Guest
    Hi
    sample code, alter to fit

    #include <stdio.h>

    int x, y;

    int main(void) {
    /* Input the two values to be tested. */

    printf("\nInput an integer value for x: ");
    scanf("%d", &x);
    printf("\nInput an integer value for y: ");
    scanf("%d", &y);

    /* Test values and print result. */

    if (x == y)
    printf("x is equal to y\n");
    else
    if (x > y)
    printf("x is greater than y\n");
    else
    printf("x is smaller than y\n");
    system("pause");
    return 0;
    }

    sall

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    11
    I thank everyone for their kind responses and advice. However, I need to be a little more specific about the criterion for this particular exercise. The only things I am supposed to use are single statement if commands, the equational and relational operators (or whatever they're called: ==, !=, =<, =>, <, >, +, -, /, *, %) and integer variables. I can't use else, &&, or much else for that matter, simply cause I haven't learned it yet. If anyone has a simple solution for this, I would appreciate it, but if not it's no big deal. Thanks again.

    Ash

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Ah, coding without else statements. So you're trying to cut down on jumps, eh? Super leet. ;p

    This is a pretty familiar problem for some reason. I don't think you want the code put in front of your face, so I'll just give you a few lines.
    Code:
    //Blah blah blah
    int min;
    // ...
    min = x;
    // ...
    printf ("The smallest value is %d", min);
    // ...
    That's the organized solution, and likely what they want. Although there's a way to kind of simulate having && with ifs.
    Code:
    // if (A && B) doX();== if (A) if (B) doX();
    if (x < y)
    {
     if (x < z)
     {
      printf ("%d is the tiny one.", x);
     }
    } // The brackets are there for aesthetic purposes only.
    But doing it that way is evil... eeeeevil.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    1
    hmm, if you could use arrays you could use nested if statements to find the minimum of a "range" of numbers, but not sure if you've learned that yet. I'm doing C programming in class and we had to do median/mode and find the min/max stuff...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-09-2008, 09:53 AM
  2. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 03:18 PM
  3. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  4. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM

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