Solved

Strange result

Posted on 2000-03-14
5
158 Views
Last Modified: 2010-04-15
Hi, according to the previous question, I have done my program, but there are some strange results comes out. Please help.
Here are the code:
----------------------------------------
#include <stdio.h>
#include <math.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_add, f3_subtract, f3_multiply, f3_divide;
 
  printf ("Numerator for f1: ");
  scanf ("%lf", &f1.n);
  printf ("Denorminator for f1: ");
  scanf ("%lf", &f1.d);

  printf ("Numerator for f2: ");
  scanf ("%lf", &f2.n);
  printf ("Denorminator for f2: ");
  scanf ("%lf", &f2.d);

  f3_add = add_fract(&f1, &f2);
  f3_subtract = subtract_fract(&f1, &f2);
  f3_multiply = multiply_fract(&f1, &f2);
  f3_divide = divide_fract(&f1, &f2);

  printf ("\nAdd = %d/%d\n", (int)f3_add.n, (int)f3_add.d);
  printf ("Subtract = %d/%d\n", (int)f3_subtract.n, (int)f3_subtract.d);
  printf ("Multiply = %d/%d\n", (int)f3_multiply.n, (int)f3_multiply.d);
  printf ("Divide = %d/%d\n", (int)f3_divide.n, (int)f3_divide.d);

  return (0);
}

fract add_fract(fract *f1, fract *f2){
  fract f;
  double common, gcd;

  if (f1->d != f2->d){
    common = f1->d * f2->d;      //Find the common denorminator

      f1->n = f1->n * f2->d;
      f2->n = f2->n * f1->d;

      (int)f.n = f1->n + f2->n;
      (int)f.d = common;

      gcd = euclid(&f.n, &f.d);
  }
  else{
    f.n = f1->n + f2->n;
      f.d = f1->d;

      gcd = euclid(&f.n, &f.d);
  }
  return f;
}

fract subtract_fract(fract *f1, fract *f2){
  fract f;
  double common, gcd;

  if (f1->d != f2->d){
    common = f1->d * f2->d;      //Find the common denorminator

      f1->n = f1->n * f2->d;
      f2->n = f2->n * f1->d;

      f.n = f1->n - f2->n;
      f.d = common;

      gcd = euclid(&f.n, &f.d);
  }
  else{
    f.n = f1->n + f2->n;
      f.d = f1->d;

      gcd = euclid(&f.n, &f.d);
  }
  return f;
}

fract multiply_fract(fract *f1, fract *f2){
  fract f;
  double gcd;

  f.n = f1->n * f2->n;
  f.d = f1->d * f2->d;

  gcd = euclid(&f.n, &f.d);

  return f;
}

fract divide_fract(fract *f1, fract *f2){
  fract f;
  double gcd;

  if (f1->d != 0 || f2->d != 0){
    f.n = f1->n * f2->d;
    f.d = f1->d * f2->n;

      gcd = euclid(&f.n, &f.d);
  }
  else printf ("Cannot divide by zero\n");
  return f;
}
---------------------------------------
The output are as the following if I input 1/3 and 5/6

Add = 7/6
Subtract = -1/2
Multiply = 90/1
Divide = 8/5


THANKS !
0
Comment
Question by:clo1
  • 3
5 Comments
 
LVL 5

Accepted Solution

by:
Wyn earned 30 total points
ID: 2615609
clo1 ,
The output is right by the logic code you give.

The problem is you change the F1 and F2 in Add and Substract function ,if you check it,you will find after substract function.the F1 become 36/3 ,F2 become 45/6.
Thus,you multiply them get:
90/1....

You'd pass F1 and F2 by value.
Change funciton declaration:
For example:
change
fract add_fract(fract *f1, fract *f2);
to
fract add_fract(fract f1, fract f2);

Free to ask.

Regards
W.Yinan
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2615613
FYI :
You'd call it this way:
add_fract(f1, f2);

Follow suit all functions and You will get correct output.

Regards
W.Yinan
0
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2615633
Hi!

I'm sorry but what does the function euclid do?

Andrew
0
 
LVL 2

Author Comment

by:clo1
ID: 2615634
Cool. Thanks for the help, cause I never notice the subtract function, I just thought that is the proble of the multiply and divide procedure. THANKS AGAIN !
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2615646
->I'm sorry but what does the function euclid do?
==============================
Yes ,clo1 ?
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XCode crashes while uploading to app store. 5 257
Console based application in Linux 1 84
How to programmatically differentiate between C and Java 10 188
How to learn Linux? 10 41
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 how to create, access, and change arrays 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.

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now