# need help with fractions in C++

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 02-18-2012
Agnes Roguska
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 }```
• 02-18-2012
CornedBee
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.
• 02-18-2012
Agnes Roguska
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
• 02-18-2012
oogabooga
As CornedBee said:
Quote:

First, fix the layout of your code.
• 02-18-2012
Agnes Roguska
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"); }```
• 02-18-2012
oogabooga
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"); }```
• 02-18-2012
Agnes Roguska

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..
• 02-18-2012
rags_to_riches
Code:

`return a,b;`
You can't return two values from a function like that. Look into passing-by-reference.
• 02-18-2012
Agnes Roguska
im going crazy here
• 02-18-2012
Agnes Roguska
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; p=gcd(e3,f3);  here is telling me(1>fdgfd.cpp.cpp(24): error C3861: 'gcd': identifier not found 1>fdgfd.cpp.cpp(25): error C3861: 'simplify': identifier not found 1>fdgfd.cpp.cpp(48): error C3861: 'gcd': identifier not found 1>fdgfd.cpp.cpp(49): error C3861: 'simplify': identifier not found 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 ....
• 02-18-2012
stahta01
Lookup function prototypes
Function prototype - Wikipedia, the free encyclopedia

The above is a guess since your code is not readable.

Tim S.
• 02-19-2012
Elysia
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!
• 02-19-2012
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 ...
my gcd function works
simplify tooo
only thing im asking is for somebody to write me how to connect it together.
• 02-19-2012
Elysia
Quote:

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.
If you don't understand how to do something, google it, or if you can't find any good resources that help your understanding, ask for help about 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.

Quote:

only thing im asking is for somebody to write me how to connect it together.
First things first. Fix what you've been told.
If you don't understand something, ask about it.
How difficult is it to change the return type of main?
What is it that is so difficult about indenting?
At least you can become better than your class mates who all write unreadable code and who will not get a programming or will be a pain in the ass to work with.
• 02-19-2012
laserlight
Quote:

Originally Posted by Agnes Roguska
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 ...

oogabooga gave you an example in post #6. The basic idea is simple: when your code is in a block of scope (e.g., within the body of a function, an if statement, or a loop), you indent by one level. If within the block there is an inner block, you indent again by one level. After the block ends, you decrease the indentation by one level. An indentation can be either a few spaces (my editor is set to produce 4 spaces when I press the tab) or a tab character.

The other thing is blank lines: as a rule of thumb, you should not have more than two consecutive blank lines in your code. Usually, a single blank line to denote some logical separation of the lines of code will suffice.
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last