Thread: Fraction Program...

  1. #1
    Registered User hottiefee's Avatar
    Join Date
    Oct 2010
    Location
    TN, USA
    Posts
    32

    Fraction Program...

    Hi all. I have been working on a program to add fractions. I want to eventually +, -, *, and / fractions. When I run it all I can do is enter two fractions and nothing else happens. If you have the time I would appreciate any and all help.
    Here is the problem:

    Write a function that reads a problem involving two common fractions (such as 2/4 + 5/6). After reading, call a function to perform the indicated operation (just adding right now). Pass the numerator and denominator of both fractions to the func that performs the operation (should return num and denom of result through its output parameters). Then display the result as a common fraction.

    I have these guidelines:

    --foundation: a = bq + r

    --1. find gcd of num & denom
    2. divid num & denom by gcd
    3. use the lcm

    --1. divide lcm by denom (multiplier)
    2. multiply num & denom by multiplier
    3. add num storing in num accumulator
    4. copy lcm into denom of accumulator

    Code:
    --In bold is what I have as guidelines and code
    --Also, there are specific questions included as comments
    Code:
    #include<iostream>
    #include<cmath>
    using namespace std;
    void getFraction(int&, int&);
    void readFracProblem(int&, int&, int&, int&, char&);	//For later use with +,-,*,/
    void add(int&, int&, int, int, int);	//I added the last "int" for my program
    int gcd(int, int);
    int lcm(int, int, int);		//I added the last "int" for program
    void normalizeFraction(int&, int&);
    void displayFraction(int, int);
    int main(){
    	int n1, d1, n2, d2, gcdNumber;
    
    	cout << "Enter a fraction (n / d): ";
    	getFraction(n1, d1);
    	cout << "Enter a fraction (n / d): ";
    	getFraction(n2, d2);
    
    	gcdNumber = gcd(d1, d2);
    
    	add(n1, d1, n2, d2, gcdNumber);
    	displayFraction(n1, d1);
    
    	return 0;
    }
    
    void getFraction(int& n, int& d){
    	char slash;			//Should it be "char = slash;" ?
    	cin >> n >> slash >> d;
    }
    
    int lcm(int accD, int d, int gcd){
    	int lcm;
    	lcm = (accD * d) / gcd;
    	return lcm;
    }
    
    void add(int& accN, int& accD, int n, int d, int gcd){
    	int lcmNumber = lcm(accD, d, gcd);	//*I'm not sure where these should go
    	int multiplier = lcmNumber / accD;	//*
    	accN = accN * multiplier;		//*
    	multiplier = lcmNumber / d;		//*
    	n = n * multiplier;			//*
    	accN = accN + n;			//*
    	accD = lcmNumber;			//*I'm not sure where these should go
    }
    
    int gcd(int a, int b){
    	int remainder, gcdNumber;
    	do{
    		remainder = a % b;
    		gcdNumber = a / b;
    	}while(remainder != 0);
    
    	return gcdNumber;
    }
    
    void normalizeFraction(int& n, int& d){
    	if(d < 0){
    		d = -d;
    		n = -n;
    	}
    	int absN = abs(n);
    	int gcdNumber = gcd(absN, d);
    	n = n / gcdNumber;
    	d = d / gcdNumber;
    }
    
    void displayFraction(int n, int d){
    	normalizeFraction(n, d);		//Does this belong here?
    	cout << n << " / " << d << endl;
    }
    I'm still a beginner so.... Thank you for any help

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Your initial problem is most likely to do with how you get the fractals. You are currently asking the user to input a number, a character and then a number again. However I am not sure this works as you think it does, for this to work the user must either press enter after each number/character or make a space. This because of how the input is handled. At least this is my early suspicioun after looking at your code.

    Edit after looking more closely your gcd function is off. if a % b != 0 on entry of the function you are stuck in an infinite loop. Check out wikipedia for an implementation of gcd (found a recursive one that shouldnt be too difficult).
    Last edited by Shakti; 01-16-2011 at 06:21 PM.

  3. #3
    Registered User hottiefee's Avatar
    Join Date
    Oct 2010
    Location
    TN, USA
    Posts
    32
    When they enter a fraction there is a function (getFraction) that reads in the character but only returns the numerator and denominator. I deleted a cout statement right after the user enters the fractions that I put in to display the two fractions to make sure that worked. They were both displayed right.
    Code:
    cout << "Enter a fraction (n / d): ";
    	getFraction(n2, d2);
                    
                    cout << n1 << " / " << d1 << endl;
                    cout << n2 << " / " << d2 << endl;

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Ah ok, then that wasnt a problem at least but check my previous post, edited in some more info.

  5. #5
    Registered User hottiefee's Avatar
    Join Date
    Oct 2010
    Location
    TN, USA
    Posts
    32
    Alright thanks, I'll check on that.

  6. #6
    Registered User hottiefee's Avatar
    Join Date
    Oct 2010
    Location
    TN, USA
    Posts
    32
    Even if I change that, am I calling too many functions inside of each other? It feels like I'm messing up with that because I end up with no final n1 and d1 for displayFraction(int n, int d). I have been working on this for about a week and think I've kinda made a mess editing. I just don't really know the order things should go I think. I'm in no rush but cannot wait to finish this. Thanks for the help so far.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your gcd function looks to be wrong. It should not contain a division, only a mod.
    Look up the algorithm on wikipedia.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User hottiefee's Avatar
    Join Date
    Oct 2010
    Location
    TN, USA
    Posts
    32
    Okay thanks. So would the gcd = a%b or gcd = a/b? I don't really understand the formula to find the gcd or lcm. I don't really understand the difference...
    Thanks so far though!

  9. #9
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    You can just write one function for gcd, then use the gcd to find the lcm. becuase
    lcm(a,b) = a*b/gcd(a,b); something like this would give you the lcm;
    Code:
    int lcm(int a, int b)
    {
      return a*b/gcd(a,b);
    }
    In your gcd, you should check if b==0. If it is, return a.
    Last edited by nimitzhunter; 01-17-2011 at 06:41 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  10. #10
    Registered User hottiefee's Avatar
    Join Date
    Oct 2010
    Location
    TN, USA
    Posts
    32
    oh okay. thanks. I get that

  11. #11
    Registered User hottiefee's Avatar
    Join Date
    Oct 2010
    Location
    TN, USA
    Posts
    32
    Does my add() function look right? Thats all im confused on now. I just dont see how I have no final answer for my displayFraction() function...

  12. #12
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    your add function and display function are ok. The only problem in that code is your gcd function.
    Last edited by nimitzhunter; 01-17-2011 at 07:06 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  13. #13
    Registered User hottiefee's Avatar
    Join Date
    Oct 2010
    Location
    TN, USA
    Posts
    32
    Okay I changed the gcd() function to:
    Code:
    int gcd(int a, int b){
    	if (b = 0)
    		return a;
    	else
    		return gcd(b, a%b);
    }
    but still nothing happens after the user enters the two fractions. Nothing else is displayed and I don't understand why...

  14. #14
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by hottiefee View Post
    Okay I changed the gcd() function to:
    Code:
    int gcd(int a, int b){
    	if (b == 0) // use "==" instead of "="
    		return a;
    	else
    		return gcd(b, a%b);
    }
    but still nothing happens after the user enters the two fractions. Nothing else is displayed and I don't understand why...
    Use logical "==" instead of assignment.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  15. #15
    Registered User hottiefee's Avatar
    Join Date
    Oct 2010
    Location
    TN, USA
    Posts
    32
    AWESOME! Thanks. I was working on this for a long time. It works completely now. Thank you so much!! Just little details had to be worked out.
    Have a great day!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:13 PM
  2. Homework Help, simple program, unsure newbie :(
    By Aslan14 in forum C Programming
    Replies: 13
    Last Post: 11-14-2010, 05:07 PM
  3. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  4. reducing a fraction
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-13-2002, 08:56 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread