-
Problems with module
Hey everyone,
I'm sure at the end it's just a stupid mistake, but I need some help at a something i'm writing on atm.
The idea is to add, subtract etc. fractions. I want to put the functions in a header and call them in the programm itself.
My problem is, that it doesnt recognize the functions i wrote in the .c of the module.
I'm a total beginner so pls no hate
The .h
Code:
#ifndef rational_h#define rational_h
#endif
typedef struct {
long numerator, denumerator;
}rational;
rational fraction1;
rational fraction2;
rational result;
The .c for the module
Code:
#include "rational.h"
rational addition(rational fraction1, rational fraction2,rational result)
{
result.numerator = fraction1.numerator + fraction2.numerator;
result.denumerator = fraction1.denumerator;
return result;
};
rational subtraction(rational fraction1, rational fraction2, rational result)
{
result.numerator = fraction1.numerator-fraction2.numerator;
result.denumerator = fraction1.denumerator;
return result;
};
rational multiplication(rational fraction1, rational fraction2, rational result)
{
result.numerator = fraction1.numerator * fraction2.numerator;
result.denumerator = fraction1.denumerator;
return result;
};
rational division(rational fraction1, rational fraction2, rational result)
{
long LMC;
result.numerator = fraction1.numerator * fraction2.denumerator;
result.denumerator = fraction1.denumerator*fraction2.numerator;
if (fraction1.denumerator < fraction2.denumerator)
{
LMC=ggT(fraction1.denumerator, fraction2.denumerator);
}
else
{
LMC = ggT(fraction2.denumerator, fraction1.denumerator);
}
result.numerator *= (LMC / result.denumerator);
result.denumerator = LMC;
return result;
};
long ggT(long a, long b)
{
static long helper = 0;
helper += b;
if (helper%a == 0 && helper%b == 0)
{
return helper;
}
else
{
return ggT(a, b);
}
}
The program itself (in between the functions im gonna add something for the output)
Code:
#include "rational.h"
int main(void)
{
addition(fraction1, fraction2);
subtraction(fraction1, fraction2);
division(fraction1, fraction2);
multiplication(fraction1, fraction2);
return 0;
}
Thanks everyone :)
-
You need to declare the functions in the header file, like so:
Code:
#ifndef RATIONAL_H
#define RATIONAL_H
typedef struct {
long numerator, denumerator;
}rational;
rational addition(rational fraction1, rational fraction2,rational result);
rational subtraction(rational fraction1, rational fraction2, rational result);
rational multiplication(rational fraction1, rational fraction2, rational result);
rational division(rational fraction1, rational fraction2, rational result);
long ggT(long a, long b);
#endif
Also note the #endif goes at the end, since the whole header should be wrapped in the #define.
You should not declare variables in a header file, and should avoid global variables in general. Those variables can be declared locally where needed.
Your function calls don't provide enough arguments to the function:
Code:
// function call with two arguments
addition(fraction1, fraction2);
// function has three parameters
rational addition(rational fraction1, rational fraction2,rational result)
{
result.numerator = fraction1.numerator + fraction2.numerator;
result.denumerator = fraction1.denumerator;
return result;
};
If you're returning "result", it does not have to be an argument.
-
Your header should look like this:
Code:
#ifndef RATIONAL_H
#define RATIONAL_H
typedef struct {
long numerator, denumerator;
} rational;
rational addition(rational fraction1, rational fraction2,rational result);
#endif
Notice that the header inclusion guard now encloses the content of the header. I have removed the global variable declarations, partly because you don't need them, and partly because if you did need them, typically they would be declared extern in the header:
Code:
extern rational fraction1;
extern rational fraction2;
extern rational result;
then declared (and hence defined) in exactly one source file.
Finally, I have added one related function declaration. You should add the remaining ones.
Of course, since the header no longer has the global variable declarations, you should declare those variables as local to the main function.
-
rational.h:
Move #endif to end
Add function prototypes
Remove definitions of fraction1, fraction2, result
"denumerator" (not a word) should be "denominator".
rational.c:
Get rid of semicolons after final brace of functions.
"addition" and "subtraction" need to convert both fractions to the same denominator (presumably through cross-multiplication).
"multiplication" needs to multiply the denominators.
I don't know what ggT is supposed to be. Usually you would use a gcd (greatest common divisor) function to help reduce the fractions to lowest terms. It would need to be used for all the operations.
main.c:
define fraction1, fraction2, result.
Accept return value in result.
-
First off, thanks to everyone :)
@john.c
rational.h:
-what do you mean by prototypes?
-english skills on point :D
rational.c:
-I did not convert the denominators, because of ggT. It's supposed to be a recursive approach on finding the lowest common divisor. I want to add this function in the part where the user inputs the fractions. So that I can change them there
This way I also only need to muliply the nominators
-and what do you mean with your last sentence?
Thank you a lot :)