Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 16, 2017, 09:01:54 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Problem with program 7.6  (Read 1898 times)
applefan90
Newbie
*
Posts: 30


« on: March 30, 2011, 10:30:11 PM »

Program 7.6 is giving me some trouble.  I typed in the code, and added in my own personal comments, as well as stuff from the book, to help me understand it better.
And yet, when I run it, i get an error message in the reduce method. "Thread 1: Program received signal: "EXC_ARITHMETIC".  Upon further examination in the debug window, it says that all the variables in the reduce method are zero.

Here's the code (I'll remove my personal comments):

Fraction.h
Code: (Objective-C)
#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;
//-(void)   add: (Fraction *) f;
-(Fraction *) add: (Fraction *) f;
-(void)   reduce;

@end

Fraction.m
Code: (Objective-C)
#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 ((double) numerator) / denominator;
    else
        return 1.0;
}
-(Fraction *) add: (Fraction *) f {
    Fraction *result = [[Fraction alloc] init];
    int resultNum, resultDenom;
   
    //declare the new num / denom variables
    resultNum = numerator * f.denominator + denominator * f.numerator;
    resultDenom = denominator * f.denominator;
   
    //set the new fraction
    [result setTo: resultNum over: resultDenom];
   
    [result reduce];
    return result;
}
//method to reduce the fraction
-(void) reduce {
    int u = numerator; //u turns out to store the gcd
    int v = denominator;
    int temp;
   
    //loop through this process to find the gcd
    while (v != 0) {
        temp = u % v;
        u = v;
        v = temp;
    }
    //divide numerator & denominator by gdc
    //here is where the debug window shows the error
    numerator /= u;
    denominator /= u;
}

@end

main.m
Code: (Objective-C)
/*
 Algorithm to perform the following operation:
 n
 Σ (1/2)^i
 i 1
 
 Sum the values of (1/2)^i, as i increases from 1 --> n
 */

#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;
   
    //set 1st fraction to 0
    [sum setTo: 0 over: 1];
   
    NSLog(@"Enter a value for n:");
    scanf("%i", &n);
   
    pow2 = 0;
    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 %i iterations, the sum is %f", n, [sum convertToNum]);
    [aFraction release];
    [sum release];
   
    [pool drain];
    return 0;
}


Thanks in advance for any answers!
Logged
applefan90
Newbie
*
Posts: 30


« Reply #1 on: March 30, 2011, 10:39:00 PM »

Just to clarify, I only get this error message after inputting a number while the program runs.
Logged
mitchb
Full Member
***
Posts: 154


« Reply #2 on: April 01, 2011, 12:08:29 AM »

On line 25 of your main section you have pow2 = 0; should be pow2 = 2;

Mitch
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
applefan90
Newbie
*
Posts: 30


« Reply #3 on: April 01, 2011, 01:06:15 PM »

That'll do it.  Thank you very much!  Don't know how I missed that.
Logged
imsachinraut
Jr. Member
**
Posts: 51



Email
« Reply #4 on: April 08, 2011, 10:09:11 AM »

Hi Stephen and all,

here is code from main section of program 7.6

for (i = 1; i <= n; ++i)
{
     [aFraction setTo: 1 over: pow2];
     sum2 = [sum add: aFraction];
     [sum release]; // release previous sum
     sum = sum2;
     pow2 *= 2;
}

instead of [sum release], should it have been [sum2 release]
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #5 on: May 19, 2011, 01:30:37 PM »

Please read the stickied post on this topic, which discusses this code in detail: http://classroomm.com/objective-c/index.php?topic=56.0.

Cheers,

Steve
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.