I have asked several friends for help. I have no idea what is going wrong with my program. It's a multifile program so I will just post the functions that are being used in my test. I'm not quite sure if I am allocating the memory properly but I assume I am because of the if statement. Well here is my code. By the way please correct me if you see any bad habits or wrong conventions. I take constructive criticism very well. Heck even harsh criticism.
Code:
int test_reduce(char * err_messages)
{
Fraction inputs[REDUCE_COUNT] = REDUCE_INPUT;
Fraction corrects[REDUCE_COUNT] = REDUCE_CORRECT;
Fraction computed[REDUCE_COUNT] = {};
Fraction in_fraction = {0,1}, correct = {0,1}, result;
int i = 0, errors = 0;
for(i = 0; i < REDUCE_COUNT; i++) {
in_fraction = inputs[i];
correct = corrects[i];
result = in_fraction;
reduce_fraction(&result);
if (!equalFractions(result,correct)) {
if (!errors) {
strcat(err_messages,"\n\nReduce Errors:\n");
errors = 1;
}
sprintf(err_messages,"%s(%d,%d)\nreceived (%d,%d), correct (%d,%d)\n\n",
err_messages,in_fraction.numer,in_fraction.denom,result.numer,
result.denom,correct.numer,correct.denom);
}
}
return !errors;
}
int gcd(int a, int b)
{
if (a == 0 && b == 0)
{
printf("Illegal args to gcd: %d, %d\n",a,b);
exit(1);
}
a = abs(a);
b = abs(b);
if (a == 0)
return b;
if (b == 0)
return a;
return gcd(b,a%b);
}
void reduce_fraction(Fraction *R)
{
int cmnDenom;
Fraction *reduce;
reduce = (Fraction*) malloc(sizeof(reduce));
//reduce = (reduce*)malloc(sizeof(reduce));
if(reduce != NULL)
{
printf( "R->numer: %d\nR->denom: %d\n\n", R->numer,R->denom);
cmnDenom = gcd(R->numer,R->denom);
reduce->numer = R->numer/cmnDenom;
reduce->denom = R->denom/cmnDenom;
*R = *reduce;
}
free(reduce);
}