# Answer to Question #75236 in C for jw

Question #75236

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);

}

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);

}

Expert's answer

#### Not answered

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment