473,241 Members | 1,626 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,241 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 6038
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.