1.11M Members

C++ Adding/Subtracting/Multipying/Dividing Two Fractions *HELP*

 
0
 

MY ASSIGNMENT:
Write a program that works with fractions. Your program should be able to add, subtract, multiply, and divide two fractions. Specifically, your program must request two fractions from the user, getting the numerator and denominator separately for each fraction, and the operation to perform (add, subtract, multiply, or divide). Your program will then compute the resulting fraction, keeping the numerator and denominator separate, and output the result. The resulting fraction must be simplified. For example, (4 / 6) should be displayed as (2 / 3), and (8 / 8) should be displayed as (1 / 1). You will need to use the GCD function from Problem 1 to do this simplification, so be sure to test it thoroughly before using it here.

You must compute the resulting fraction using fraction-based math (working with numerators and denominators). Do not simply convert the fractions to double values (like 1.5), do the math, and convert back to a fraction. There are numerous resources on the Internet if you need a refresher on how to add, subtract, multiply, and divide fractions.

_______________________________________________________________________________________

This is what I have so far. I kinda need some help on this. Thank You!!

#include <iostream>
using namespace std;

int fraction_subtract (int x1, int y1, int x2, int y2);
int fraction_add (int x1, int y1, int x2, int y2);
int fraction_multiply (int x1, int y1, int x2, int y2);
int fraction_divide (int x1, int y1, int x2, int y2);

int main()
{
	int x1, y1, x2, y2;

	cout << "Enter a number for x1 and y1: ";
	cin >> x1 >> y1;
	cout << "Enter a number for x2 and y2: ";
	cin >> x2 >> y2;
	
	return 0;
}

int fraction_subtract (int x1, int y1, int x2, int y2)
{
	int denom, num, frac;

	denom = y1 * y2;
	num = ((x2 * -1) * y1) - ((x1 * -1) * y2);
	frac = (num, denom);

	return frac;
}
 
0
 

What do you need help with?
Its best to write out your logic on paper.

For addition you simply multiply the numerators by the opposite fraction's denomiator and multiply the two denomiators together. Your sum is simply an unsimplified fraction with the numerator beginning obviously the sum of the two numerators over the product of the two denom.

Multiplying fractions is self-explaination I hope.
Division of fractions is simply multiply a fraction by the reprocial of the fraction your dividing by i.e (1/2)/(1/4) yields the same result as (1/2)* 4 = 2 in this case

 
0
 

The question to what I'm doing is above and I need help to finish the program with the code I have below.

This is the updated code I have:

#include <iostream>
using namespace std;

void add();
void subtract();
void multiply();
void divide();
void user_choice();

int choice;
int x1, y1, x2, y2;
int num, denom;

int main()
{
	cout << "Enter a number for x1 and y1: ";
	cin >> x1 >> y1;
	cout << "Enter a number for x2 and y2: ";
	cin >> x2 >> y2;
	cout << "Press 1 to add, Press 2 to subtract, Press 3 to multiply or Press 4 to divide: ";
	cin >> choice;

	return 0;
}

void user_choice()
{
	switch(choice)
	{
	case 1:
		add();
		break;
	case 2:
		subtract();
		break;
	case 3: 
		multiply();
		break;
	case 4:
		divide();
		break;
	}
}

void add()
{
	num = ((x1 * y2) + (y1 * x2));
	denom = (y1 * y2);
}

void subtract()
{
	num = ((x1 * y2) - (y1 * x2));
	denom = (y1 * y2);
}

void multiply()
{
	num = (x1 * x2);
	denom = (y1 * y2);
}

void divide()
{
	num = (x1 * y2);
	denom = (y1 * x2);
}
 
0
 

The question to what I'm doing is above and I need help to finish the program with the code I have below.

This is the updated code I have:

#include <iostream>
using namespace std;

void add();
void subtract();
void multiply();
void divide();
void user_choice();

int choice;
int x1, y1, x2, y2;
int num, denom;

int main()
{
	cout << "Enter a number for x1 and y1: ";
	cin >> x1 >> y1;
	cout << "Enter a number for x2 and y2: ";
	cin >> x2 >> y2;
	cout << "Press 1 to add, Press 2 to subtract, Press 3 to multiply or Press 4 to divide: ";
	cin >> choice;

	return 0;
}

void user_choice()
{
	switch(choice)
	{
	case 1:
		add();
		break;
	case 2:
		subtract();
		break;
	case 3: 
		multiply();
		break;
	case 4:
		divide();
		break;
	}
}

