I'm having a hard time figuring out how to simplify the fractions. Can anyone look at this code and see if you can see something I don't. - //This is the fraction adding function
-
void add_fractions (int a, int b, int c, int d, int *ansn, int *ansd)
-
{
-
//int e, x, y, rem, gcd;
-
a = a*d; //numerator 1 * denominator 2
-
c = c*b; //numerator 2 * denominator 1
-
b = b*d; // denominator 1 * denominator 2
-
d = b; //denominator 2 = denominator 1
-
//e = c + a;
-
if ( d == 0 && b == 0)
-
{
-
printf ("Answer is undefined \n");//denominator = to 0 case
-
}
-
-
*ansn = a + c;
-
*ansd = d;
-
}
-
-
//This is the fraction subtraction function
-
-
void subtract_fractions (int a, int b, int c, int d, int *ansn, int *ansd)
-
{
-
a = a*d; //numerator 1 * denominator 2
-
c = c*b; //numerator 2 * denominator 1
-
b = b*d; // denominator 1 * denominator 2
-
d = b; //denominator 2 = denominator 1
-
-
if ( d == 0 && b == 0)
-
{
-
printf ("Answer is undefined \n");//denomiator = to 0 case
-
}
-
-
*ansn = a - c;
-
*ansd = d;
-
}
-
-
-
//This is the fraction multiplication function
-
-
void multiply_fractions (int a, int b, int c, int d, int *ansn, int *ansd)
-
{
-
a = a*c; //numerator 1 * numerator 2
-
c = a; // numerator 2 = numerator 1
-
b = b*d; //denominator 1 * denominator 2
-
d = b; //denominator 2 = denominator 1
-
-
if ( d == 0 && b == 0)
-
{
-
printf ("Answer is undefined \n");
-
}
-
-
*ansn = a;
-
*ansd = b;
-
-
}
-
-
//This is the fraction division function
-
void divide_fractions (int a, int b, int c, int d, int *ansn, int *ansd)
-
{
-
a = a*d; //numerator 1 * denominator 2
-
d = a; //denominator 2 = to numerator 1
-
b = b*c; //denominator 1 * numerator 2
-
c = b; // numerator 2 = denominator 1
-
-
if ( d == 0 && b == 0)
-
{
-
printf ("Answer is undefined \n");
-
}
-
-
*ansn = a;
-
*ansd = b;
-
-
}
-
-
-
-
-
int main ()
-
{
-
int a_num, b_denom, c_num, d_denom;
-
int ans1, ans2;
-
int operation;
-
int simplify;
-
int x,y;
-
int rem, gcd;
-
-
-
-
printf ("Please enter the first numerator> \n");
-
scanf ("%d", &a_num);
-
-
printf ("Please enter the first denominator> \n ");
-
scanf ("%d", &b_denom);
-
-
printf ("Please enter the second numerator> \n ");
-
scanf ("%d", &c_num);
-
-
printf ("Please enter the second denominator> \n ");
-
scanf ("%d", &d_denom);
-
-
-
printf ("Please enter 1 for addition, 2 for subtraction, 3 for multiplication, or 4 for division> \n");
-
scanf ("%d", &operation);
-
-
-
-
if (operation == 1)
-
-
{
-
-
add_fractions (a_num, b_denom, c_num, d_denom, &ans1, &ans2);
-
-
printf ("Your answer is %d/%d \n\n", ans1, ans2);
-
-
}
-
-
else
-
if (operation == 2)
-
-
{
-
-
subtract_fractions (a_num, b_denom, c_num, d_denom, &ans1, &ans2);
-
-
printf ("Your answer is %d/%d \n", ans1, ans2);
-
-
}
-
-
else
-
if (operation == 3)
-
-
{
-
-
multiply_fractions (a_num, b_denom, c_num, d_denom, &ans1, &ans2);
-
-
printf ("Your answer is %d/%d \n", ans1, ans2);
-
-
}
-
-
else
-
if (operation == 4)
-
-
{
-
-
divide_fractions (a_num, b_denom, c_num, d_denom, &ans1, &ans2);
-
-
printf ("Your answer is %d/%d \n", ans1, ans2);
-
-
}
-
-
-
else
-
-
{
-
-
printf ("No Real Solutions \n");
-
-
}
-
-
printf("Do you want to reduce the fraction? \n Press 1 for yes and 2 for no> \n");
-
scanf ("%d", &simplify);
-
-
if (simplify == 1)
-
-
{if (y==0)
-
gcd = x;
-
else
-
{
-
do
-
{
-
rem = x%y;
-
x = y;
-
y = rem;
-
-
-
}
-
while (y!=0);
-
gcd = x;
-
-
printf ("%d", &x);
-
}
-
-
}
-
-
if (simplify == 2)
-
{
-
printf("Your answer is %d/%d", ans1, ans2);
-
}
-
}
1 6051
At the very end, I see you using printf() to print an integer (%d), but you give it &x, which will point the address of x. Aside from this, I don't see any place where you use the greatest common denominator to reduce the fraction. You might clean up that code by writing a gcd() function - it would certainly look nicer.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: skip |
last post by:
I'm looking for a solution (or ideas about a solution) to the problem
that strftime(3) and strptime(3) don't understand time increments of
less than one second. Most operating systems can provide...
|
by: karp |
last post by:
Lets say I have the following class in order to add two fractions,
class Fraction{
public:
Fractions::Fraction(int numer = 0, int denom = 1)
{
valeurNumerateur = numer;
valeurDenominateur =...
|
by: selowitch |
last post by:
I've been searching in vain for a way to present typographically
correct fractions (not resorting to <sup> and <sub> tags) but have been
frustrated by the fact that the glyphs for one-half,...
|
by: Steffen |
last post by:
Hi,
is it possible to have two fractions, which (mathematically) have the
order a/b < c/d (a,b,c,d integers), but when (correctly) converted into
floating point representation just have the...
|
by: Mori |
last post by:
Hi,
Can someone supply a code example of displaying a string with a fractional
part, say 5 and 7 16ths.
I cannot find an example of how to use the Encoding object (if that is what
you use).
...
|
by: Bob |
last post by:
Hi All,
Was wondering if in C# there is an elegant way of displaying and or calculating
fractions.
The case: we have an app that measures/slices dices etc and all our internal
measures and...
|
by: Semajthewise |
last post by:
Here it is cleaned up a little more.
Here's what this code does. It will take 2 fractions and add, subtract, multiply, or divide them. The user enters the fractions to be calculated into two...
|
by: gubbachchi |
last post by:
Hi all,
How to convert the fractions into decimals and vice versa in php.
I have a form, where the user will enter fractions in the text boxes such as "1 1/2", "1 1/4" and so on. I need to store...
|
by: Paddy |
last post by:
(From: http://paddy3118.blogspot.com/2008/09/python-fractions-issue.html)
There seems to be a problem/difference in calculating with the new
fractions module when comparing Python 26rc2 and 30rc1...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |