Amazon.com Widgets Problem with Exercise 13.4(ARC)
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 20, 2014, 07:11:39 PM
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
| | | |-+ Problem with Exercise 13.4(ARC)
Pages: [1] Go Down
Print
Author Topic: Problem with Exercise 13.4(ARC) (Read 746 times)
Anatoliy Gatt
Newbie
*
Posts: 8


Macro - Web is the future...


WWW




on: March 30, 2012, 11:44:58 PM

Hi guys!
I'm stuck on the Exercise 13.4

So here is the code:
Function
Code: (Objective-C)
Fraction *calculateFractions (Fraction  **array, int numberOfElements) {
    Fraction *resultFraction = [[Fraction alloc] initWithNumerator:0 andDenominator:1];
    Fraction *holder;
   
    for(int i = 1; i < numberOfElements; i++) {
        holder = [resultFraction add:array[i]];
        resultFraction = holder;
    }
   
    return resultFraction;
}
Code in main
Code: (Objective-C)
const int numberOfFractions = 5;
       
        Fraction *fractionArrayElementOne = [[Fraction alloc] initWithNumerator:5 andDenominator:10];
        Fraction *fractionArrayElementTwo = [[Fraction alloc] initWithNumerator:8 andDenominator:13];
        Fraction *fractionArrayElementThree = [[Fraction alloc] initWithNumerator:7 andDenominator:15];
        Fraction *fractionArrayElementFour = [[Fraction alloc] initWithNumerator:9 andDenominator:23];
        Fraction *fractionArrayElementFive = [[Fraction alloc] initWithNumerator:4 andDenominator:17];
       
        Fraction __autoreleasing *arrayOfFractions[numberOfFractions]= {fractionArrayElementOne, fractionArrayElementTwo, fractionArrayElementThree, fractionArrayElementFour, fractionArrayElementFive};
       
        printf("Fraction Expression: %i/%i + %i/%i + %i/%i + %i/%i + %i/%i = ", arrayOfFractions[0].numerator, arrayOfFractions[0].denominator, arrayOfFractions[1].numerator, arrayOfFractions[1].denominator, arrayOfFractions[2].numerator, arrayOfFractions[2].denominator, arrayOfFractions[3].numerator, arrayOfFractions[3].denominator, arrayOfFractions[4].numerator, arrayOfFractions[4].denominator);
       
        Fraction *calculateFractions (Fraction **array, int numberOfElements);
       
        Fraction *resultOfFractionArrayCalculation = calculateFractions(arrayOfFractions, 5);
       
        printf("%i/%i\n", resultOfFractionArrayCalculation.numerator, resultOfFractionArrayCalculation.denominator);

Output
Fraction Expression: 5/10 + 8/13 + 7/15 + 9/23 + 4/17 = 1/0

What is the problem?
Why it is 1/0?

Thanks in advance!!!
Logged
Anatoliy Gatt
Newbie
*
Posts: 8


Macro - Web is the future...


WWW




Reply #1 on: April 01, 2012, 07:09:21 AM

Would I get any help?
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.