Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 16, 2017, 09:05:00 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: Chapter 7, Exercise 2. How to reduce without changing original value?  (Read 2028 times)
chrono1081
Newbie
*
Posts: 6


« on: October 30, 2011, 02:27:30 PM »

Hi guys,

I had a question about Chapter 7, exercise 2 (page 156 of the 2nd edition).

The problem says to modify the print method to accept a bool to verify whether the printed fraction should be reduced. Easy enough here is my method:

Code: (Objective-C)
-(void) print: (BOOL) reduce
{
    if(reduce == FALSE)
        NSLog(@"%i/%i", numerator, denominator);
    
    else
        [self reduce];
        NSLog(@"%i/%i", numerator, denominator);
}

The problem I have is making it so that the original fraction is not altered. I can think of ways to do this, but it requires doing things not yet taught in this book, and I want to get proficient with Objective-C so I do not want to do anything I haven't learned in the book yet.

I can also think of a messy way to do this where I'd either change reduce to a fraction returning function or create additional member variables to store a reduced numerator and denominator, but I'm looking for a more elegant solution.

Does anyone have any suggestions to how I may go about this? (Is there a more elegant solution that doesn't require adding member variables or altering the reduce function?)
Logged
chrono1081
Newbie
*
Posts: 6


« Reply #1 on: October 30, 2011, 03:00:52 PM »

I should add this is the way I am currently doing it, I was just looking for a more optimized way:

Here is the Fraction.m file:

Code: (Objective-C)
//
//  Fraction.m

#import "Fraction.h"

@implementation Fraction

@synthesize numerator, denominator, numeratorReduced, denominatorReduced;

-(void) print: (BOOL) reduce
{
    if(reduce == FALSE)
        NSLog(@"%i/%i", numerator, denominator);
  
    /*Return the reduced fraction if BOOL is true
    Also make sure to INCLUDE brackets aruond this else, if not you will have a phantom printing
    of 0/0 even though no print function is called!*/
    else{
        [self reduce];
        NSLog(@"%i/%i", numeratorReduced, denominatorReduced);
    }
}

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

-(void) reduce
{
    //Assign initial numerator and denominator values to the reduced members
    numeratorReduced = numerator;
    denominatorReduced = denominator;
    
    int u = numeratorReduced;
    int v = denominatorReduced;
    int temp;
    
    while(v != 0)
    {
        temp = u % v;
        u = v;
        v = temp;
    }

    numeratorReduced   /= u;
    denominatorReduced /= u;
}

-(double) convertToNum
{
    if(denominator != 0)
        return (double)numerator / denominator;
    
    else
        NSLog(@"Division by zero");
        return 0;
}

-(Fraction *) add: (Fraction *) f
{
    //To add two fractions
    //a/b + c/d = ( (a*d) + (b*c) ) / (b * d)
    
    Fraction *result = [[Fraction alloc] init];

    [result setTo: numerator * f.denominator + denominator * f.numerator
             over: denominator * f.denominator];
    
    return result;
}

-(Fraction *) subtract:(Fraction *)f
{
    //To subtract two fractions
    //a/b + c/d = ( (a*d) - (b*c) ) / (b * d)
    
    Fraction *result = [[Fraction alloc] init];
    
    [result setTo: numerator * f.denominator - denominator * f.numerator
             over: denominator * f.denominator];
    
    return result;
}

-(Fraction *) multiply:(Fraction *)f
{
    //To multiply two fractions
    //a/b + c/d = ( (a * c) / (b * d) )
    
    Fraction *result = [[Fraction alloc] init];
    
    [result setTo: numerator * f.numerator
             over: denominator * f.denominator];

    return result;
}

-(Fraction *) divide:(Fraction *)f
{
    //To divide two fractions
    //a/b + c/d = ( (a * c) / b * d) )
    
    Fraction *result = [[Fraction alloc] init];
    
    [result setTo: numerator * f.denominator
             over: denominator * f.numerator];

    return result;
}


@end

Here is the Fraction.h file

Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface Fraction : NSObject
{
    int numerator;
    int denominator;
    int numeratorReduced;   //Stores reduced numerator
    int denominatorReduced; //Stores reduced denominator
}

@property int numerator, denominator, numeratorReduced, denominatorReduced;

-(void)   print: (BOOL) reduce;
-(void)   setTo: (int) n over: (int) d;
-(void)   reduce;
-(double) convertToNum;
-(Fraction *) add:      (Fraction *) f;
-(Fraction *) subtract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide:   (Fraction *) f;

@end

And here is the main file:

Code: (Objective-C)

#import "Fraction.h"


int main (int argc, char*argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    Fraction *aFrac = [[Fraction alloc] init];
    Fraction *bFrac = [[Fraction alloc] init];
    
    [aFrac setTo:5 over:10];
    [aFrac print: FALSE];
    [aFrac print: TRUE];
                      
    [aFrac release];
    [bFrac release];
    [pool drain];
    return 0;
}
« Last Edit: October 30, 2011, 03:15:56 PM by chrono1081 » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #2 on: October 30, 2011, 07:07:55 PM »

You can look at other solutions here.  You need to allocate a new fraction object and reduce and print that one as appropriate.

Cheers,

Steve
Logged
chrono1081
Newbie
*
Posts: 6


« Reply #3 on: October 31, 2011, 12:49:26 PM »

Thanks but I couldn't find any other threads in this section about this particular problem : /
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #4 on: October 31, 2011, 12:51:12 PM »

Did you look at the Answers to Exercises category?

Cheers,

Steve
Logged
chrono1081
Newbie
*
Posts: 6


« Reply #5 on: November 01, 2011, 07:04:43 AM »

I am an idiot. I have NO clue how I missed that section before! Thanks so much I will look there Smiley
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.