Amazon.com Widgets Trouble with 7.4
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 23, 2014, 09:57:16 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
| |-+ Exercises
| | |-+ Chapter 7
| | | |-+ Trouble with 7.4
Pages: [1] Go Down
Print
Author Topic: Trouble with 7.4 (Read 925 times)
gbrandon
Newbie
*
Posts: 5






on: March 10, 2012, 01:00:33 PM

I am trying yo get Ex 7.4 to work.  But for some odd reason, I am

Here is my print method:


-(void) print
{
    BOOL shouldReduce;
   
    shouldReduce = YES;
   
    if (shouldReduce == YES)
    {
        int u = numerator;
        int v = denominator;
        int temp;
       
        while (v != 0) {
            temp = u % v;
            u = v;
            v = temp;
        }
        numerator /= u;
        denominator /= u;
    }
    int whole;
    int remain;
    if (numerator == denominator)
    {
        NSLog (@"1");
    }
    else if (numerator > denominator)
    {
        whole = numerator / denominator;
        remain = numerator % denominator;
        if (remain == 0)
            NSLog (@"%i", whole);
        else if (whole == 0)
            NSLog (@"%i/%i", remain, denominator);
        else
            NSLog (@"%i %i/%i", whole, remain, denominator);
    }
    else
    {
        NSLog (@"%i/%i", remain, denominator);
    }
}

And below is my output when I run 5/4 and 3/2 as aFraction and bFraction. For some reason the divide give a bizarre number in the numerator.  But it works just fine with some other numbers.  Thanks for any help.


2012-03-10 14:54:36.129 FractionTest[1480:707] 1 1/4
2012-03-10 14:54:36.132 FractionTest[1480:707] +
2012-03-10 14:54:36.132 FractionTest[1480:707] 1 1/2
2012-03-10 14:54:36.133 FractionTest[1480:707] =
2012-03-10 14:54:36.134 FractionTest[1480:707] 2 3/4
2012-03-10 14:54:36.136 FractionTest[1480:707] 1 1/4
2012-03-10 14:54:36.142 FractionTest[1480:707] -
2012-03-10 14:54:36.143 FractionTest[1480:707] 1 1/2
2012-03-10 14:54:36.143 FractionTest[1480:707] =
2012-03-10 14:54:36.144 FractionTest[1480:707] 1/-4
2012-03-10 14:54:36.145 FractionTest[1480:707] 1 1/4
2012-03-10 14:54:36.145 FractionTest[1480:707] *
2012-03-10 14:54:36.146 FractionTest[1480:707] 1 1/2
2012-03-10 14:54:36.147 FractionTest[1480:707] =
2012-03-10 14:54:36.147 FractionTest[1480:707] 1 7/8
2012-03-10 14:54:36.148 FractionTest[1480:707] 1 1/4
2012-03-10 14:54:36.148 FractionTest[1480:707] /
2012-03-10 14:54:36.149 FractionTest[1480:707] 1 1/2
2012-03-10 14:54:36.150 FractionTest[1480:707] =
2012-03-10 14:54:36.150 FractionTest[1480:707] 1634559340/6
Logged
gbrandon
Newbie
*
Posts: 5






Reply #1 on: March 10, 2012, 01:04:08 PM

Disregard.  As soon as I posted this and saw it outside of xcode, the error jumped out at me.  I was running the incorrect numerator at the end of the method.

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