Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

math in C++ 2

Status
Not open for further replies.

RollerMan

Technical User
Jan 27, 2002
6
0
0
US
I am working with classes in C++ and I can get the math part of it down. The problem I am having is figuring a way to reduce a fraction within a function say for instance I needed to reduce a fraction to the lowest term
Like for example:

10/12 to reduce to 5/6 I can get the math to get to 10/12 but I am trying to figure out how I could write a function to reduce any fraction of any size. Even if I had an example like 25/6 this would remain 25/6 because I am not mixing reduced fractions.

Any suggestions???
 
Lets say,that you have some fraction "a/b" that you want to reduce,then you need to find the biggest common factor of "a" and "b".

This can be done by using this little program:

#include <iostream.h>
void main()
{
int a = 10;
int b = 12;
for( int i = a; i > 0; i-- )
{
if(!( a%i) && !( b%i ))
break;
}
a = a/i;
b = b/i;
cout<<&quot;a = &quot;<<a<<&quot; b = &quot;<<b;
}

So,you might modified it and using it as a fonction to reduce any fraction you want to.
 
write a function called common factor that returns the common factor of the 2 numbers. If 1 is returned you know they are as low as they go.

Matt
 
I could be wrong, but I think ANSI C has a function for finding common denominators...
 
Hi, can you help me how to make this FIBONACI SERIES, i don't know how to make it, pleasssssseeee....
 
#define N 100
int f[N +1];
f[0] = 0;
f[1] = 1;
for (int i = 2;i++; i<=N) f = f[i-1] + f[i-2];

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top