# Thread: need help with fractions in C++

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

int multiply()
{
int one3,two3,three3,four3;
int e3,f3, x;

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;
x=gcd(e3,f3);
simplify(e3,f3,x);

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

}
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;
}

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

int main()
{
multiply();
system ("PAUSE");
}```

2. gcd is working

3. Very good. Now you are light years ahead of your class mates.
Now you need to learn how to use references in order to pass back two values:

Code:
```int simplify(int x2 , int y2 ,int gcd, int& a, int& b)
{
a= x2/gcd;
b= y2/gcd;
return (a,b);
}

Call as
int a, b;
simplify(x2, t2, gcd, a, b);
// a and b will have the results```

4. Originally Posted by Agnes Roguska
gcd is working
That's good, though I note that there's a better algorithm (search the Web for Euclid's algorithm).

So, what else do you need help with?

Here's a tip: use descriptive variable names. Instead of one3, two3, three3, and four3, it would be better to name them numerator1, denominator1, numerator2, and denominator2.

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

int multiply()
{
int numerator,denominator,numerator2,denominator2;
int x2,t2, x;

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

cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
cin>>numerator2;
cin>>denominator2;

cout<<"Input instruction*_" << endl;
x2 = numerator*numerator2;
t2 =denominator*denominator2;
int a, b;
x=gcd(x2,t2);
simplify(x2, t2,x, a, b);

cout<<"("<< numerator << "/"<<denominator <<") * ("<< numerator2 <<"/"<< denominator2 <<") = " <<a << "/" << b;
cout << "\n";

}
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;
}

int simplify(int x2 , int y2 ,int gcd , int a , int b )
{

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

}

int main()
{
multiply();
system ("PAUSE");
}```

why is showing always that 1>fdgfd.cpp.cpp(22): error C3861: 'gcd': identifier not found

6. Learn function prototypes.

7. And the pass by reference that's been repeatedly pointed out to you. You seem to struggle to heed the advice given to you.

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

int 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;
}
int multiply()
{
int numerator,denominator,numerator2,denominator2;
int x2,t2, x;

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

cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
cin>>numerator2;
cin>>denominator2;

cout<<"Input instruction*_" << endl;
x2 = numerator*numerator2;
t2 =denominator*denominator2;
int a, b;
x=gcd( x2, t2);
simplify( x2, t2, x, a, b);

cout<<"("<< numerator << "/"<<denominator <<") * ("<< numerator2 <<"/"<< denominator2 <<") = " <<a << "/" << b;
cout << "\n";
return 0;
}

int main()
{
multiply();
system ("PAUSE");
}```

hmm the result is 0 hahah i know u are giving me tips but im so useless

9. Read up on references again.

10. pass by the references - can u give me an easy example .. cos im searching on google and its showing me hard examples.
sorry guys but really programming is too hard for me ..cos im mixing everything

11. I already did a while ago.

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

int 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;
}
int multiply()
{
int numerator,denominator,numerator2,denominator2;
int x2,t2, x;

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

cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
cin>>numerator2;
cin>>denominator2;

cout<<"Input instruction*_" << endl;
x2 = numerator*numerator2;
t2 =denominator*denominator2;
int a, b;
x=gcd( x2, t2);
simplify( x2, t2, x, a, b);

cout<<"("<< numerator << "/"<<denominator <<") * ("<< numerator2 <<"/"<< denominator2 <<") = " <<a << "/" << b;
cout << "\n";
return 0;
}

int main()
{
multiply();
system ("PAUSE");
}```

13. can somebody do it for me !!!so i will not disturb u anymore .

14. There is absolutely no need for pointers. You are only complicating this more than it needs to be.
Go back to your previous code, then go back look at my example on how to use references. If needs be, start a new project and try to get it working, then return to your current project once you got it working.
And no - no one will do it for you! You are the one who needs to learn, not us!

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

int simplify(int x2 ,int y2 ,int gcd ,int a ,int b )
{

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

return simplify;
}

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;
}
int multiply()
{
int numerator,denominator,numerator2,denominator2 ;
int x;
int x2, t2;
int c;
int d;

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

cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
cin>>numerator2;
cin>>denominator2;

cout<<"Input instruction*_" << endl;
x2 = numerator*numerator2;
t2 =denominator*denominator2;

x=gcd( x2, t2);
simplify(  x2 ,  t2 ,x , c ,  d);

cout<<"("<< numerator << "/"<<denominator <<") * ("<< numerator2 <<"/"<< denominator2 <<") = " << c << "/" << d;
cout << "\n";
return 0;
}

int main()
{
multiply();
system ("PAUSE");
}```