How to call a function that simplifies a fraction in C? - c

This is what I need to do:
Define a structure called “fraction” with integer members “numerator” and
“denominator.” Prompt the user to enter a fraction in the form “#/#”. Call a function called
“simplFrac” that simplifies a fraction by dividing the numerator and denominator by the greatest
common factor and returns the simplified fraction to the calling function. Print the results from
main().
And here is my code:
#include <stdio.h>
struct fraction {
int numerator;
int denominator;
};
struct fraction simplFrac(struct fraction x);
int main(void) {
struct fraction a;
printf("Enter a fraction in the form #/#: ");
scanf("%i/%i",&a.numerator,&a.denominator);
printf("\n");
printf("The simplified fraction is: %i/%i\n",);
return 0;
}
struct fraction simplFrac(struct fraction x) {
int gcf,remainder;
while (x.numerator != 0)
{
remainder = x.denominator % x.numerator;
x.denominator = x.numerator;
x.numerator = remainder;
}
gcf = x.denominator;
x.numerator = x.numerator / gcf;
x.denominator = x.denominator / gcf;
return x;
}

Would you please try the following:
#include <stdio.h>
struct fraction
{
int numerator;
int denominator;
};
struct fraction simplFrac(struct fraction x);
int gcf(int, int);
int main(void)
{
struct fraction a;
char buf[BUFSIZ];
printf("Enter a fraction in the form #/#: ");
fgets(buf, BUFSIZ, stdin);
sscanf(buf, "%i/%i", &a.numerator, &a.denominator);
a = simplFrac(a);
printf("The simplified fraction is: %i/%i\n", a.numerator, a.denominator);
return 0;
}
struct fraction simplFrac(struct fraction x)
{
int factor = gcf(x.denominator, x.numerator);
x.numerator /= factor;
x.denominator /= factor;
return x;
}
int gcf(int a, int b)
{
if (b == 0) return a;
else return gcf(b, a % b);
}
The algorythm to calculate the gcf is separated as a function gcf().

Related

why does the value of this factorial change when called by a function that SHOULD calculate the value of e

#include <stdio.h>
#include <math.h>
int main()
{
double precision = 0;
printf("\ninsert number\n");
while(precision < 1){
scanf("%lf",&precision);
}
printf("the value of e with precision of %.0lf is %lf",precision,e(precision));
return 0;
}
int fact(int num){
int ris = 1;
for(int i = num;i > 0;i--){
ris = ris * i;
}
printf("res=%d\n",ris);
return ris;
}
int e(double precision){
double valE = 1;
for(double i = precision;i > 0 ;i--){
valE = valE + 1/fact(i);
printf("\nsame res:%.1lf\n",fact(i));
}
return (double)valE;
}
debug
i know there is an answer for that but my problem is the comunication between the 2 functions, i know i could solve it by slapping everything inside the main()
There are many issues:
format specifiers (for scanf and printf) must match the arguments
don't use floating point types as counters
if you divide one integer by another integer, the result will be an integer that is trucated. If you want the result to be a floating point type, you need to convert at least one of the operands to a floating point type.
you need to declare the functions you use (fact and e) before using them, or just put them before main, like below.
You want this, explanations in the comments:
#include <stdio.h>
#include <math.h>
int fact(int num) {
int ris = 1;
for (int i = num; i > 0; i--) {
ris = ris * i;
}
printf("res=%d\n", ris);
return ris;
}
double e(int precision) {
double valE = 1;
for (int i = precision; i > 0; i--) { // use int for loop counters
valE = valE + 1.0 / fact(i); // use `1.0` instead of `1`, otherwise an
// integer division will be performed
printf("\nsame res: %d\n", fact(i)); // use %d for int^, not %llf
}
return valE; // (double) cast is useless
}
// put both functions e and fact before main, so they are no longer
// declared implicitely
int main()
{
int precision = 0; // precision should be an int
printf("\ninsert number\n");
while (precision < 1) {
scanf("%d", &precision); // use %d for int
}
printf("the value of e with precision of %d is %lf", precision, e(precision));
return 0;
}

How to call a function to reduce fraction using C

i have to write a function to input and output a fraction, then add two fraction then i have to reduce the fraction.
And this is what i came up with
#include <stdio.h>
/*
1. Input
2. Output
3. Function
- Add Fraction
- Reduce fraction
*/
//Input
void inputFraction(struct FRACTION &F)
{
printf("Enter a Fraction (#/#): ");
scanf("%i/%i", &a.num, &a.deno);
a = reduceFraction(a)
}
//Output
void outputFraction(struct FRACTION F)
{
int a;
a = reduceFraction(a);
printf("Reduction form of the Fraction is: %i/%i\n", a.num, a.deno );
return 0;
}
//-----------FUNCTION-------------
struct FRACTION addFraction(struct FRACTION A, struct FRACTION B)
{
}
//
struct FRACTION
{
int num;
int deno;
};
//Reduce
struct FRACTION reduceFraction(struct FRACTION F)
{
int factor = gcf(F.deno, F.num);
F.num /= factor;
F.deno/= factor;
return F;
}
//--------------------------------
//
int main()
{
int F;
inputFraction(F);
outputFraction(F);
}
But i didn't know how to do it? if i call it in MAIN() then it wont work with other with out INT it again?
struct FRACTION &F is an invalid syntax in C. If you want to use pass-by-reference in C, you will have to emulate that using pointers.
The function reduceFraction returns struct FRACTION. It won't fit in int. You should use struct FRACTION for receiving the return value.
Ignoring the argument F and using only new variable a in the functions inputFraction and outputFraction doesn't make sense. You should use the argument F.
You have to declare or define functions and structures before using them.
You cannot do return 0; in a function whose return type is void.
Improved code:
#include <stdio.h>
/*
1. Input
2. Output
3. Function
- Add Fraction
- Reduce fraction
*/
// move structure declaration before where it is used
//
struct FRACTION
{
int num;
int deno;
};
// declare function to use
struct FRACTION reduceFraction(struct FRACTION F);
//Input
void inputFraction(struct FRACTION *F) // receive a pointer
{
printf("Enter a Fraction (#/#): ");
scanf("%i/%i", &F->num, &F->deno); // use F instead of a
*F = reduceFraction(*F); // use F instead of a
}
//Output
void outputFraction(struct FRACTION F)
{
struct FRACTION a; // use the structure instead of int
a = reduceFraction(F); // use the argument
printf("Reduction form of the Fraction is: %i/%i\n", a.num, a.deno );
// remove invalid and meaningless return statement
}
//-----------FUNCTION-------------
struct FRACTION addFraction(struct FRACTION A, struct FRACTION B)
{
// TODO: implement this
return A;
}
int gcf(int a, int b) {
// TODO: implement this
return 1;
}
//Reduce
struct FRACTION reduceFraction(struct FRACTION F)
{
int factor = gcf(F.deno, F.num);
F.num /= factor;
F.deno/= factor;
return F;
}
//--------------------------------
//
int main()
{
struct FRACTION F; // use the structure instead of int
inputFraction(&F); // pass a pointer
outputFraction(F);
}

New to structs, how come my program is not running?

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);
}
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;

Recursive Function Struct Simplify Fractions

I am having a hard here and could use some help (I have been at this for hours and am getting no where...).
OK, so my problem is that I can't figure out how to make a Function that uses a typedef to return a simplified fraction. In other words I want to use Euclidean method to get GCD like this:
int gcd(int a, int b)
{
int rem;
if ((rem = a % b) == 0) {
return b;
}
else {
return gcd(b, rem);
}
}
Then simply with something like this:
int result = gcd(num, den);
int simple_num = num / result;
int simple_den = den / result;
printf("%d / %d", simple_num, simple_den);
But, I am trying to make it with a typedef called fraction and use one function that will return my simplified numerator and denominator.
typedef struct
{
int numerator;
int denominator;
} Fraction;
Fraction simplify(Fraction myFraction)
{
return myFraction;
}
Anyone know how this could be done?
This should work:
Fraction simplify(Fraction myFraction)
{
int result = gcd(myFraction.numerator, myFraction.denominator);
int simple_num = num / result;
int simple_den = den / result;
Fraction newFraction = {simple_num, simple_den};
return newFraction;
}

Error: request for member in something not a structure or union [duplicate]

This question already has answers here:
Compile error: with request not something structure or union error
(2 answers)
Closed 8 years ago.
I'm having trouble with my code. My program is a program to simplify fractions. So my problem is this:
I declare the structure Fraction. And then I declare structure Fraction f in my main function.
But when I try to use any member of the structure fraction (i.e. f.num, f.den) it says it's not a member of a structure or union. I have like 10 errors all saying the same thing for my program.
The error (verbatim): error: request for member "num" in something not a structure of union
#include <stio.h>
struct Fraction{
int num;
int den;
int lownum;//lownum = lowest numerator.
int lowden;//lowden = lowest denominator
int error;
};
void enter(struct Fraction *f);
void simplify(struct Fraction *f);
void display(const struct Fraction *f);
int main(void){
struct Fraction f;
printf("Fraction Simplifier\n");
printf("===================\n");
enter(&f);
simplify(&f);
display(&f);
}
void enter(struct Fraction *f){
printf("Please enter numerator : \n");
scanf("%d", &f.num);
printf("please enter denominator : \n");
scanf("%d", &f.den);
printf("%d %d", f.num, f.den);
}
void simplify(struct Fraction *f){
int a;
int b;
int c;
int negative; //is fraction positive?
a = f.num;
b = f.den;
if (a/b < 0){
negative = 1;
}
if(b == 0){
f.error = 1;
}
if(a < 0){
a = a * -1;
}
if(b < 0){
b = b * -1;
}
//euclids method
if(a < b){
c = a;
a = b;
b = c;
}
while(b != 0){
c = a % b;
a = b;
b = c;
}
f.lownum = f.num / a;
f.lowden = f.den / a;
if(negative = 1){
f.lownum = f.lownum * -1;
}
}
void display (const struct Fraction *f){
if (f.error != 1){
printf("%d / %d", f.lownum, f.lowden);
}else{
printf("error");
}
}
In
void simplify(struct Fraction *f)
f is a pointer to struct Fraction. Therefore, instead of
a = f.num;
you have to write
a = (*f).num;
which can be shortened to the equivalent notation:
a = f->num;
The same applies to all other references to struct Fraction *f in your functions.
You use a pointer (struct Fraction *f). So you have to acces members with the -> operator:
f->num

Resources