How to compare 2 fractions that are stored in pointer instances? consider the below code:
struct frac {
int num;
int denom;
};
We need to implement : fr_equal(a,b) returns true if *a is equal to *b.
bool fr_equal(const struct frac *a, const struct frac *b);
I am facing difficulties in this, my code is as below:
bool fr_equal(const struct frac *a, const struct frac *b) {
if ( (*a).num != 0 && (*b).num !=0)
{
int x = (*a).num;
int y = (*a).denom;
int m = (*b).num;
int n = (*b).denom;
if (x==m && y==n )
return true;
if (x%y==0)
y=y/x;
else
return false;
}
else
{
return false;
}
}
My code is incomplete, as I haven't understood how to proceed and what all cases to consider. I could have 4 cases but its too long.
Can anyone tell me a short and efficient way of doing this
Thanks!
Aucun commentaire:
Enregistrer un commentaire