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);
}
Related
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'm trying to make a little power calculator for learning purposes in C without using pow,
but it always returns 0.00 when exponent is negative, please help.
full code:
#include<stdio.h>
//* power caculator function
int power(x,y)
{
float p=1.00;
int i;
if (y<0){
y=-1*y;
x=1/x;
}
for (i=1;i<=y;i++)
{
p=p*x;
}
return p;
}
//* main gets input, calls power caculator and prints result'
int main()
{
int b;
int e;
float p;
printf("enter base");
scanf("%d",&b);
printf("enter exponent");
scanf("%d",&e);
p=power(b,e);
printf("%d to the power of %d is %.2f",b,e,p);
return 0;
}
//* I am NOOB
You are using integers to hold decimal values, in this case with x and with the return type of the power function.
try:
float power(x,y)
{
float p=1.00;
float xx = (float)x;
int i;
if (y<0){
y=-1*y;
xx=1/xx;
}
for (i=1;i<=y;i++)
{
p=p*xx;
}
return p;
}
define data types of x and y explicitly and then adjust the return data type.
I need to convert my float (sensor) value to a char array for sending my data through GPRS. If I use any library function for converting, it takes a lot of memory, because the controller has less amount of flash. I have tried my best to convert the data without a library function, but finally it needs the math.h file compulsorily. How can I solve this issue without any library function?
#include <stdio.h>
#include <string.h>
#include <math.h>
unsigned char str[20];
unsigned char *ftos(float f,int precision)
{
memset(str,0,sizeof(str));
float ff;
ff = f;
int a,b,c,k,l=0,m,i=0;
// check for negetive float
if(f<0.0)
{
str[i++]='-';
f*=-1;
}
a=f; // extracting whole number
f-=a; // extracting decimal part
k = precision;
// number of digits in whole number
while(k>0)
{
l = pow(10,k);
m = a/l;
if(m>0)
{
break;
}
k--;
}
// number of digits in whole number are k+1
/*
extracting most significant digit i.e. right most digit , and concatenating to string
obtained as quotient by dividing number by 10^k where k = (number of digit -1)
*/
for(l=k+1;l>0;l--)
{
b = pow(10,l-1);
c = a/b;
str[i++]=c+48;
a%=b;
}
str[i++] = '.';
/* extracting decimal digits till precision */
for(l=0;l<precision;l++)
{
f*=10.0;
b = f;
str[i++]=b+48;
f-=b;
}
str[i]='\0';
return str;
}
int main()
{
float temp = 35.2;
printf("%s",ftos(temp,2));
}
Don't try to print a floating point value, print a fixed point value instead. For example, this prints value of x with 2 digits after the decimal point:
int main()
{
float x = 35.2;
printf("%d.%02d\n", (int)x, (int)(x * 100) - (int)x * 100);
}
If you need to actually convert the value to a char array, use sprintf instead of printf. Be careful to avoid integer overflows when multiplying, especially if your platform has 16-bit integers: use long values if required.
Overall, there are very few cases when printing floating point numbers is a good idea from a microcontroller C code.
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;
}
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