Amazon.com Widgets Confused on why I can't get more iteration than 16
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 07:44:44 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
| | | |-+  Confused on why I can't get more iteration than 16
Pages: [1]   Go Down
Print
Author Topic: Confused on why I can't get more iteration than 16  (Read 689 times)
kivery
Newbie
*
Posts: 3






« on: July 21, 2011, 09:20:06 AM »

Ok I've read all the post and I've tried a few of the suggestions.  However, I still can't get pass 16 iterations.  Can someone please help?



#import <Foundation/Foundation.h>



@interface Fraction : NSObject

{
   int numerator;
   int denominator;
}


@property int numerator, denominator;

-(void) print; //Print will be called for mixed numeral fraction display.
-(void) print: (BOOL) isReduce;
-(void) setTo: (int) n over: (int)  d;
-(double) converToNum;
-(Fraction *) add:(Fraction *) f;
-(Fraction *) subtract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;
-(void) reduce;


@end
----------------------------------------------------------------------------


#import "7.3.h"


@implementation Fraction

@synthesize numerator, denominator;

-(void) print  //to display mix numeral fraction
{
   int whole;
   
   whole = numerator / denominator;
   numerator %= denominator;
   
   NSLog(@" %i %i/%i", whole, numerator, denominator);
}


-(void) print: (BOOL) isReduce
{   
   
   if (denominator == 1)
      NSLog(@"%i", numerator);
   else if (numerator == 0)
      NSLog(@"0");
   else if (isReduce)
   {
      Fraction *result = [[Fraction alloc] init];
      [result setTo: numerator over: denominator];
      [result reduce];
      NSLog(@"%i/%i", result.numerator, result.denominator);
      // release result in main
   }
   else
      NSLog(@"%i/%i", numerator, denominator);   
}


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

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

-(Fraction *) add: (Fraction *) f
{
   //To add two fractions:
   // a/b + c/d = ((a * d) + (b * C) / (b * d)
   
   //result will store the result of the addition
   
   Fraction *result = [[Fraction alloc] init];
   
   int resultNum, resultDenom;   
   
   resultNum = numerator * f.denominator
   + denominator * f.numerator;
   resultDenom = denominator *f.denominator;
   
   [result setTo: resultNum over: resultDenom];
   [result reduce];
   
   return result;
}

-(Fraction *) subtract: (Fraction *) f
{   
   //To subtract two fractions:
   // a/b c c/d = ((a * d) - (b * C) / (b * d)
   
   //result will store the result of the addition
   
   Fraction *result = [[Fraction alloc] init];
   
   int resultNum, resultDenom;      
   
   resultNum = numerator * f.denominator
   - denominator * f.numerator;
   resultDenom = denominator *f.denominator;   
   
   [result setTo: resultNum over: resultDenom];
   [result reduce];
   
   return result;   
}

-(Fraction *) multiply: (Fraction *) f
{
   
   //To multiply two fractions:
   // (a * c) / (b * d)
   
   //result will store the result of the addition
   
   Fraction *result = [[Fraction alloc] init];
   
   int resultNum, resultDenom;   
      
   resultNum = numerator * f.numerator;
   resultDenom = denominator *f.denominator;
      
   [result setTo: resultNum over: resultDenom];
   [result reduce];
   
   return result;
}

-(Fraction *) divide: (Fraction *) f
{
   
   //To divide two fractions:
   // (a * d) / (b * c)
   
   //result will store the result of the addition
   
   Fraction *result = [[Fraction alloc] init];
   
   int resultNum, resultDenom;      

   resultNum = numerator * f.denominator;
   resultDenom = denominator *f.numerator;
      
   [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
---------------------------------------------------------------------------------


#import <Foundation/Foundation.h>
#import "7.3.h"

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   Fraction *aFraction = [[Fraction alloc] init];
      
   Fraction *sum = [[Fraction alloc] init];
   Fraction *sum2 =[[Fraction alloc] init];
   
   
   int i, n, pow2;
   
   [sum setTo: 0 over: 1]; //sets first fraction to 0
   
   sum2 = 0;
   
   NSLog(@"Enter the value for n");
   scanf( "%i", &n);
   
   pow2 = 2;
   for (i = 1; i <= n; ++i)
   {
      [aFraction setTo: 1 over: pow2];
      sum2 = [sum add: aFraction];
      [sum release];       //release previous sum
      sum = sum2;
      pow2 *= 2;
   
   }
   
   
   NSLog (@"After the %i iterations, the is is %Lg", n, [sum converToNum]);
   
   [sum print: YES];
   [aFraction release];
   [sum release];
   
    [pool drain];
    return 0;
}

How can I change this program to get past 16 iterations?

Any Help is greatly appreciated
Logged
dharr19
Full Member
***
Posts: 175


Email




« Reply #1 on: July 25, 2011, 04:31:50 PM »

Hi,

Looks like a typo.  Here sum2 is set to 0

   int i, n, pow2;
   
   [sum setTo: 0 over: 1]; //sets first fraction to 0
   
   sum2 = 0;

   

David
« Last Edit: July 25, 2011, 04:34:55 PM by dharr19 » Logged
skochan
Administrator
Hero Member
*****
Posts: 3104







« Reply #2 on: July 25, 2011, 07:05:23 PM »

BTW, the correct format is %lg (not %Lg).  After a certain number of iterations, the capacity of your integers will overflow.   A 32-bit integer can only store a maximum positive integer of 2 ^ 16 - 1 (or 65535).  If you want a greater range of values, you can try using int's, long int's or  long long int's in your program.

Cheers,

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