Amazon.com Widgets Program 7.6 and Exercise 7.3
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 12:49:42 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
| |-+  Webcast Series Part I, Sept. 1 - 24
| | |-+  Questions and Discussion
| | | |-+  Program 7.6 and Exercise 7.3
Pages: [1]   Go Down
Print
Author Topic: Program 7.6 and Exercise 7.3  (Read 908 times)
webwrx
Newbie
*
Posts: 41






« on: September 15, 2009, 04:25:45 PM »

Exercise 7.3 reads -
Quote
Modify program 7.7 7.6 to also display the resulting sum as a fraction, not just as a real number.

I'm having some weirdness with this example program, which is carrying through to this exercise.

The example in the book shows you can enter values for 5, 10, and 15 and get the answers 0.96875, 0.999023, 0.999969 respectively.

I'm getting a 0.96875 for 5, but anything 8 and above I'm just getting 1 as the answer.

My convertToNum method as per the book -
Code: (Objective-C)
-(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}

And the relevant section of int main
Code: (Objective-C)
	Fraction *aFraction = [[Fraction alloc] init];
Fraction *sum = [[Fraction alloc] init], *sum2;
int i, n, pow2;

[sum setTo: 0 over: 1];

NSLog(@"Enter your 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];
sum = sum2;
pow2 *= 2;
}

NSLog (@"After %i iterations, the sum is %g.", n, [sum convertToNum]);
[aFraction release];
[sum release];

Once I sort out what's going on with this I'm confident I can answer exercise 7.3 quite easily.

Ben
Logged
skochan
Administrator
Hero Member
*****
Posts: 3106







« Reply #1 on: September 15, 2009, 05:39:59 PM »

Ben,

I don't see any problems with your code. Can you post your Fraction.m file?

Cheers,

Steve Kochan

Logged
webwrx
Newbie
*
Posts: 41






« Reply #2 on: September 15, 2009, 05:53:32 PM »

Fraction.m

Code: (Objective-C)
//
//  Fraction.m
//  exer7-1
//
//  Created by Ben Johnson on 15/09/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
@synthesize numerator, denominator;

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

-(void) print
{
int sign;
int u;
int v;

// test if fraction is positive or negative
if ((numerator >= 0 && denominator >= 0) || (numerator < 0 && denominator < 0))
sign = 1;
else
sign = -1;

// get absolute value of numerator into temp variable u
if (numerator < 0)
u = -numerator;
else
u = numerator;

// get absolute value of denominator into temp variable v
if (denominator < 0)
v = -denominator;
else
v = denominator;

// perform printing operation
if (numerator == denominator)
NSLog(@"1");
else if (numerator == 0)
NSLog(@"0");
else if (denominator == 0)
NSLog(@"Divide by Zero!");
else if (u < v)
NSLog(@"%i/%i", sign * u, v);
else
NSLog(@"%i %i/%i", sign * u/v, u-v, v);
}

-(void) printReduced: (BOOL) r
{
if (r == YES) {
Fraction *tempResult = [[Fraction alloc] init];
[tempResult setNumerator: numerator];
[tempResult setDenominator: denominator];
[tempResult reduce];
[tempResult print];
[tempResult release];
}
else
[self print];
}

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

-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;

while (v != 0) {
temp = u % v;
u = v;
v = temp;
}

numerator /= u;
denominator /= u;
}

-(Fraction *) add: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator + denominator * f.numerator;
result.denominator = denominator * f.denominator;
return result;
}

-(Fraction *) subtract: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator - denominator * f.numerator;
result.denominator = denominator * f.denominator;
return result;
}

-(Fraction *) multiply: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.numerator;
result.denominator = denominator * f.denominator;
return result;
}

-(Fraction *) divide: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator;
result.denominator = denominator * f.numerator;
return result;
}

@end
Logged
webwrx
Newbie
*
Posts: 41






« Reply #3 on: September 15, 2009, 06:15:11 PM »

Unrelated - but in case anyone is trying to follow my logic with printing mixed fractions, I just noticed a couple of minor fixes. It now properly handles cases where the whole number is 2 or more. Also previously wasn't handling the possibility where the answer is -1. Have fixed below.

Code: (Objective-C)
	// perform printing operation
if (numerator == denominator)
NSLog(@"1");
else if (numerator == -denominator)
NSLog(@"-1");
else if (numerator == 0)
NSLog(@"0");
else if (denominator == 0)
NSLog(@"Divide by Zero!");
else if (u < v)
NSLog(@"%i/%i", sign * u, v);
else
NSLog(@"%i %i/%i", sign * u/v, u - (u/v) * v, v);
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.