Answer to Question #75239 in C for jw

Question #75239
It turns out that struct s in C (not C++) are passed by value, both in and out of a function.
That means there is actually no need to use malloc and pass pointers around.
Thus, the function make_rat in presented in lecture could be defined like this:
Rat make_rat(int p, int q) {
int g = gcd(p, q);
Rat r = {p/g, q/g};
return r;
}

Provide an alternate implementation of Rat ADT without using pointers, along with the operations add_rat, sub_rat, mul_rat, div_rat.

Assume that the gcd function has been defined.

Change the code below to without pointer
Rat * add_rat(Rat *x, Rat *y) {
int nx = numer(x), dx = denom(x);
int ny = numer(y), dy = denom(y);
return make_rat(nx * dy + ny * dx,
dx * dy);
}
0
Expert's answer

Answer in progress...

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS