# Thread: need help with fractions in C++

1. ## need help with fractions in C++

I need to make fractions to be in the simplest form. how to do it .i know i need to call and pass fraction to gcd and simplify but i dont now how to do it so it will work..

Code:
```int divide()

{

int one4,two4,three4,four4;
int e4,f4;

cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
cin>>one4;
cin>>two4;
cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
cin>>three4;
cin>>four4;

cout<<"Input instruction/_" << endl;

e4 = one4*four4;
f4 = two4*three4;

cout<<"("<< one4 << "/"<< two4 <<") / ("<< three4 <<"/"<< four4 <<") = " << e4 << "/" << f4;

cout << "\n";
return 0;
}

void simplify(int x2 , int y2 ,int gcd )

{

int a;
int b;

cout<< "Input the nominator and denomiantor of a fraction separated by spaces :" << x2 <<"  "<< y2 << endl;

a= x2/gcd;
b= y2/gcd;

cout<<"This fraction equals ("<< a <<"/"<< b << ")"<< endl;

}

int gcd(int x2, int y2)
{
int i;
int gcd2;

for (i=1; i<=x2; i++)
{

if((x2%i==0) && (y2%i==0))
gcd2 = i;
}

return gcd2;

}

void input()
{
int x,y,k;
cout << "Enter first integer: ";
cin >> x;
cout << "Enter second integer: ";
cin >> y;
k=gcd(x,y) ;

cout <<"GCD is "<< k << endl;

simplify( x , y , k); //calls

}```

2. First, fix the layout of your code. Far too many empty lines and no indentation; it's completely unreadable.

Second, state exactly what you want to happen and what actually happens with your code.

3. i want the fraction result in multiply to be simpler.thats why i was trying to send result from multiply to gcd and after to simplify but im doing something wrong

4. As CornedBee said:
First, fix the layout of your code.

5. Code:
```#include "stdafx.h"
#include <iostream>
using namespace std;

int multiply()

{
int one3,two3,three3,four3;
int e3,f3;

cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
cin>>one3;
cin>>two3;
cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
cin>>three3;
cin>>four3;
cout<<"Input instruction*_" << endl;
e3 = one3*three3;
f3 = two3*four3;

cout<<"("<< one3 << "/"<< two3 <<") * ("<< three3 <<"/"<< four3 <<") = " << e3 << "/" << f3;
cout << "\n";
return 0;
}
int gcd(int x2, int y2)
{
int i;
int gcd2;
for (i=1; i<=x2; i++)
{
if((x2%i==0) && (y2%i==0))
gcd2 = i;
}
return gcd2;

}

void simplify(int x2 , int y2 ,int gcd )
{
int a;
int b;
a= x2/gcd;
b= y2/gcd;
}

void main()

{
multiply();

system ("PAUSE");
}```

6. You seem to have some extra formatting (e.g., bolding) in the code you posted. And very little indentation. Properly formatted code looks more like this:
Code:
```#include "stdafx.h"
#include <iostream>
using namespace std;

int multiply()  /// Should be void multiply() if it only ever returns 0.
{
/// Why do your variables all end with the number 3?
int one3,two3,three3,four3;
int e3,f3;

/// It's "numerator" not "nominator".
cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
cin>>one3;
cin>>two3;

cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
cin>>three3;
cin>>four3;

cout<<"Input instruction*_" << endl;
e3 = one3*three3;
f3 = two3*four3;

cout<<"("<< one3 << "/"<< two3 <<") * ("<< three3 <<"/"<< four3 <<") = " << e3 << "/" << f3;
cout << "\n";
return 0;
}

/// This doesn't seem to be a proper gcd implementation.
/// Didn't you look at the pseudocode I gave in your original thread?
int gcd(int x2, int y2)
{
int i;
int gcd2;
for (i=1; i<=x2; i++)
{
if((x2%i==0) && (y2%i==0))
gcd2 = i;
}
return gcd2;
}

/// This function doesn't actually accomplish anything. (And is never called.)
void simplify(int x2 , int y2 ,int gcd )
{
int a;
int b;
a= x2/gcd;
b= y2/gcd;
}

void main()            /// should be int main() and return 0 at the end
{
multiply();
system ("PAUSE");
}```

7. Code:
```
int multiply()

{
int one3,two3,three3,four3;
int e3,f3;

cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
cin>>one3;
cin>>two3;
cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
cin>>three3;
cin>>four3;
cout<<"Input instruction*_" << endl;

e3 = one3*three3;
f3 = two3*four3;
gcd(e3,f3); //call
cout<<"("<< one3 << "/"<< two3 <<") * ("<< three3 <<"/"<< four3 <<") = " << e3 << "/" << f3<<this  e3 and f3 i want to be simpler
cout << "\n";
return 0;
}

void simplify(int x2 , int y2 ,int gcd )

{

int a;
int b;
a= x2/gcd;
b= y2/gcd;
return a,b;

}

int gcd(int x2, int y2)
{
int i;
int gcd2;
for (i=1; i<=x2; i++)
{
if((x2%i==0) && (y2%i==0))
gcd2 = i;
}
return gcd2;

}

void main()

{
multiply();

system ("PAUSE");
}```

its only part of my assigment ..thats why u can see 3 in the end of letters.. my assigment was to divide multiply add substract fractions and after make them simpler... so forexample 5/10 will be 1/2 .... thats why first i did multiply after i want the integers to move to gcd and simplify and come back simpler to multiply in cout...

in simplify i cant move a,b to my cout in multiply... please help me Im so confussed..

8. Code:
`return a,b;`
You can't return two values from a function like that. Look into passing-by-reference.

9. im going crazy here

10. Code:
```#include "stdafx.h"
#include <iostream>
using namespace std;

int divide()

{
int one4,two4,three4,four4;
int e4,f4;
int x;

cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
cin>>one4;
cin>>two4;
cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
cout<<"Input instruction/_" << endl;

e4 = one4*four4;
f4 = two4*three4;

x=gcd(e4,f4);
simplify(e4,f4,x);
cout<<x<<endl;
cout<<"("<< one4 << "/"<< two4 <<") / ("<< three4 <<"/"<< four4 <<") = " << e4 << "/" << f4;
cout << "\n";
return 0;
}

int multiply()

{

int one3,two3,three3,four3;
int e3,f3,p;

cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
cin>>one3;
cin>>two3;
cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
cin>>three3;
cin>>four3;
cout<<"Input instruction*_" << endl;
e3 = one3*three3;
f3 = two3*four3;
1>
simplify(e3,f3,p);

cout<<"("<< one3 << "/"<< two3 <<") * ("<< three3 <<"/"<< four3 <<") = " << e3 << "/" << f3;

cout << "\n";
return 0;
}
void simplify(int x2 , int y2 ,int gcd )
{

int a;
int b;
a= x2/gcd;
b= y2/gcd;
return (a,b);

}
int gcd(int x2, int y2)
{
int i;
int gcd2;

for (i=1; i<=x2; i++)
{

if((x2%i==0) && (y2%i==0))
gcd2 = i;
}
return gcd2;
}
void main()

{
multiply();
divide();

system ("PAUSE");
}```

MAybe now somebody can help me ....

11. Lookup function prototypes
Function prototype - Wikipedia, the free encyclopedia

Tim S.

12. You've been told to indent your code. You've even been shows how it should look like.
You've even been told main should return int.

Yet, I still do not see you have fixed any of this. Why?
If you expect any more help, then fix what you've already been told to fix!

13. if i will know how to do it I will be not on this forum.... Yes people told me that my code is unreadable but in my class everybody is writing like that.. so im not catching u guys.. im new to it .. all of u know it ...
my gcd function works
simplify tooo
only thing im asking is for somebody to write me how to connect it together.

14. Originally Posted by Agnes Roguska
if i will know how to do it I will be not on this forum.... Yes people told me that my code is unreadable but in my class everybody is writing like that.. so im not catching u guys.. im new to it .. all of u know it ...
Don't blame it on others. Because someone else is doing something is not an excuse for you to do it.
We do not expect you to know everything here. But we do expect you to put an effort into learning what you're told, not ignoring it.
We will help you achieve the understanding as long as you are willing. But do not ignore advice or comments if you don't understand them.

only thing im asking is for somebody to write me how to connect it together.
First things first. Fix what you've been told.