Thread: help: simple problem

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    help: simple problem

    hi,

    just started c prog for college. useless at it. need help finding out what this comes to.its for exam.

    three parts:

    a =3, b=2, c=4,

    what values of following if legal:


    b<a<c

    a*b%c+3

    --b*c+a


    need help especially with --b;

    also what does <<a or >>a mean

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    Quote Originally Posted by screamer903
    need help especially with --b;
    -- is unary decrement operator which evaluates to
    Code:
    --b;//same as
    b = b -1;
    So, it'll decrement the value of b first and then only will it return the final value of b.
    Quote Originally Posted by screamer903
    also what does <<a or >>a mean
    Have a look at the shift operators explanation in the FAQs then if there is something specific you're unclear about post back.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >b<a<c
    Legal, but it doesn't do what you may expect. At first glance one might expect this to say "true if b is less than a and a is less than c", but in reality it compares the boolean result of b < a (0 or 1) to c. b is 2 and a is 3, so b < a is true. c is 4, so 1 < 4 is also true.

    >a*b%c+3
    This is an easy one, all operations go from left to right. So it's:
    Code:
    ( ( 3 * 2 ) % 4 ) + 3
    My best code is written with the delete key.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>the boolean result of b < a (0 or 1)
    I was always under the impression that true was represented as anything nonzero, and that it wasn't guaranteed to be 1. That would mean that the statement is totally unpredictable, but I probably missed something somewhere.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by XSquared
    I was always under the impression that true was represented as anything nonzero
    "On input":
    http://www.eskimo.com/~scs/C-faq/q9.2.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I was always under the impression that true was represented as anything nonzero
    Correct. True is non-zero and false is zero. However, boolean expressions are well defined and always evaluate to either 0 or 1.
    My best code is written with the delete key.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It makes for neat little tricks like replacing:
    Code:
    if(a > b)
      c++;
    with:
    Code:
    c += a > b;
    If you understand what you're doing, you're not learning anything.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    That's not nearly as practical as my brilliant binary search tree trick:
    Code:
    struct node {
      T data;
      struct node *link[2];
    };
    
    struct node *insert ( struct node *tree, T data )
    {
      if ( tree == NULL ) {
        tree = make_node ( data );
      } else {
        int dir = data > tree->data;
        tree->link[dir] = insert ( tree->link[dir], data );
      }
    
      return tree;
    }
    It gets better and better as the symmetric cases get longer and longer.
    My best code is written with the delete key.

  9. #9
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Smile Well I Do Have The Explanation

    a*b%c+3

    in this apply the BOMDAS rule
    that is bracket
    of
    multiply
    divide
    add
    substract

    --b*c+a
    --b will decrease the value od b by one unit
    the remaining will Follow bomdas rule

    >>a and <<a are the right shift and left shift operators which will shift the binary blase either right or left

    for example,
    10 = 1010(in binart system)
    >> 10= 0101 =5 (here, the binary digits got shifted to a place right)

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM