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;
}
Related
I wrote a program to calculate the value of e^x by series and by library function.See the following:
#include<stdio.h>
#include<math.h>
long long fact(int x)
{
long prod=1;
int i=1;
if(x==0)
return 1;
else{
while(i<=x)
{
prod=prod*i;
i++;
}
return prod;
}
}
int main()
{
int i;
float x;
double sum=1;
for(x=1;x<20;x++)
{
for(i=1;i<=10;i++)
{
if(fact(i)!=0);
sum=sum+pow(x,i)/fact(i);
}
printf("by code e=%.15lf\t\t",sum);
printf("by libfnc e=%.15f\t",exp(x));
printf("quotient =%.15f\n",sum/exp(x));
}
}
The code works for smaller values like 1,2 but with the increase of the value of x the difference (here quotient) increases.That is my code no longer gives correct answer for higher values of x.
You need function prototype before calling it.
Add some ifs to check if you do not divide by zero.
scanf returns number of successfully scanned elements. Check for 1 in this case.
int fct = fact(2*i);
if(fct)
sum=sum+y/(double)fct;
else
{printf("DIVISION BY ZERO!!!!\n"); return 1;}
You will discover that int is to small for factorial function. Change it to double. Also, use double in all floating point operations.
double fact(int x);
int main()
{
double sum,y;
int x,i=0;
double fct;
while(scanf("%d",&x) == 1)
{ sum=0;
for(i=0;i<=N;i++)
{
y=pow(-1,i)*pow(x,2*i);
fct = fact(2*i);
if(fct != 0.0)
sum=sum+y/fct;
else
{printf("DIVISION BY ZERO!!!!\n"); return 1;}
}
printf("using Maclaurin's series %.10f and original cosx=%.10f",sum,cos(x));
}
}
double fact(int x)
{
double prod=1.0;
int i=1;
for(i=1;i<=x;i++)
prod=prod*i;
return prod;
}
https://godbolt.org/z/qsh4qh3d1
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);
}
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().
I am attempting to create some code with C programming that can show a number to a certain power with both values specified by the user. I want to use pow for the first part and a while loop for the second. However, I've confronted an error with this current code that I cannot seem to get rid of.
Here is ther error that I am unfamiliar with:
error: invalid operands to binary * (have ‘int (*)(int, int)’ and ‘int’)
result2 = result2 * base;
I've tried looking into other questions with the same error, but they differ so much that I cannot understand.
I've tried researching into "long" but I have not been experienced with it yet through my C textbook, so I would like to refrain from using it if possible.
#include <stdio.h>
#include <math.h>
int result2(int base, int exponent);
int main(void)
{
double base;
double exponent;
double result1;
puts("Please enter a value as the base and another as the exponent.");
scanf("%lf%lf", &base, &exponent);
result1 = pow(base, exponent);
printf("Library solution: %lf\n", result1);
printf("My solution: %d\n", result2(base, exponent));
}
int result2(int base, int exponent)
{
int i;
for(i=1; i<=exponent; i++)
{
result2 = result2 * base;
}
return;
}
I would like to be able to calculate the equation properly using both methods with the user's values. However, with this error, I just cannot seem to get past and achieve that. Thank you.
int result2(int base, int exponent)
{
int i;
for(i=1; i<=exponent; i++)
{
result2 = result2 * base;
}
return;
}
result2 is the name of your function. Rather than using it like a variable that can accumulate the results of the loop, you should create a variable to do that job. Then return the value of the variable at the end.
int result2(int base, int exponent)
{
int i;
int result = 1;
for(i=1; i<=exponent; i++)
{
result = result * base;
}
return result;
}
Better yet, give the function a different name. result2 sounds like the name of a variable. The function should be named something that indicates what it does. Since pow is taken, how about power?
int power(int base, int exponent)
{
int result = 1;
for (int i=1; i<=exponent; i++)
{
result *= base;
}
return result;
}
Some other small improvements are declaring int i inside the for loop, and using result *= base as shorthand for result = result * base.
i made a few modifications to your code.
1) Renamed funtion result2 to my_exp. result2 sound like a variable.
2) Modify types for funtion my_exp to int to be consistent with the variables types of main.
3) Add a variable result in my_exp to store the partial result of for loop.
4) Declared i inside for declaration.
5) call pow and my_exp inside printf and remove result1 is not necesary anymore.
6) Changed how result is calculed to a more compact way.
most of those changes are cosmetic except for types changes.
int my_exp(int base, int exponent);
int main(void)
{
int base;
int exponent;
puts("Please enter a value as the base and another as the exponent.");
scanf("%i", &base);
scanf("%i", &exponent);
printf("Library solution: %i\n", pow(base, exponent));
printf("My solution: %i\n", my_exp(base, exponent));
}
int my_exp(int base, int exponent)
{
//int i;
int result;
result = 1;
for(int i=1; i<=exponent; i++)
{
result *= base;
}
return result;
}
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