Welcome, Guest. Please login or register.
Did you miss your activation email?
April 25, 2017, 03:27:43 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 12
| | |-+ Chapter 12 - Exercise 7 Solution
Pages: [1] Go Down
Print
Author Topic: Chapter 12 - Exercise 7 Solution (Read 3787 times)
bertobettencourt
Newbie
*
Posts: 45


Email
on: October 29, 2013, 10:26:51 AM

Here i made 2 different macros to calculate your absolute, the second one, is probably the most correct one and goes straight to the point.

Code: (Objective-C)
#import <Foundation/Foundation.h>

#define ABSOLUTE_VALUE(x) if (x < 0) \
NSLog(@"Value is negative, converting: %i", -x); \
else \
NSLog(@"Value is positive, remains: %i", x)


#define ABSOLUTE_VALUE2(x) (x < 0) ? -x : x

int main(int argc, const char * argv[])
{

    @autoreleasepool {
   

        NSLog(@"Testing %i", ABSOLUTE_VALUE2(-5 * 2));

       ABSOLUTE_VALUE(-5 * 2);
       
    }
    return 0;
}

Cheers.
Logged
cary_W
Newbie
*
Posts: 10


Email
Reply #1 on: May 15, 2016, 04:31:03 AM

Code: (Objective-C)
#define ABSOLUTE_VALUE(x) ((x) >= 0 ? (x) : -(x))
this macro will do
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.