1. ## simplfying fractions

Hi guys,

Ok this is more to do with laziness than anything else but I was wondering how would you create a program that cancels down fractions into its simplest form. I've managed to do it for positive integers but I have a problem when dealing with negative integers.

Remember these rules:

Code:
``` + / + = +
+ / -  =  -
- / + =  -
-  /  - =  +

so:

=  -6
---
-10

=   3
---
5```
Possible solutions include using the absolute function, or perhaps converting the integer to a string and then checking to see if the first element in that array is equal to '-' or '+' and then doing the necessary arithmetic manipulations.

If you can see a quick easy solution, I would be most grateful.

Here's my code so far, note it only handles positive integers!

Code:
```#include <iostream>
#include <stdlib.h>
#include <math.h>

using namespace std;

int main()
{
int top_part;
int bottom_part;
int smallest;

cout<<"Enter top part of fraction:";
cin>>top_part;

cout<<"Enter bottom part of fraction:";
cin>>bottom_part;

if (top_part>bottom_part)
{
smallest=bottom_part;
}
if (top_part<bottom_part)
{
smallest=top_part;
}

double it,its,newtop,newbot;
for (int a=1; a<=smallest; a++)
{

it=top_part%a;
its=bottom_part%a;

if (it==0)
{
if (its==0)
{
newtop=top_part/a;
newbot=bottom_part/a;
}
}
}
cout<<"When cancelled down..."<<endl;
cout<<newtop<<"/"<<newbot;

int stop;
cin>>stop;

return 0;
}```

2. There's a bunch of ways of testing if a number's negative....

Code Fun with jverkoey!

Code:
`if(val<0) // Negative`
Code:
`if(abs(val)!=val) // Negative`
And remember, unsigned variables can't be negative. Also remember to use fabs for double values and fabsf for float values!

3. Possible solutions include using the absolute function
That seems like the easiest:

if absolute value of numerator == numerator, then numerator_flag = true;
if absolute value of denominator == denominator, then denominator = true;

Then, you could use a switch with the 4 operations to get the sign--> true(plus) or false(minus).

4. Originally Posted by treenef
Hi guys,

Here's my code so far, note it only handles positive integers!
If you are happy with your code for positive integers, you could consider something like this

Code:
```  cin>>bottom_part;

int sign = (((top_part>0)&&(bottom_part<0)) || ((top_part<0)&&(bottom_part>0)));
top_part = abs(top_part);
bottom_part = abs(bottom_part);

if (top_part>bottom_part)```

Then:

Code:
```  cout<<"When cancelled down..."<<endl;
if (sign) {
cout << "- ";
}
cout<<newtop<<"/"<<newbot;```
Regards,

Dave

5. Might be clearer to use XOR:
Code:
```//if top_part -ve XOR bottom_part -ve
bool negsign = (top_part < 0 || bottom_part < 0) && !(top_part < 0 && bottom_part < 0);
//make both non-negative
top_part = abs(top_part);
bottom_part = abs(bottom_part);```
After all, it is easy to say in words that "if numerator or denominator is negative, but not both negative, then fraction is negative".

Incidentally, wouldnt it be somewhat better to find the GCD, and then divide both by that?
Of course, then there's a missing check for division by zero.