I'm a computer science student and I did a test recently when I solved a cool question in a weird way but I think I succeeded. I'm ask before I appeal the test.
The question is to find the largest fraction from an array of struct.
The way I solved the question is by dividing into cases instead of a common denominator.
*** The Denominator always smaller then the numerator and the number is positive
*** After I run few end cases I did it wrong.
Thanks for all !
Fraction who_is_the_biggest(Fraction* a, int size) {
Fraction max = a[0];
int i;
// find the max num
for (i = 0; i < size; i ) {
if (a[i].whole > max.whole) {
max = a[i];
}
// if a[i] == max do compare bewteen numerator and denominator
if (a[i].whole == max.whole) {
if (a[i].denominator == max.denominator && a[i].numerator > max.numerator) { // 3/4 & 2/4 --- > 3/4
max = a[i];
}
else if (a[i].numerator == max.numerator && a[i].denominator < max.denominator) { // 3/5 & 3/6 --- > 3/5
max = a[i];
}
else if (a[i].numerator != max.numerator && a[i].denominator != max.denominator) { // 2/100 & 3/4 --> 3/4
if (a[i].numerator > max.numerator && a[i].denominator < max.denominator) {
max = a[i];
}
}
}
}
return max;
}
CodePudding user response:
You algorithm is completely wrong.
Test it
int main(void)
{
Fraction a[] =
{
{.whole = 0, .numerator = 98, 101}, {.whole = 0, .numerator = 97, 99}
};
Fraction f = who_is_the_biggest(a, 2);
printf("%d %d %f\n", f.numerator, f.denominator, ((double)a[0].numerator / a[0].denominator) / ((double)a[1].numerator / a[1].denominator));
}
https://godbolt.org/z/xPbWPzTWd
It returns a[0] but a[0] is smaller than a[1].
You need to find the smallest common denominator, then compare the fraction parts.
I did a test recently when I solved a cool question in a weird way but I think I succeeded
No, you did not succeed, do not appeal.
