# 2 quick questions

• 11-28-2001
Unregistered
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 subtract(rationalType fraction1, rationalType fraction2);
rationalType multiply(rationalType fraction1, rationalType fraction2);
rationalType divide(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");
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...
• 11-29-2001
quzah
Quote:

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.
• 11-29-2001
Unregistered
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?
• 11-29-2001
QuestionC
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.