simplify fractions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AmLegacy
    New Member
    • Feb 2008
    • 6

    simplify fractions

    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.

    [CODE=c]//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_fracti ons (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_fracti ons (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_fraction s (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_fracti ons (a_num, b_denom, c_num, d_denom, &ans1, &ans2);

    printf ("Your answer is %d/%d \n", ans1, ans2);

    }

    else
    if (operation == 3)

    {

    multiply_fracti ons (a_num, b_denom, c_num, d_denom, &ans1, &ans2);

    printf ("Your answer is %d/%d \n", ans1, ans2);

    }

    else
    if (operation == 4)

    {

    divide_fraction s (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);
    }
    }[/CODE]
    Last edited by Ganon11; Feb 19 '08, 03:14 AM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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.

    Comment

    Working...