Structures function

I'm writing a function that compares two fraction. if the fractions are equal it returns 0. If the fraction in the first parameter is less than the fraction in the second parameter it returns a negative number. Otherwise it returns a positive number. in doing so convert the fraction to a floating point number.

Im stuck need help completing the function to run


typedef struct fracion
{
int numo;
int denom;
}fraction;

int compareFractions (fracion, fraction);

void main()
{
int x;
fraction f1, f2;
printf("Enter Numo and Demo");
scanf_s("%d%d", &f1.numo, &f1.denom);
printf("Enter Numo and Demo");
scanf_s("%d%d", &f2.numo, &f2.denom);

x = compareFractions(f1, f2);
}

int compareFractions(fraction frac1, fraction frac2)
{
float conf1;
float conf2;

conf1 = frac1.numo / frac1.denom;
conf1 = frac2.numo / frac2.denom;



}
Here try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int main()
{
    int frac[4];
    std::cout << ("Enter Numerator:");
    std::cin >> frac[0];
    std::cout << ("\nDenominator: ");
    std::cin >> frac[1];
    std::cout << ("Enter Numerator:");
    std::cin >> frac[2];
    std::cout << ("\nDenominator: ");
    std::cin >> frac[3];
    if((frac[0] && frac[1]) == (frac[2] && frac[3]))
    {
        if((frac[0] / frac[1]) < (frac[2] / frac[3]))
            std::cout << (frac[0] / frac[1]) << (' ') << (frac[2] / frac[3]);
            return(-1);
    }
    else
    {
        std::cout << (frac[0] / frac[1]) << (' ') << (frac[2] / frac[3]);
        return(0);
    }
    
}
Last edited on
if p/q == a/b then p*b = a*q.

if p/q < a/b then p*b < a*q etc ...

So, if you enter the two fractions as p,q,a & b you can apply the tests and produce the result you want all in integers.
I see where thesethese solutions would work but I'm look for something that say in the statements below which is guided from the original question.

Return 0
Else
Return -1
Well, you must remember that main() cannot be type void it must always be int main(). Your best bet is to seperate the code:

compareFraction.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
typedef struct fracion
{
int numo;
int denom;
}fraction;
int compareFractions(fraction frac1, fraction frac2)
{
float conf1, conf2;
conf1 = frac1.numo / frac1.denom;
conf1 = frac2.numo / frac2.denom;
if(conf1 == conf2)
{
return(0);
}
else
{
return(-1);
}
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "compareFraction.h"
int main()
{
int x;
fraction f1, f2;
printf("Enter Numo and Demo");
scanf_s("%d%d", &f1.numo, &f1.denom);
printf("Enter Numo and Demo");
scanf_s("%d%d", &f2.numo, &f2.denom);	

x = compareFractions(f1, f2);
return(2); //Non-void functions must return a value.
}

And then go from there.
Last edited on
Registered users can post here. Sign in or register to post.