Amazon.com Widgets 7.6 just returns zero
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 31, 2014, 07:06:14 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
| |-+ Program Examples
| | |-+ Chapter 7
| | | |-+ 7.6 just returns zero
Pages: [1] Go Down
Print
Author Topic: 7.6 just returns zero (Read 1364 times)
matverna
Newbie
*
Posts: 1






on: June 22, 2009, 11:04:29 PM

For exercise 7.6 on page 153 of 2d ed...

I've tried typing the whole thing over from scratch, and every time I run it I just get zero, no matter how many iterations I enter.  I've checked over and again, and cannot figure out where the problem is.  Has 'nyone else had this problem?

Fraction.h
#import <Foundation/Foundation.h>

//The Fraction Class

@interface Fraction : NSObject
{
   int numerator;
   int denominator;
}

@property int numerator, denominator;

-(void) print;

-(void) setTo: (int) n over: (int) d;

-(double) convertToNum;

-(Fraction *) add: (Fraction *) f;

-(void) reduce;

@end

------------------------------------------------------------------

Fraction.m

#import "Fraction.h"


@implementation Fraction
@synthesize numerator, denominator;

-(void) print
{
   NSLog(@"%i / %i", numerator, denominator);
}


-(void) setTo: (int) n over: (int) d
{
   numerator = n;
   denominator = d;
}


-(double) convertToNum
{
   if (denominator != 0 )
      return ( numerator / denominator );
   else
      return 1.0;
}


-(Fraction *) add: (Fraction *) f
{
   Fraction *result = [[Fraction alloc] init];
   int resultNum, resultDenom;
   
   resultNum = numerator * f.denominator + f.numerator * denominator;
   resultDenom = denominator * f.denominator;
   
   [result setTo: resultNum over: resultDenom];
   [result reduce];
   
   return result;
}   


-(void) reduce
{
   int u = numerator;
   int v = denominator;
   int temp;
   
   while ( v != 0)
   {
      temp = u % v;
      u = v;
      v = temp;
   }
   
   numerator /= u;
   denominator /= u;
}


@end

---------------------------------------------------------------------------------------------

7-6 Fraction Test.m

#import "Fraction.h"

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

            
   Fraction *aFraction = [[Fraction alloc] init];
   Fraction *sum = [[Fraction alloc] init], *sum2;
   int i, n, pow2;
   
   [sum setTo: 0 over: 1];
      
   NSLog(@"Please enter your number");
   scanf("%i", &n);
   
   pow2 = 2;
   
   for (i = 1 ; i <= n ; ++i)
   {
      [aFraction setTo: 1 over: pow2];
      sum2 = [sum add: aFraction];
      [sum release];
      sum = sum2;
      pow2 *= 2;
   }
   
   NSLog(@"After %i iterations, the result is %g", n, [sum convertToNum]);
   
   [aFraction release];
   [sum release];
      
      [pool drain];
      return 0;
}

-----------------------------------------------------------------------------------------------------------

RESULTS:::::::

[Session started at 2009-06-23 01:50:28 -0400.]
2009-06-23 01:50:28.093 7-6 Fraction Test[6806:10b] Please enter your number
5
2009-06-23 01:50:31.415 7-6 Fraction Test[6806:10b] After 5 iterations, the result is 0

The Debugger has exited with status 0.

---------------------------------------------------------------------------------------------

Any help is appreciated !!

Matias
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: June 23, 2009, 04:18:58 AM

Matiias,

Reread the section in Chapter 4 about integer arithmetic.  This line in your convertToNum method:

Code: (Objective-C)
   return ( numerator / denominator );
               

divides two integers, resulting in truncation.  Since your fractions are all less than 1, this will always produce 0.

Cheers,

Steve Kochan
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.