Thread: Finding Absolute Value in C

  1. #1
    Unregistered
    Guest

    Question Finding Absolute Value in C

    Hello!
    Learning C--writing a program that tells if an entered # is positive/negative and gives the absolute value. I have the pos./neg. part all set, but I am needing help with the AV part. C did not understand:
    absval=|number|
    So I was wonder how can I get C to understand an AV?
    Thanks in advance
    :-)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    IIRC, an absolute value is the positive version of the number. Thus, if you enter 1, the absolute value is 1. If you enter -2, the absolute value is 2.

    a=b<0?0-b:b;

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

  3. #3
    Unregistered
    Guest

    Smile

    Thanks!
    It works now!

  4. #4
    Unregistered
    Guest

    Arrow

    umm, one last thing,
    what would be the longhand of:
    a=b<0?0-b:b;
    I mean, using if and elses.
    thanks

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    umm, one last thing,
    what would be the longhand of:
    a=b<0?0-b:b;
    I mean, using if and elses.
    thanks
    That's a bit too easy, you should try it yourself

    Look, this how the shorthand works:

    >Result = (TestMe)?ReturnThisIfTrue:ReturnThisIfFalse;

    Now, I'm sure you know, or can easily find out how the long hand structure is made up. Translation from one to the other is then easy.

    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Absolute value of each element in a given array
    By DriftinSW20 in forum C Programming
    Replies: 9
    Last Post: 11-15-2007, 04:08 PM
  3. Finding primes
    By starripper in forum C++ Programming
    Replies: 19
    Last Post: 01-14-2006, 04:17 PM
  4. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM