Reputation: 11
The program asks the user for a numerator and denominator in the enter function, then it needs to simplify and then display it. I tried running it and my program broke.
Any tips on how to do this?
I am still trying to learn how to do structures.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct Fraction
{
int numerator;
int denominator;
};
void enter(struct Fraction *choice)
{
printf("Numerator: \n");
scanf("%d", choice->numerator);
printf("Denominator: \n");
scanf("%d", choice->denominator);
}
void simplify(struct Fraction *reduce)
{
reduce->numerator = reduce->numerator / reduce->numerator;
reduce->denominator = reduce->denominator / reduce->denominator;
}
void display(const struct Fraction *show)
{
printf("%d / %d", show->numerator, show->denominator);
}
int main(void)
{
struct Fraction f;
printf("Fraction Simplifier\n");
printf("===================\n");
enter(&f);
simplify(&f);
display(&f);
}
Upvotes: 0
Views: 49
Reputation: 206577
Problem 1
The lines
scanf("%d", choice->numerator);
scanf("%d", choice->denominator);
need to be:
scanf("%d", &choice->numerator);
scanf("%d", &choice->denominator);
// ^^ Missing
Problem 2
The following lines:
reduce->numerator = reduce->numerator / reduce->numerator;
reduce->denominator = reduce->denominator / reduce->denominator;
are equivalent to:
reduce->numerator = 1.0;
reduce->denominator = 1.0;
You need code to compute the GCD of the numerator and denominator and then use:
double gcd = get_gcd(reduce->numerator, reduce->denominator);
reduce->numerator = reduce->numerator/gcd;
reduce->denominator = reduce->denominator/gcd;
Upvotes: 2