clo1
asked on
Unknow logical error
Can somebody help me, I don't know how come I will got 0 for the output, but I am ok if I use "int" for the struct. All I have to use is "double" in this program. Please help. Thanks !!
#include <stdio.h>
typedef struct{
double n;
double d;
}fract;
fract add_fract(fract f1, fract f2);
fract subtract_fract(fract f1, fract f2);
fract multiply_fract(fract f1, fract f2);
fract divide_fract(fract f1, fract f2);
int main(void){
fract f1, f2, f3;
char key;
printf ("Enter the n & d for f1 ");
scanf ("%f %f", &f1.n, &f1.d);
printf ("Enter the n & d for f2 ");
scanf ("%f %f", &f2.n, &f2.d);
f3 = add_fract(f1, f2);
printf ("f3 = %f/%f\n", f3.n, f3.d);
return (0);
}
fract add_fract(fract f1, fract f2){
fract f;
int common, temp1, temp2;
if (f1.d != f2.d){
common = f1.d * f2.d;
temp1 = common / f1.d;
temp2 = common / f2.d;
f1.n = f1.n * temp1;
f2.n = f2.n * temp2;
f.n = f1.n + f2.n;
f.d = common;
}
else{
f.n = f1.n + f2.n;
f.d = f1.d;
}
return f;
}
#include <stdio.h>
typedef struct{
double n;
double d;
}fract;
fract add_fract(fract f1, fract f2);
fract subtract_fract(fract f1, fract f2);
fract multiply_fract(fract f1, fract f2);
fract divide_fract(fract f1, fract f2);
int main(void){
fract f1, f2, f3;
char key;
printf ("Enter the n & d for f1 ");
scanf ("%f %f", &f1.n, &f1.d);
printf ("Enter the n & d for f2 ");
scanf ("%f %f", &f2.n, &f2.d);
f3 = add_fract(f1, f2);
printf ("f3 = %f/%f\n", f3.n, f3.d);
return (0);
}
fract add_fract(fract f1, fract f2){
fract f;
int common, temp1, temp2;
if (f1.d != f2.d){
common = f1.d * f2.d;
temp1 = common / f1.d;
temp2 = common / f2.d;
f1.n = f1.n * temp1;
f2.n = f2.n * temp2;
f.n = f1.n + f2.n;
f.d = common;
}
else{
f.n = f1.n + f2.n;
f.d = f1.d;
}
return f;
}
ASKER
I mean the declaration in the structure. It's okay for the output if it is "int", but not for "double". Thanks !
first the add func is very stupid you
use:
common = f1.d * f2.d;
temp1 = common / f1.d;
temp2 = common / f2.d;
f1.n = f1.n * temp1;
f2.n = f2.n * temp2;
but temp1=f2.d
and temp2=f1.d
so why to use the temp
second i still don't know so first check with the debuger
if you don't lost any data when you entered the add func
i mean check f1.n,f1.d,f2.n,f2.d
when you just enter the func
use:
common = f1.d * f2.d;
temp1 = common / f1.d;
temp2 = common / f2.d;
f1.n = f1.n * temp1;
f2.n = f2.n * temp2;
but temp1=f2.d
and temp2=f1.d
so why to use the temp
second i still don't know so first check with the debuger
if you don't lost any data when you entered the add func
i mean check f1.n,f1.d,f2.n,f2.d
when you just enter the func
common is int
wheras fract members are double.
Did you give that intentionally or is it a mistake. common = f1.d*f2.d wont give proper result to common.
wheras fract members are double.
Did you give that intentionally or is it a mistake. common = f1.d*f2.d wont give proper result to common.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I forgot to mention. I believe the problem was the use of %f in scanf, you need to use %lf in scanf & printf with a double (thats all I changed). %f would be used for a float. That lets you get the data into your program so you can test it out.
A double is a sort of 'long float'
A double is a sort of 'long float'
try to use common,temp1,temp2 as doubles
else they will be rounded
else they will be rounded
ASKER
Very good. I can make it work now. Thanks for the remind. Cause I was totally forgot the %lf. Thanks
clo1
clo1
but i have a question first what did you mean about the "int" and the "double"???