im having trouble not gettin correct output, it give me 0.
Code:void addFrac(Frac a,Frac b,Frac c,Frac e) { int sum; sum=(a.n*b.d)+(b.n*a.d); sum+=(c.n*e.d)+(e.n*c.d); //printing sums printf("\n%.2f is the total\n",sum); }
im having trouble not gettin correct output, it give me 0.
Code:void addFrac(Frac a,Frac b,Frac c,Frac e) { int sum; sum=(a.n*b.d)+(b.n*a.d); sum+=(c.n*e.d)+(e.n*c.d); //printing sums printf("\n%.2f is the total\n",sum); }
Last edited by SledMan2002; 11-25-2002 at 06:17 PM.
What are you trying to do here? You realize that you can't just add fractions and expect it to give you a non-fraction (ie: whole) number.
Your addition is incorrect. Do you know how to add fractions?
Quzah.
Hope is the first step on the road to disappointment.
I was over at his house earlier (trying to figure this junk out). We decided to see if we could spit out a decimal that worked correctly. I'm not sure if how he was adding the fractions (cross multipying) was working or not. Right now for the decimal output we're receiving zero. I'm not sure what's wrong, but i know it has to do with how we're adding the fractions...we'll get fraction output later, for now decimal's fine. Thanks.
PHP and XML
Let's talk about SAX
i think i have the adding function messed up. C what u can do with it for me.
Code:#include <stdio.h> #include <math.h> typedef struct fraction{ int n; int d; }Frac; void getFrac(Frac a,Frac b,Frac c,Frac e); void addFrac(Frac a,Frac b,Frac c,Frac e); /////////////////////////////////MAIN///////////////////////////////////////////////// void main() { Frac a={0,1},b={0,1},c={0,1},e={0,1},sum={0,1}; getFrac(a,b,c,e); addFrac(a,b,c,e); } /////////////////////////////////FUNCTIONS//////////////////////////////////////////// void getFrac(Frac a,Frac b,Frac c,Frac e) { printf("Enter Fraction\n"); scanf("%d%*c%d",&a.n,&a.d); while(a.d==0){ printf("Enter Fraction Again\n"); scanf("%d%*c%d",&a.n,&a.d); } printf("Enter Fraction\n"); scanf("%d%*c%d",&b.n,&b.d); while(b.d==0){ printf("Enter Fraction Again\n"); scanf("%d%*c%d",&b.n,&b.d); } printf("Enter Fraction\n"); scanf("%d%*c%d",&c.n,&c.d); while(c.d==0){ printf("Enter Fraction Again\n"); scanf("%d%*c%d",&c.n,&c.d); } printf("Enter Fraction\n"); scanf("%d%*c%d",&e.n,&e.d); while(e.d==0){ printf("Enter Fraction Again\n"); scanf("%d%*c%d",&e.n,&e.d); } //printing inputed printf("Input Reads...\n"); printf("\n%d/%d\n",a.n,a.d); printf("%d/%d\n",b.n,b.d); printf("%d/%d\n",c.n,c.d); printf("%d/%d\n\n",e.n,e.d); } void addFrac(Frac a,Frac b,Frac c,Frac e) { int sum; sum=(a.n*b.d)+(b.n*a.d); sum+=(c.n*e.d)+(e.n*c.d); //printing sums printf("\n%.2f is the total\n",sum); }
Last edited by SledMan2002; 11-25-2002 at 06:19 PM.
You should make a simple function first. Make one that adds two fractions:
That should get you started.Code:int sum1, sum2; sum1 = fraction1.d; sum2 = fraction2.d; fraction1.n *= sum2; fraction1.d *= sum2; fraction2.n *= sum1; fraction2.d *= sum1; fraction1.n += fraction2.n; fraction1.d += fraction2.d;
Quzah.
Hope is the first step on the road to disappointment.
ah, quzah, you mean you did the code starting in the main function??? and then try to split it up to other function? is coding programming better and easlier?
what can u do with it then, i think itz a syntax error in it. let me know what u get.
Dave
If it's a syntax error then you should be able to figure it out pretty easily. That is, just try to compile it.Originally posted by SledMan2002
what can u do with it then, i think itz a syntax error in it. let me know what u get.
i have been tryin to figure out how to make the addFrac function come up with a common denominator, how can i make this give me the right answer in fraction and decimal form.
Thanks for all your help!Code:#include <stdio.h> #include <math.h> typedef struct fraction{ int n; int d; }Frac; void getFrac(Frac a,Frac b,Frac c,Frac e); void addFrac(Frac a,Frac b,Frac c,Frac e); /////////////////////////////////MAIN///////////////////////////////////////////////// void main() { Frac a={0,1},b={0,1},c={0,1},e={0,1},sum={0,1}; getFrac(a,b,c,e); addFrac(a,b,c,e); } /////////////////////////////////FUNCTIONS//////////////////////////////////////////// void getFrac(Frac a,Frac b,Frac c,Frac e) { printf("Enter Fraction\n"); scanf("%d%*c%d",&a.n,&a.d); while(a.d==0){ printf("Enter Fraction Again\n"); scanf("%d%*c%d",&a.n,&a.d); } printf("Enter Fraction\n"); scanf("%d%*c%d",&b.n,&b.d); while(b.d==0){ printf("Enter Fraction Again\n"); scanf("%d%*c%d",&b.n,&b.d); } printf("Enter Fraction\n"); scanf("%d%*c%d",&c.n,&c.d); while(c.d==0){ printf("Enter Fraction Again\n"); scanf("%d%*c%d",&c.n,&c.d); } printf("Enter Fraction\n"); scanf("%d%*c%d",&e.n,&e.d); while(e.d==0){ printf("Enter Fraction Again\n"); scanf("%d%*c%d",&e.n,&e.d); } //printing inputed printf("Input Reads...\n"); printf("\n%d/%d\n",a.n,a.d); printf("%d/%d\n",b.n,b.d); printf("%d/%d\n",c.n,c.d); printf("%d/%d\n\n",e.n,e.d); } void addFrac(Frac a,Frac b,Frac c,Frac e) { int sum1,sum2,sum3,sum4; sum1 = a.d; sum2 = b.d; //a+b a.n *= sum2; //multiplies the numerator of a to the denomiator of b a.d *= sum2; //denom of a to denom of b b.n *= sum1; //first only reversed b.d *= sum1; //second reversed a.n += b.n; //add the numerators now that they're compatible a.d += b.d; //add denominators now that they're compatible //printing printf("\nSum for a and b: %d/%d\n",a.d,b.d); //this works....but the problem is, 1 and 1/2 will come out as 2/1....not 1.5 over 1....spit out decimal next printf("Decimal: %d\n",(a.d/b.d)); //c+d sum3 = c.d; sum4 = e.d; c.n *= sum4; //multiplies the numerator of c to the denomiator of e c.d *= sum4; //denom of c to denom of e e.n *= sum3; //first only reversed e.d *= sum3; //second reversed c.n += e.n; //add the numerators now that they're compatible c.d += e.d; //add denominators now that they're compatible //printing printf("\nSum for c and e: %d/%d\n",c.d,e.d);//this works....but the problem is, 1 and 1/2 will come out as 2/1....not 1.5 over 1....spit out decimal next printf("Decimal: %d\n",(c.d/e.d)); //total printf("Sum Total is: %d\n",(a.d/b.d)+(c.d/e.d)); }
Dave
My original math was incorrect. This is what happens when you talk in the phone and code at the same time. Vola!
Now just take this and use it to add up all your values.Code:struct Frac addTwo( struct Frac a, struct Frac b ) { struct Frac x; a.n *= b.d; b.n *= a.d; x.n = a.n + b.n; x.d = a.d * b.d; return x; }
Quzah.
Hope is the first step on the road to disappointment.
it is not getting numbers in the addFrac().
it gives me 0/1 as answers on everything. y? Heres what i came up with b4 u sent this. It should work, but itz not gettin the values to work with it.
c what u can come up with, i would appreciate it if you could work with the code i have provided. Im jus trying to learn how itz working. ThanksCode:#include <stdio.h> #include <math.h> typedef struct fraction{ int n; int d; }Frac; void getFrac(Frac a,Frac b,Frac c,Frac e); void addFrac(Frac a,Frac b,Frac c,Frac e); /////////////////////////////////MAIN///////////////////////////////////////////////// void main() { Frac a={0,1},b={0,1},c={0,1},e={0,1}; getFrac(a,b,c,e); addFrac(a,b,c,e); } /////////////////////////////////FUNCTIONS//////////////////////////////////////////// void getFrac(Frac a,Frac b,Frac c,Frac e) { printf("Enter Fraction\n"); scanf("%d%*c%d",&a.n,&a.d); while(a.d==0){ printf("Enter Fraction Again\n"); scanf("%d%*c%d",&a.n,&a.d); } printf("Enter Fraction\n"); scanf("%d%*c%d",&b.n,&b.d); while(b.d==0){ printf("Enter Fraction Again\n"); scanf("%d%*c%d",&b.n,&b.d); } printf("Enter Fraction\n"); scanf("%d%*c%d",&c.n,&c.d); while(c.d==0){ printf("Enter Fraction Again\n"); scanf("%d%*c%d",&c.n,&c.d); } printf("Enter Fraction\n"); scanf("%d%*c%d",&e.n,&e.d); while(e.d==0){ printf("Enter Fraction Again\n"); scanf("%d%*c%d",&e.n,&e.d); } //printing inputed printf("Input Reads...\n"); printf("\n%d/%d\n",a.n,a.d); printf("%d/%d\n",b.n,b.d); printf("%d/%d\n",c.n,c.d); printf("%d/%d\n\n",e.n,e.d); } void addFrac(Frac a,Frac b,Frac c,Frac e) { int sum1,sum2,numer,denom,sum3,sum4,numer2,denom2; sum1=a.d*b.n; sum2=a.n*b.d; numer=sum1+sum2; denom=a.d*b.d; printf("\na+b= %d/%d",numer,denom); sum3=c.d*e.n; sum4=c.n*e.d; numer2=sum3+sum4; denom2=c.d*e.d; printf("\nc+e= %d/%d\n",numer2,denom2); }
@SledMan2002
Your latest code doesn't work because you are passing not populating the structs you created in main() with the data you are getting in getFrac(). Look:
You are passing the structs by value, meaning both getFrac and addFrac have their own copies of the fraction structs. Whatever changes they make to them do not affect those you created in main. To fix, you need to pass by reference (ie a pointer).Code:... in main() ... Frac a={0,1},b={0,1},c={0,1},e={0,1}; getFrac(a,b,c,e); addFrac(a,b,c,e);
To see exactly what I mean, add this to the addFrac() function:
>printf("\n%d/%d\n",a.n,a.d);
And don't use void main. Use int main(void) and return 0; at the end.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
i got that 2 work, but i still cant get it to reduce to simplest terms.
Dave
I came up with smoething for him this morning to fix it. And it was due today too so the mods can close this thread.
PHP and XML
Let's talk about SAX