473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

simplify fractions

AmLegacy
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.

Expand|Select|Wrap|Line Numbers
  1. //This is the fraction adding function
  2. void add_fractions (int a, int b, int c, int d, int *ansn, int *ansd)
  3. {
  4.     //int e, x, y, rem, gcd;
  5.     a = a*d; //numerator 1 * denominator 2
  6.     c = c*b; //numerator 2 * denominator 1
  7.     b = b*d; // denominator 1 * denominator 2
  8.     d = b;  //denominator 2 = denominator 1
  9.     //e = c + a;
  10.     if ( d == 0 && b == 0)
  11.     {
  12.     printf ("Answer is undefined \n");//denominator = to 0 case
  13.     }
  14.  
  15.     *ansn = a + c;
  16.     *ansd = d;
  17. }
  18.  
  19. //This is the fraction subtraction function
  20.  
  21. void subtract_fractions (int a, int b, int c, int d, int *ansn, int *ansd)
  22. {
  23.     a = a*d; //numerator 1 * denominator 2
  24.     c = c*b; //numerator 2 * denominator 1
  25.     b = b*d;  // denominator 1 * denominator 2    
  26.     d = b; //denominator 2 = denominator 1
  27.  
  28.     if ( d == 0 && b == 0)
  29.     {
  30.     printf ("Answer is undefined \n");//denomiator = to 0 case
  31.     }
  32.  
  33.     *ansn = a - c;
  34.     *ansd = d;
  35. }
  36.  
  37.  
  38. //This is the fraction multiplication function
  39.  
  40. void multiply_fractions (int a, int b, int c, int d, int *ansn, int *ansd)
  41. {
  42.     a = a*c; //numerator 1 * numerator 2
  43.     c = a; // numerator 2 = numerator 1
  44.     b = b*d; //denominator 1 * denominator 2
  45.     d = b; //denominator 2 = denominator 1
  46.  
  47.     if ( d == 0 && b == 0)
  48.     {
  49.     printf ("Answer is undefined \n");
  50.     }
  51.  
  52.     *ansn = a;
  53.     *ansd = b;
  54.  
  55. }
  56.  
  57. //This is the fraction division function
  58. void divide_fractions (int a, int b, int c, int d, int *ansn, int *ansd)
  59. {
  60.     a = a*d; //numerator 1 * denominator 2
  61.     d = a;  //denominator 2 = to numerator 1
  62.     b = b*c; //denominator 1 * numerator 2
  63.     c = b;  // numerator 2 = denominator 1
  64.  
  65.     if ( d == 0 && b == 0)
  66.     {
  67.     printf ("Answer is undefined \n");
  68.     }
  69.  
  70.     *ansn = a;
  71.     *ansd = b;
  72.  
  73. }
  74.  
  75.  
  76.  
  77.  
  78. int main ()
  79. {
  80.     int a_num, b_denom, c_num, d_denom;
  81.     int ans1, ans2;
  82.     int operation;
  83.     int simplify; 
  84.     int x,y;
  85.     int rem, gcd;
  86.  
  87.  
  88.  
  89.     printf ("Please enter the first numerator> \n");
  90.     scanf ("%d", &a_num);
  91.  
  92.     printf ("Please enter the first denominator> \n ");
  93.     scanf ("%d", &b_denom);
  94.  
  95.     printf ("Please enter the second numerator> \n ");
  96.     scanf ("%d", &c_num);
  97.  
  98.     printf ("Please enter the second denominator> \n ");
  99.     scanf ("%d", &d_denom);
  100.  
  101.  
  102.     printf ("Please enter 1 for addition, 2 for subtraction, 3 for multiplication, or 4 for division> \n");
  103.     scanf ("%d", &operation);
  104.  
  105.  
  106.  
  107.     if (operation == 1)
  108.  
  109.     { 
  110.  
  111.     add_fractions (a_num, b_denom, c_num, d_denom, &ans1, &ans2);
  112.  
  113.     printf ("Your answer is %d/%d \n\n", ans1, ans2);
  114.  
  115.     }
  116.  
  117.     else
  118.     if (operation == 2)
  119.  
  120.     {
  121.  
  122.     subtract_fractions (a_num, b_denom, c_num, d_denom, &ans1, &ans2);
  123.  
  124.     printf ("Your answer is %d/%d \n", ans1, ans2);
  125.  
  126.     }
  127.  
  128.     else
  129.     if (operation == 3)
  130.  
  131.     {
  132.  
  133.     multiply_fractions (a_num, b_denom, c_num, d_denom, &ans1, &ans2);
  134.  
  135.     printf ("Your answer is %d/%d \n", ans1, ans2);
  136.  
  137.     }
  138.  
  139.     else
  140.     if (operation == 4)
  141.  
  142.     {
  143.  
  144.     divide_fractions (a_num, b_denom, c_num, d_denom, &ans1, &ans2);
  145.  
  146.     printf ("Your answer is %d/%d \n", ans1, ans2);
  147.  
  148.     }
  149.  
  150.  
  151.     else
  152.  
  153.     {
  154.  
  155.     printf ("No Real Solutions \n");
  156.  
  157.     }
  158.  
  159.     printf("Do you want to reduce the fraction? \n Press 1 for yes and 2 for no> \n");
  160.     scanf ("%d", &simplify);
  161.  
  162.     if (simplify == 1)
  163.  
  164.     {if (y==0)
  165.     gcd = x;
  166.     else
  167.     {
  168.         do
  169.         {
  170.         rem = x%y;
  171.         x = y;
  172.         y = rem;
  173.  
  174.  
  175.         }
  176.         while (y!=0);
  177.         gcd = x;
  178.  
  179.         printf ("%d", &x);
  180.     }
  181.  
  182. }
  183.  
  184.     if (simplify == 2)
  185.     {
  186.     printf("Your answer is %d/%d", ans1, ans2);
  187. }
  188. }
Feb 19 '08 #1
1 6051
Ganon11
3,652 Expert 2GB
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.
Feb 19 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
2
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 =...
33
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,...
5
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...
2
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). ...
4
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...
1
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...
5
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...
0
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...
0
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
0
BarryA
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.