Thread: 2 quick questions

  1. #1
    Unregistered
    Guest

    2 quick questions

    hello..i need some help with my program. i cant get any of my functions to return their values in to the main function to print :-(
    the purpose of the program is to add, subtract, multiply, and divide rational numbers.

    this is my code:

    #include<stdio.h>

    struct rationalType{
    int numerator;
    int denominator;
    };
    typedef struct rationalType rationalType;

    rationalType add(rationalType fraction1, rationalType fraction2);
    rationalType subtract(rationalType fraction1, rationalType fraction2);
    rationalType multiply(rationalType fraction1, rationalType fraction2);
    rationalType divide(rationalType fraction1, rationalType fraction2);

    rationalType add(rationalType fraction1, rationalType fraction2)
    {
    int denom, num1, num2;
    rationalType result;
    denom = fraction1.denominator * fraction2.denominator;
    num1 = (denom/fraction1.denominator) * fraction1.numerator;
    num2 = (denom/fraction2.denominator) * fraction2.numerator;
    result.numerator = num1 + num2;
    result.denominator = denom;
    printf("%d/%d", result.numerator, result.denominator);
    return result;
    }

    rationalType subtract(rationalType fraction1, rationalType fraction2)
    {
    int denom, num1, num2;
    rationalType result;
    denom = fraction1.denominator * fraction2.denominator;
    num1 = (denom/fraction1.denominator) * fraction1.numerator;
    num2 = (denom/fraction2.denominator) * fraction2.numerator;
    result.numerator = num1 - num2;
    result.denominator = denom;
    return result;
    }

    rationalType multiply(rationalType fraction1, rationalType fraction2)
    {
    rationalType result;
    result.numerator = fraction1.numerator * fraction2.numerator;
    result.denominator = fraction1.denominator * fraction2.denominator;
    return result;

    }

    rationalType divide(rationalType fraction1, rationalType fraction2)
    {
    rationalType result;
    result.numerator = fraction1.numerator * fraction2.denominator;
    result.denominator = fraction2.denominator * fraction2.numerator;
    return result;
    }

    int main(void)
    {
    rationalType fr1;
    rationalType fr2;
    rationalType result;

    printf("Enter the First Fraction: " );
    scanf("%d/%d", &fr1.numerator, &fr1.denominator);
    printf("Enter the Second Fraction: ");
    scanf("%d/%d", &fr2.numerator, &fr2.denominator);
    printf("\nHere are the results: \n");
    add(fr1, fr2);
    printf("Sum : %d/%d\n", result.numerator, result.denominator);
    subtract(fr1, fr2);
    printf("Difference : %d/%d\n", result.numerator, result.denominator);
    multiply(fr1, fr2);
    printf("Product : %d/%d\n", result.numerator, result.denominator);
    divide(fr1, fr2);
    printf("Quotient : %d/%d\n", result.numerator, result.denominator);

    }


    and also..another question. my answers need to be in simplest form. so if the answer is 3/6 it should be reduced to 1/2. so basically my two questions are: how do i get my results to return and print and how would i make it so that the result printed is in its simplest form?

    any help is greatly appreaciated! thanks in advance...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    add(fr1, fr2);
    printf("Sum : %d/%d\n", result.numerator, result.denominator);
    subtract(fr1, fr2);
    printf("Difference : %d/%d\n", result.numerator, result.denominator);
    multiply(fr1, fr2);
    printf("Product : %d/%d\n", result.numerator, result.denominator);
    divide(fr1, fr2);
    printf("Quotient : %d/%d\n", result.numerator, result.denominator);
    Do you now know how return values work? You actually have to _USE_ the return value. Try this:

    result = add( fr1, fr2 );

    You actually have to give "result" a value before you can expect it to store the results of "add". The compiler doesn't just simply assume that because you aren't using "result" that it should automaticly stick every return value in it. It doesn't work that way.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    yeah, i figured it out after i submitted the thread. that was a dumb mistake on my part, sorry. but would u happen to know an easy to get the values in their simplest forms?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    The way you are calling your functions, they don't actually have any way to store their information in result...
    Basically, lines like this...

    subtract(fr1, fr2);

    Should look like this:

    result = subtract (fr1, fr2);


    Second, there's the issue of reducing your fractions. I suggest making a seperate function for this, the prototype of which will look like this:

    rationalType reduce(rationalType fraction1);

    And the logic of the function would go something like so...

    consider the fraction 12/16...
    12 is a multiple of 2
    16 is a multiple of 2
    so we reduce by 2... now we have 6/8
    6 is a multiple of 2
    8 is a multiple of 2
    so we reduce by 2... now we have 3/4
    3 isn't a multiple of 2
    so we go to the next number... 3
    3 is a multiple of 3
    4 is not a multiple of 3
    so we go to the next number... 4
    4 is larger than 3
    so we can stop now
    We end up with 3/4 as the reduced version of 12/16

    This algorithm should work for any fraction.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two quick questions about open file dialog boxes
    By PJYelton in forum Windows Programming
    Replies: 5
    Last Post: 04-05-2005, 08:49 AM
  2. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  3. A quick question(s)
    By EvBladeRunnervE in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2003, 09:39 PM
  4. A few quick questions...
    By cpp4ever in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2001, 09:28 AM