Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unknow logical error

Posted on 2000-03-05
8
Medium Priority
?
167 Views
Last Modified: 2010-04-15
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;
}
0
Comment
Question by:clo1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Expert Comment

by:ntdragon
ID: 2585409
it a joke to give only 5 point for a questions like that but i"ll try to help you anyway

but i have a question first what did you mean about the "int" and the "double"???
0
 
LVL 2

Author Comment

by:clo1
ID: 2585900
I mean the declaration in the structure. It's okay for the output if it is "int", but not for "double". Thanks !
0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2586611
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:daks2003
ID: 2586932
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.
0
 
LVL 18

Accepted Solution

by:
deighton earned 20 total points
ID: 2587121
Voila!

                   #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 ("%lf %lf", &f1.n, &f1.d);

                     printf ("Enter the n & d for f2 ");
                 scanf ("%lf %lf", &f2.n, &f2.d);

                     f3 = add_fract(f1, f2);

                 printf ("f3 = %lf/%lf\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;
                   }
0
 
LVL 18

Expert Comment

by:deighton
ID: 2587648
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'

0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2587828
try to use common,temp1,temp2 as doubles
else they will be rounded
0
 
LVL 2

Author Comment

by:clo1
ID: 2588491
Very good. I can make it work now. Thanks for the remind. Cause I was totally forgot the %lf. Thanks

clo1
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question