Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
June 27, 2017, 01:49:05 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Chapter 12 - Exercise 7 Solution  (Read 4138 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:  

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 ゥ 2009 classroomM.com. All rights reserved.