void add()
{
	num = ((x1 * y2) + (y1 * x2));
	denom = (y1 * y2);
}

void subtract()
{
	num = ((x1 * y2) - (y1 * x2));
	denom = (y1 * y2);
}

void multiply()
{
	num = (x1 * x2);
	denom = (y1 * y2);
}

void divide()
{
	num = (x1 * y2);
	denom = (y1 * x2);
}
 
0
 

Are you not familiar with arthmetic with fractions?
The greatest common denom. of any two fractions or any multiple of fractions is the product of the two denomiators. This shouldn't be to hard to see after doing a few problems on paper. However, I don't know how to implement the arthimetic these might help. Also, you did not post a problem, you posted your assignment/homework which no one here will do for you. But looking at it, it doesnt seem your too far off from completion so good job thus far.

These might help
http://patrickjmt.com/fractions-adding-and-subtracting/
http://patrickjmt.com/fractions-%e2%80%93-multiplying-and-dividing/

and also if you need a review on factoring
http://patrickjmt.com/factoring-a-number-2/

now if you were looking for the LCD this would be a different story.

Also to reduce the fraction both the denom. must divide evenly by a term that can be multipled to find the denom.
i.e 4/6, 3 * 2 = 6, now do num % any term that created 6 (3 or 2) and do the same from the denom
doing
num % 2 and denom % 2 , this will return the remainder if it not 0 if did not simplify.
Same with the other term, num % 3 and denom % 3 and same case as above.
Thats one way you can do it


which will return the value of the remainder in any of the test if the remainder is not

 
0
 

This is my updated code and when I run it I get a 0/0 for the answer.
Can anyone help me take a look at it and take me what's wrong so I can fix it.

Thank You!!

#include <iostream>
using namespace std;

void add(int& x, int& x1, int& y1, int& x2, int& y2, int& num, int& denom);
void subtract(int& x, int& x1, int& y1, int& x2, int& y2, int& num, int& denom);
void multiply(int& x, int& x1, int& y1, int& x2, int& y2, int& num, int& denom);
void divide(int& x, int& x1, int& y1, int& x2, int& y2, int& num, int& denom);
void user_choice(int& x1, int& y1, int& x2, int& y2);
void GCD(int& x, int& num, int& denom);
int simplify(int& x, int& num, int& denom);

int choice;
int num, denom;

int main()
{
	int x1, y1, x2, y2;

	cout << "Enter a number for x1: ";
	cin >> x1;
	cout << "Enter a number for y1: ";
	cin >> y1;
	cout << "Enter a number for x2: ";
	cin >> x2;
	cout << "Enter a number for y2: ";
	cin >> y2;
	cout << "Press 1 to add, Press 2 to subtract, Press 3 to multiply or Press 4 to divide: ";
	cin >> choice;

	cout << num << " / " << denom << endl;

	return 0;
}

void user_choice(int& x, int& x1, int& y1, int& x2, int& y2, int& num, int& denom)
{
	switch(choice)
	{
	case 1:
		add(x, x1, y1, x2, y2, num, denom);
		break;
	case 2:
		subtract(x, x1, y1, x2, y2, num, denom);
		break;
	case 3: 
		multiply(x, x1, y1, x2, y2, num, denom);
		break;
	case 4:
		divide(x, x1, y1, x2, y2, num, denom);
		break;
	}
}

void add(int& x, int& x1, int& y1, int& x2, int& y2, int& num, int& denom)
{
	num = ((x1 * y2) + (y1 * x2));
	denom = (y1 * y2);
	GCD(x, num, denom);
}

void subtract(int& x, int& x1, int& y1, int& x2, int& y2, int& num, int& denom)
{
	num = ((x1 * y2) - (y1 * x2));
	denom = (y1 * y2);
	GCD(x, num, denom);
}

void multiply(int& x, int& x1, int& y1, int& x2, int& y2, int& num, int& denom)
{
	num = (x1 * x2);
	denom = (y1 * y2);
	GCD(x, num, denom);
}

void divide(int& x, int& x1, int& y1, int& x2, int& y2, int& num, int& denom)
{
	num = (x1 * y2);
	denom = (y1 * x2);
	GCD(x, num, denom);
}

void GCD(int& x, int& num, int& denom)
{
	int y;

	while(denom != 0)
	{
		y = denom;
		denom = num % denom;
		x = y;
		simplify(x, num, denom);
	}
}

int simplify(int& x, int& num, int& denom)
{
	num = num / x;
	denom = denom / x;

	return (num, denom);
}
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article