Fraction Program...too many functions?

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. I think I'm messing up with maybe calling too many functions inside 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). 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:
--There are specific questions included as comments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#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. :)
Last edited on
It looks to me like you have a pretty good handle on things.
The whole point of functions is to make them useful to do things you would otherwise do repeatedly throughout your code. You are making pretty good use of them.

Don't have external code do stuff your function could, though. For example, you have to calculate the GCD to use your add() function. Don't do that. The add() function should calculate the GCD itself -- it shouldn't need help from the caller to do its job. Likewise with lcm().

I would, however, use a struct to represent a fraction:

1
2
3
4
5
struct fraction
  {
  int n, d;
  fraction( int n = 0, int d = 1 ): n( n ), d( d ) { }
  };

Now all your functions should take and return a fraction.

1
2
3
4
5
6
7
int gcd( int a, int b );
int lcm( int a, int b );

fraction getFraction();
fraction addFraction( const fraction& a, const fraction& b );
fraction normalizeFraction( const fraction& a );
void displayFraction( const fraction& a );




Argh, I just reread your problem statement... Your professor requires you to use individual numerator and denominator arguments with your functions... Alas. Well, good job! Just fix the extra stuff....
Alright thanks. I'm still working on the gcd(), lcm(), and add() functions. I hope to figure this out soon! :) If you have any other ideas just let me know.
Argh, I just reread your problem statement... Your professor requires you to use individual numerator and denominator arguments with your functions... Alas


*continues to be amazed at what is taught in programming courses*
Its funny to me because my professor used fractions to teach structures then classes (with the methods being part of the classes). That was only the intro to classes. Makes me want to go program a class for fractions now.
Yeah, I'm still new at this. 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...
Okay I changed the gcd() function to:
1
2
3
4
5
6
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... :(
I figured it out.
1
2
3
4
5
6
7
8
Okay I changed the gcd() function to:
[code]
int gcd(int a, int b){
	if (b == 0)      //Had to use assignment char instead of =.
		return a;
	else
		return gcd(b, a%b);
}

Thanks all for the help :)
Have a great day!
How I handled add():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void simplify_fraction(int &num, int &denom)
{
  int i = 2; // Divisor
  int lowest;

  (num > denom) ? (lowest = denom) : (lowest = num);
  while ( i < (lowest / i) )
  {
    if (((num % i) == 0) && ((denom % i) == 0) ) {
      num     /= i;
      denom   /= i;
      lowest  /= i;
    } // Keeps doing it until its no longer divisible by the current i. So if its even it will strip out all the 2's.
    else
      (i < 3) ? (i += 1) : (i += 2); // Logic. Once no longer even, check all odd divsors.
  }

}

void add_fractions(int num1, int denom1, int num2, int denom2, int &resultnum, int &resultdenom)
{
  resultnum = (num1 * denom2) + (num2 * denom1);
  resultdenom = denom1 * denom2;
  simplify_fraction(resultnum, resultdenom);
}
Topic archived. No new replies allowed.