Exercise 12 - 8 ( Last one of the chapter!!! )

Discussion in 'Mac Programming' started by mdeh, Jan 27, 2009.

  1. macrumors 6502

    Joined:
    Jan 3, 2009
    Messages:
    344
    #1
    Could I please get some feedback from the group. I see from the other reply to my query, that this type of thing is not used much...but I suppose the experience does give one some new insights into the workings of the language...although not strictly speaking Obj-c I suppose.
    I have changed the printf function to NSLog to comply with the authors wish to only use functions already presented in the book. ( I think printf is a holdout from the 1st edition.

    Code:
    #import <Foundation/Foundation.h>
    
    #define PRINTINT(var)  NSLog(@ "x%i = %i", var, var)
    #define PRINTX(n)   PRINTINT( x ## n)
    #define NAMEVAR(n)  int x ## n
    #define INITIALIZEVAR(n)  NAMEVAR(n) = n
    
    
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    	
    	int i;
    	for ( i = 1; i <= 100; i++){
    		INITIALIZEVAR(i);
    		PRINTX(i);
    		}
    		
    	[pool drain];
        return 0;
    }
    sample output:

    It feels kind of sloppy to me, and does not exactly comply with the code asked for, but it does seem to work. Anyway, that closes out that chapter!! Yeh :)
     

Share This Page