Page 1 of 7 123 ... LastLast
Results 1 to 10 of 61

Thread: Programming Challenge 2: Fractions

  1. #1
    Join Date
    Jun 2006
    Beans
    943

    Programming Challenge 2: Fractions

    So it seems I must pick the next challenge.

    *Drum roll* This weeks challenge is... to implement a simple fractions library that can manipulate fractions in various ways. Probably, you will want to implement a class to store the fraction with a numerator and denominator. As usual, creativity, efficiency and elegance are weighed to decide the winner. Any language is welcome - and the elegance is subjective to that language.

    The implementation must provide the following functions:

    • It must have a function for making a new fraction, setting the numerator and denominator. For example, it could be called Fraction_Create.
    • A function for multiplying two fractions, returning a new fraction as output. It could be called Fraction_Multiply.
    • A function for printing the fraction, such as Fraction_Print. It can print the fraction as either a top heavy fraction or a mixed fraction.


    Bonus features (for more points):

    • A function for returning the reciprocal of a fraction.
    • Functions for addition, subtraction and division (of fractions).
    • The ability to simplify fractions and only ever returning fully simplified fractions from any of the functions listed will get you bonus points.
    • A function to convert a fraction to an approximate decimal.
    • A function to convert a decimal to an approximate fraction. Clever algorithms here will get you bonus points!


    You can't use any fraction libraries already created - you must code them from scratch! A winner will be picked on Wednesday the fifth of March based on the above.

    Good luck and if you have any question, post them here!
    Lster
    Last edited by Lster; February 27th, 2008 at 03:16 PM.

  2. #2
    Join Date
    Oct 2006
    Location
    Myra, WV
    Beans
    209
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Programming Challenge 2: Fractions

    Haha, I just had to write this program for my intro to C++ class...

    Where do I submit it to?
    bigubuntu: amd 900 256mb ram 540gb hd ubuntu 7.10
    bigdesktop: intel 2.2 512md ram 80gb hd ubuntu 8.04
    bigmapper: intel 2.4 1gb ram 60gb hd ubuntu 8.10
    bigtoughbook: intel 3.12 core duo 1.5gb ram 80gb hd xp

  3. #3
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Programming Challenge 2: Fractions

    Note, this was written as soon as I got up, and saw this thread. So it isn't the best, obviously, that I could do.

    Updating
    main.c:
    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "fraction.h"

    int main(int args,char*argv)
    {
        
    Fraction x newFraction(3,2);
        
    Fraction y newFraction(5,7);
        
    Fraction z getReciprocal(y);

        
    Fraction added addFraction(x,y);
        
    Fraction subbed subFraction(x,y);
        
    Fraction mult multiplyFraction(x,y);
        
    Fraction div divideFraction(x,y);


        
    printFraction(x,0);
        
    printFraction(y,0);
        
    printFraction(z,0);

        
    printFraction(x,1);
        
    printFraction(y,1);
        
    printFraction(z,1);

        
    printFraction(added,0);
        
    printFraction(subbed,0);
        
    printFraction(mult,0);
        
    printFraction(div,0);

        
    deleteFraction(x);
        
    deleteFraction(y);
        
    deleteFraction(z);
        
    deleteFraction(added);
        
    deleteFraction(subbed);
        
    deleteFraction(mult);
        
    deleteFraction(div);

        return 
    0;

    Output:
    Code:
    3/2
    5/7
    7/5
    1 1/2
    5/7
    2 2/5
    31/14
    11/14
    15/14
    21/10
    fraction.h
    PHP Code:
    typedef struct
    {
        
    int num;
        
    int denom;
    } * 
    Fraction;

    Fraction addFraction(Fraction,Fraction);
    Fraction divideFraction(Fraction,Fraction);
    Fraction getReciprocal(Fraction);
    Fraction multiplyFraction(Fraction,Fraction);
    Fraction newFraction(int,int);
    Fraction subFraction(Fraction,Fraction);

    void deleteFraction(Fraction);
    int getCommonDenom(Fraction,Fraction);
    void printFraction(Fraction,int); 
    fraction.c
    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "fraction.h"
    Fraction addFraction(Fraction fFraction f2)
    {
        
    int commonDenom getCommonDenom(f,f2);
        return 
    newFraction(((commonDenom/f->denom) * f->num)+((commonDenom/f2->denom) * f2->num),commonDenom);
    }

    Fraction divideFraction(Fraction fFraction f2)
    {
        
    Fraction temp getReciprocal(f2);
        
    Fraction divideFraction multiplyFraction(f,temp);
        
    deleteFraction(temp);
        return 
    divideFraction;
    }

    Fraction subFraction(Fraction fFraction f2)
    {
        
    int commonDenom getCommonDenom(f,f2);
        return 
    newFraction(((commonDenom/f->denom) * f->num)-((commonDenom/f2->denom) * f2->num),commonDenom);
    }

    Fraction getReciprocal(Fraction f)
    {
        return 
    newFraction(f->denom,f->num);
    }

    Fraction multiplyFraction(Fraction f,Fraction f2)
    {
        return 
    newFraction(f->num f2->num,f->denom f2->denom);
    }

    Fraction newFraction(int x,int y)
    {
        
    Fraction f malloc(sizeof(Fraction));
        
    && != f->num xf->denom printf("Error in init, denominator = %d numerator = %d",x,y);
        return 
    f;
    }


    void deleteFraction(Fraction f)
    {
        
    free(f);
    }
    int getCommonDenom(Fraction fFraction f2)
    {
        return 
    f->denom f2->denom;
    }
    void printFraction(Fraction f,int mode)
    {
        
    mode == printf("%d/%d\n",f->num,f->denom) : f->denom == printf("%d\n",f->num) : f->num f->denom printf("%d %d/%d\n",f->num f->denom,f->num - ((f->num f->denom) * f->denom),f->denom) : printFraction(f,0);

    It is a mix of fulflilling the specs in the OP, rapid coding, and trying to confuse new C coders.
    Last edited by LaRoza; February 28th, 2008 at 02:48 AM.

  4. #4
    Join Date
    Jun 2006
    Beans
    943

    Re: Programming Challenge 2: Fractions

    Very nice. How about add, subtract and canceling?

  5. #5
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Programming Challenge 2: Fractions

    Quote Originally Posted by Lster View Post
    Very nice. How about add, subtract and canceling?
    I haven't:

    * eaten
    * drank
    * gotten fully dress
    * read the paper
    * looked for job (do it every day)

    Finishing the library will have to wait. I have priorities (and coding this showed where some of them are)

    (Note, anyone who doesn't see the point of pointers, read the code, and try to do it without pointers)

    Updated to include other operations, sorry, nothing fancy or clever going on
    Last edited by LaRoza; February 27th, 2008 at 08:31 PM.

  6. #6
    Join Date
    Jun 2006
    Beans
    943

    Re: Programming Challenge 2: Fractions

    Quote Originally Posted by LaRoza
    I haven't:

    * eaten
    * drank
    * gotten fully dress
    * read the paper
    * looked for job (do it every day)

    Finishing the library will have to wait. I have priorities (and coding this showed where some of them are)
    I guess you're in a different time zone to me.

  7. #7
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Programming Challenge 2: Fractions

    Quote Originally Posted by Lster View Post
    I guess you're in a different time zone to me.
    I got up later than normal, it is midday now.

  8. #8
    Join Date
    Dec 2006
    Location
    Glasgow, Scotland
    Beans
    470
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Programming Challenge 2: Fractions

    Another objc implementation (I'm learning objc, if you hadn't guessed).

    Code:
    #import <objc/Object.h>
    
    // Define the Fraction class
    @interface Fraction : Object
    {
    	int numerator;
    	int denominator;
    }
    - (Fraction *) initWith: (int) n: (int) d;
    - (void) setNumerator: (int) n;
    - (void) setDenominator: (int) d;
    - (void) setTo: (int) n over: (int) d;
    - (int)	numerator;
    - (int)	denominator;
    - (void) reduce;
    - (double) convertToNum;
    - (void) print;
    - (void) printMixed;
    @end
    
    // Define the MathOps category
    @interface Fraction (MathOps)
    - (Fraction *) add: (Fraction *) f;
    - (Fraction *) mul: (Fraction *) f;
    - (Fraction *) sub: (Fraction *) f;
    - (Fraction *) div: (Fraction *) f;
    - (Fraction *) reciprocal: (Fraction *) f;
    @end
    Code:
    #import "Fraction.h"
    #import <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    @implementation Fraction;
    -(Fraction *) initWith: (int) n: (int) d;
    {
    	self = [ super init ];
    
    	if (self)
    		[ self setTo: n over: d ];
    
    	return self;
    }
    
    -(void) setNumerator: (int) n
    {
    	numerator = n;
    }
    
    -(void) setDenominator: (int) d
    {
    	denominator = d;
    }
    
    -(int) numerator 
    {
    	return numerator;
    }
    
    -(int) denominator
    {
    	return denominator;
    }
    
    -(double) convertToNum
    {
    	if (denominator != 0)
    		return (double) numerator / denominator;
    	else
    		return 0.0;
    }
    
    -(void) setTo: (int) n over: (int) d
    {
    	numerator = n;
    	denominator = d;
    }
    
    - (void) reduce
    {
    	int	u = numerator;
    	int	v = denominator;
    	int	temp;
    
    	if (u < 0)
    		u = -u;
    
    	while (v != 0) {
    		temp = u % v;
    		u = v;
    		v = temp;
    	}
    
    	numerator /= u;
    	denominator /= u;
    }	
    
    -(void) print 
    {
    	printf (" %i/%i ", numerator, denominator);
    }
    
    - (void) printMixed
    {
    	Fraction *result = [[Fraction alloc] init];
    	int whole_part;
    	div_t tdiv;
    
     	if (numerator >= denominator) {
    		if (numerator == denominator) {
    			printf(" 1 ");
    			return;
    		}
    		tdiv = div(numerator, denominator);
    		whole_part = tdiv.quot;
    		if (tdiv.rem != 0) {
    			[result setTo: tdiv.rem over: denominator];
    			[result reduce];
    			printf(" %i+%i/%i ", whole_part, [result numerator], 
                                                                      [result denominator]);
    			return;
    		}
    		else {
    			printf(" %i ", whole_part);
    			return;
    		}
    	}
    	else
    		[self print];
    }
    @end
    
    /* MathOps category implementation */
    @implementation Fraction (MathOps);
    -(Fraction *) add: (Fraction *) f
    {
    	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 *) sub: (Fraction *) f
    {
    	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 *) mul: (Fraction *) f
    {
    	Fraction *result = [[Fraction alloc] init];
    
    	[result setTo: numerator * [f numerator]
    		 over: denominator * [f denominator]];
    	[result reduce];
    
    	return result;
    }
    
    - (Fraction *) div: (Fraction *) f
    {
    	Fraction *result = [[Fraction alloc] init];
    
    	[result setTo: numerator * [f denominator]
    		 over: denominator * [f numerator]];
    	[result reduce];
    
    	return result;
    }
    
    - (Fraction *) reciprocal: (Fraction *) f
    {
    	Fraction *result = [[Fraction alloc] init];
    
    	[result setTo: denominator over: numerator];
    	[result reduce];
    
    	return result;
    }
    @end
    Code:
    #import "Fraction.h"
    
    int main (int argc, char *argv[])
    {
    	Fraction  *a;
    	Fraction  *b;
    	Fraction  *result;
    	
    	a = [[Fraction alloc] init];
    	b = [[Fraction alloc] init];
    
    	[a setTo: 4 over: 3];
    	[b setTo: 2 over: 8];
    
    	[a print]; 
    	printf (" + "); 
    	[b print]; 
    	printf (" = ");
    	result = [a add: b];
    	[result print];
    	printf("\t = ");
    	[result printMixed];
    	printf("\t = %e\n", [result convertToNum]);
    
    	[a print];
    	printf(" - ");
    	[b print];
    	printf(" = ");
    	result = [a sub: b];
    	[result print];
    	printf("\t = ");
    	[result printMixed];
    	printf("\t = %e\n", [result convertToNum]);
    
    	[a print]; 
    	printf (" * "); 
    	[b print]; 
    	printf (" = ");
    	result = [a mul: b];
    	[result print];
    	printf("\t\t\t = %e\n", [result convertToNum]);
    
    	[a print]; 
    	printf(" / "); 
    	[b print]; 
    	printf(" = ");
    	result = [a div: b];
    	[result print];
    	printf("\t = ");
    	[result printMixed];
    	printf("\t = %e\n", [result convertToNum]);
    
    	[a free];
    	[b free];
    	[result free];
    
    	return 0;
    }
    compile with -lobjc and -lm

    EDIT: added printMixed to handle mixed fractions. I might add convert decimal to fraction later. (I'm getting whitespace inserted into my code blocks when I post)
    Last edited by ruy_lopez; February 28th, 2008 at 12:10 AM.

  9. #9
    Join Date
    Oct 2006
    Location
    Myra, WV
    Beans
    209
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Programming Challenge 2: Fractions

    Like I said above, I had to write this program for a class anyway...
    The teacher is kind of weird, he won't let us use anything that he hasn't taught us yet... so no classes or anything.


    Das Code:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    // End pre-processor directives...
    
    /*
      Program: Team Project
      Author: CHRIS TERRY
      Date written: 2/20/2008
      
      This of each fractal equation like this:
      (numeratorOne / denominatorOne) + (numeratorTwo / denominatorTwo)
    
      This is of course assuming that you want to add them.
      Assumptions/Checks: 
        Denominators are NOT 0
        Menu choices can only be 0 - 5
        The user only wants to do an equation regarding two(2) fractions
        There are NO command args
        Debug mode is choice 5 on the menu, it will restart the menu
    
      If you got this, please use it appropriately.
    */
        
    
    bool DEBUG = false;
    
    // Global debug variable...
    
    void menu(int&);
    void addFractions(int, int, int, int, int&, int&);
    void subtractFractions(int, int, int, int, int&, int&);
    void multiplyFractions(int, int, int, int, int&, int&);
    void divideFractions(int, int, int, int, int&, int&);
    void getNumbers(int&, int&, int&, int&, char);
    int returnProduct(int, int);
    
    // End method prototyping...
    
    int main() {
      
      int finalNumerator, finalDenominator;
      // The finals...
      
      int numeratorOne, numeratorTwo, denominatorOne, denominatorTwo;
      // The numbers the will enter...
    
      int menuChoice = 0;
      // The menu item they enter...
    
      char symbol;
      // Which symbol for the equation...
    
      menu(menuChoice);
      // Get which method they want to do...
    
      switch(menuChoice) {
        // Determines which symbol based on which operation they want to perform...
    
      case 1: symbol = '+'; break;
      case 2: symbol = '-'; break;
      case 3: symbol = 'x'; break;
      case 4: symbol = '/'; break;
      }
      // End switch-case...
    
      getNumbers(numeratorOne, numeratorTwo, denominatorOne, denominatorTwo, symbol);
      // Get their numbers...
    
      if(DEBUG) cout << endl <<  numeratorOne << denominatorOne << numeratorTwo << denominatorTwo << endl;
      // Debug code...
    
      switch(menuChoice) {
        // Switch to determine witch method to call...
    
      case 1: {
        // The addition case...
    
        addFractions( numeratorOne, numeratorTwo, denominatorOne, denominatorTwo, finalNumerator, finalDenominator);
        // Call the method...
    
         break;
      } 
      case 2: {
        // The subtraction case...
        
        subtractFractions ( numeratorOne, numeratorTwo, denominatorOne, denominatorTwo, finalNumerator, finalDenominator );
        // Call the method...
    
        break;
      }
      case 3: {
        // The multiplication case...
        
        multiplyFractions ( numeratorOne, numeratorTwo, denominatorOne, denominatorTwo, finalNumerator, finalDenominator );
        // Call the method...
    
        break;
      }
      case 4: {
        // The division case...
    
        divideFractions ( numeratorOne, numeratorTwo, denominatorOne, denominatorTwo, finalNumerator, finalDenominator );
        //Call the method...
    
        break;
      }
       
      }
      // End switch-case...
    
      cout << endl << "Your problem was: ( " << numeratorOne << " / " << denominatorOne
           << " ) " << symbol << " ( " << numeratorTwo << " / " << denominatorTwo << " )" << endl;
      // Output their original problem...
      
      cout << "Your answer is: " << finalNumerator << " / " << finalDenominator << endl;
      // Output the answer...
    
      cout << "Just for fun, the decimal answer is: " << (finalNumerator + 0.0) / (finalDenominator + 0.0) << endl;
      // I like to make sure the numbers are real, so output the decimal value...
    
      cout << "Remember, this program does NOT calculate the lowest common denominator." << endl;
      // Disclaimer...
    
      return 0;
      // Terminate the program...
    }
    
    void divideFractions( int numeratorOne, int numeratorTwo, int denominatorOne,
    		      int denominatorTwo, int& finalNumerator, int& finalDenominator ) {
      // The division method...
    
      if(DEBUG) cout << endl << "divideFractions: Entering method..." << endl;
      // Debug code...
    
      int newNumerator = returnProduct(numeratorOne, denominatorTwo);
      // Get the numerator: numeratorOne * denominatorTwo
    
      int newDenominator = returnProduct(denominatorOne, numeratorTwo);
      // Get the denominator: denominatorOne * numeratorTwo
    
      finalNumerator = newNumerator;
      finalDenominator = newDenominator;
      // Set the references one we have both the values...
    
      if(DEBUG) cout << endl << finalNumerator << finalDenominator << endl;
      //Debug code...
    }
    
    void multiplyFractions( int numeratorOne, int numeratorTwo, int denominatorOne,
    			int denominatorTwo, int& finalNumerator, int& finalDenominator ) {
      // The multiplication method...
    
      if(DEBUG) cout << endl << "multiplyFractions: Entering method..." << endl;
      // Debug code...
    
      int newNumerator = returnProduct(numeratorOne, numeratorTwo);
      // Get the numerator: numeratorOne * numeratorTwo
    
      int newDenominator = returnProduct(denominatorOne, denominatorTwo);
      // Get the denominator: denominatorOne * denominatorTwo
      
      finalNumerator = newNumerator;
      finalDenominator = newDenominator;
      // Apply the references once we have the values...
    
      if(DEBUG) cout << endl << finalNumerator << finalDenominator << endl;
      // Debug code...
    }
    
    int returnProduct( int numberOne, int numberTwo ) {
      return numberOne * numberTwo;
      // All this method does is return the product of variable 1 by variable 2...
    }
    
    void subtractFractions ( int numeratorOne, int numeratorTwo, int denominatorOne, 
    			 int denominatorTwo, int& finalNumerator, int& finalDenominator ) {
      // The subtraction method...
    
      if(DEBUG) cout << endl << "subtractFractions: Entering method... " << endl;
      // Debug code...
    
      int commonDenominator = returnProduct( denominatorOne, denominatorTwo );
      // Get the common denominator: denominatorOne * denominatorTwo
    
      int newNumeratorOne = returnProduct(numeratorOne, denominatorTwo);
      // Get the first numerator: numeratorOne * denominatorTwo
    
      int newNumeratorTwo = returnProduct(numeratorTwo, denominatorOne);
      // Get the second numerator: numeratorTwo * denominatorOne
    
      finalNumerator = newNumeratorOne - newNumeratorTwo;
      // The final numerator is the first minus the second...
    
      finalDenominator = commonDenominator;
      // Apply the references one we compute the values...
    
      if(DEBUG) cout << endl << commonDenominator << finalNumerator << finalDenominator << endl;
      // Debug code...
    }
    
    void addFractions ( int numeratorOne, int numeratorTwo, int denominatorOne, 
    		    int denominatorTwo, int& finalNumerator, int& finalDenominator) {
      if(DEBUG) cout << endl << "addFractions: Entering method..." << endl;
      // Debug code...
      
      int commonDenominator = returnProduct(denominatorOne , denominatorTwo);
      // Calculate the common denominator...
    
      int newNumeratorOne = returnProduct(numeratorOne , denominatorTwo);
      // Calculate the first new numerator...
    
      int newNumeratorTwo = returnProduct(numeratorTwo , denominatorOne);
      // Calculate the second new numerator...
    
      finalNumerator = newNumeratorOne + newNumeratorTwo;
      // Calculate the final numerator...
    
      finalDenominator = commonDenominator;
      // Calculate the final denominator...
    
      if(DEBUG) cout << endl << commonDenominator << finalNumerator << finalDenominator << endl;
      // Debug code...
    }
    
    void getNumbers( int& numeratorOne, int& numeratorTwo, int& denominatorOne, int& denominatorTwo, char symbol) {
      // This method just gets the numbers from the user with a pretty menu...
    
      if(DEBUG) cout << endl << "getNumbers: Entering method..." << endl;
      // Debug code...
    
      do {
        cout << endl << "I am here to get your desired numerators and denominators" << endl;
        cout << "Please enter them in this fashion: 1 2 3 4" << endl;
        cout << "Please note the SPACES between the numbers." << endl;
        cout << "Thus, 1 2 3 4 would mean: ( 1 / 2 ) " << symbol << " ( 3 / 4 ) " << endl;
        cout << "I'll take those numbers now: ";
        // Output a nice little input prompt...
    
        cin >> numeratorOne >> denominatorOne >> numeratorTwo >> denominatorTwo;
        // Get the numbers...
    
        if((denominatorOne == 0) || (denominatorTwo == 0)) {
          cout << endl << "You cannot have a denominator equal to 0. Please try again." << endl;
        }
        // Check for division by 0...
    
      } while ( (denominatorOne == 0) || (denominatorTwo == 0));
        // Run while the denominators are equal to 0...
       
    }
    
    void menu(int& menuChoice) {
      // This is the menu method, it passes the choice back up to main...
      
      do {
        cout << "Welcome to the fraction calculator" << endl;
        cout << "Your choices are:" << endl;
        cout << "\t0) Replay Menu" << endl;
        cout << "\t1) Add Fractions" << endl;
        cout << "\t2) Subtract Fractions" << endl;
        cout << "\t3) Multiply Fractions" << endl;
        cout << "\t4) Divide Fractions" << endl;
        ( DEBUG ) ? cout << "\t5) DEBUG MODE OFF" << endl : cout << "\t5) DEBUG MODE ON" << endl;
        cout << "Please enter your choice( ex. 1 ): ";
        cin >> menuChoice;
        // Get the actual choice after advisal...
        if ( menuChoice == 5 ) {
          DEBUG = !DEBUG;
          menuChoice = 0;
        }
        if ( ( menuChoice < 0 ) || ( menuChoice > 5 ) ) {
          cout << "Please enter a number 0 - 4." << endl;
          menuChoice = 0;
        }
        // If it is 0 (or anything else), we will reprint the menu...
    
      } while( menuChoice == 0 );
      // End menu...
    
      if(DEBUG) cout << endl << "Menu: Exit do-while... " << menuChoice << endl;
      // Debug code...
    }
    Output:
    Code:
    Welcome to the fraction calculator
    Your choices are:
            0) Replay Menu
            1) Add Fractions
            2) Subtract Fractions
            3) Multiply Fractions
            4) Divide Fractions
            5) DEBUG MODE ON
    Please enter your choice( ex. 1 ): 5
    Welcome to the fraction calculator
    Your choices are:
            0) Replay Menu
            1) Add Fractions
            2) Subtract Fractions
            3) Multiply Fractions
            4) Divide Fractions
            5) DEBUG MODE OFF
    Please enter your choice( ex. 1 ): 1
    
    Menu: Exit do-while... 1
    
    getNumbers: Entering method...
    
    I am here to get your desired numerators and denominators
    Please enter them in this fashion: 1 2 3 4
    Please note the SPACES between the numbers.
    Thus, 1 2 3 4 would mean: ( 1 / 2 ) + ( 3 / 4 ) 
    I'll take those numbers now: 1 2 3 4
    
    1234
    
    addFractions: Entering method...
    
    8108
    
    Your problem was: ( 1 / 2 ) + ( 3 / 4 )
    Your answer is: 10 / 8
    Just for fun, the decimal answer is: 1.25
    Remember, this program does NOT calculate the lowest common denominator.
    bigubuntu: amd 900 256mb ram 540gb hd ubuntu 7.10
    bigdesktop: intel 2.2 512md ram 80gb hd ubuntu 8.04
    bigmapper: intel 2.4 1gb ram 60gb hd ubuntu 8.10
    bigtoughbook: intel 3.12 core duo 1.5gb ram 80gb hd xp

  10. #10
    Join Date
    Nov 2007
    Location
    UK
    Beans
    772
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Programming Challenge 2: Fractions

    Quote Originally Posted by LaRoza View Post
    fraction.h
    PHP Code:
    typedef struct
    {
        
    int num;
        
    int denom;
    } * 
    Fraction;

    Fraction addFraction(Fraction,Fraction);
    Fraction getReciprocal(Fraction);
    Fraction multiplyFraction(Fraction,Fraction);
    Fraction newFraction(int,int);
    Fraction subFraction(Fraction,Fraction);

    void deleteFraction(Fraction);
    int getCommonDenom(Fraction,Fraction);
    void printFraction(Fraction,int); 
    Very nice.
    Forgot to declare:

    Fraction divideFraction(Fraction,Fraction);

    In the header. But otherwise, smashing stuff.
    Disclaimer: Yes I usually talk crap

Page 1 of 7 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •