Amazon.com Widgets 13-4 with double pointer
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 23, 2014, 03:49:42 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
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 13
| | | |-+ 13-4 with double pointer
Pages: [1] Go Down
Print
Author Topic: 13-4 with double pointer (Read 1127 times)
rocketangel
Newbie
*
Posts: 2






on: September 07, 2010, 10:47:33 PM

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

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int size = 5;

Fraction * fractions[size];
Fraction ** fracPtr = nil; 
Fraction *addFractions( Fraction *array[], int length ); // prototype

// initialization
for (fracPtr = fractions; fracPtr < (fractions + size); fracPtr++) {
*fracPtr = [[Fraction alloc] init];
[*fracPtr setTo:1 over:3];
}

// add
Fraction * result = addFractions( fractions, size );

// print
printf("Sum is %i/%i\n", [result numerator], [result denominator]);

// release
for (fracPtr = fractions; fracPtr < (fractions + size); fracPtr++)
[*fracPtr release];

    [pool drain];
    return 0;
}

Fraction *addFractions( Fraction *array[], int length )
{
Fraction ** fractPtr = array;
Fraction *sum = [[Fraction alloc] init], *temp;
[sum setTo:0 over:1];

for ( ; fractPtr < (array + length); fractPtr++ ) {
temp = [sum add:*fractPtr];
[sum release];
sum = temp;
}

return sum;

}
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.