By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,086 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,086 IT Pros & Developers. It's quick & easy.

Fraction problem in C

xpun
P: 39
Hi all. im kinda new to c and I could use some help to point me in the right direction. basically I have to perform basic math between fractions and put out a result in fraction form.

right now i am just trying to get the add and multiply functions to work . this is what i have so far..

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <math.h>
  4.  
  5. char symbol;
  6. int num1=0;
  7. int num2=0;
  8. int denum1=0;
  9. int denum2=0;
  10. int ansnum=0;
  11. int denumans=0;
  12. long ans=0;
  13.  
  14. int add(int n1,int d1,int n2,int d2) 
  15. {
  16.  int hold1 = 0;
  17.  int hold2 = 0;
  18.  char  symhold = '/';   
  19.    hold1=((n1 * d2) + (n2 * d1)); 
  20.    hold2=(d1 * d2); 
  21. printf ("%d / %d\n"),hold1,hold2;   
  22. return (hold1);
  23. return (hold2);
  24.  
  25. }
  26. int mult(int n1,int d1,int n2,int d2) 
  27. {
  28.   int hold1 = 0;
  29.   int hold2 = 0;
  30.  char  symhold = '/';   
  31.    hold1=(n1 * n2); 
  32.    hold2=(d1 * d2); 
  33. printf ("%d / %d\n"),hold1,hold2;   
  34. return (hold1);
  35. return (hold2); 
  36. }
  37. int main(void)
  38. {
  39.     char   ra = 'y';
  40.  while (ra != 'n')   
  41.  {   
  42.  
  43.  printf(" Eter the symbol ");
  44.    scanf ("%c",&symbol);  
  45.   printf("Enter The 1st Numerator  ");
  46.     scanf("%d",&num1);
  47.   printf("Enter the 1st Denomanator  ");
  48.     scanf("%d",&denum1);
  49.   printf("Enter the 2nd Numerator  ");
  50.    scanf("%d",&num2); 
  51.   printf("Enter the 2nd Denomanator  ");
  52.     scanf("%d",&denum2); 
  53.  
  54.  
  55.     if (symbol = '+')
  56.     {
  57.     ans= add(num1,denum1,num2,denum2);
  58.  //    printf(" %d\n",ans);
  59.      }
  60.     if (symbol = '*')
  61.     {
  62.     ans= mult(num1,denum1,num2,denum2);
  63.  
  64.   //  printf(" %d\n",ans);
  65.      }
  66. getchar();
  67. printf("would you like to use the fraction operator again? Y or N\n");
  68. ra = getchar();
  69. getchar();
  70. }
  71. }
  72.  
any help is appreciated ...
Apr 3 '07 #1
Share this Question
Share on Google+
16 Replies


Expert 10K+
P: 11,448
You seem to be using C++ and I'd at least expected a Fraction class then.
Besides that, the following:
Expand|Select|Wrap|Line Numbers
  1. return hold1;
  2. return hold2;
... doesn't make any sense: a function can only return once, i.e it can't say:
"oh, yeah, before I forget, take this too".

kind regards,

Jos
Apr 3 '07 #2

xpun
P: 39
You seem to be using C++ and I'd at least expected a Fraction class then.
Besides that, the following:
Expand|Select|Wrap|Line Numbers
  1. return hold1;
  2. return hold2;
... doesn't make any sense: a function can only return once, i.e it can't say:
"oh, yeah, before I forget, take this too".

kind regards,

Jos
hmm ok im writing in c by the way i don't know c++ yet
Apr 3 '07 #3

Expert 10K+
P: 11,448
hmm ok im writing in c by the way i don't know c++ yet
Ok, fair enough, but then you shouldn't include the iostream header file and you
should define a little struct that can represent a fraction. Something like this:
Expand|Select|Wrap|Line Numbers
  1. typedef struct _fraction {
  2.    int n; /* the numerator */
  3.    int d; /* the denominator */
  4. } fraction_t;
kind regards,

Jos
Apr 3 '07 #4

xpun
P: 39
Ok, fair enough, but then you shouldn't include the iostream header file and you
should define a little struct that can represent a fraction. Something like this:
Expand|Select|Wrap|Line Numbers
  1. typedef struct _fraction {
  2.    int n; /* the numerator */
  3.    int d; /* the denominator */
  4. } fraction_t;
kind regards,

Jos

here in lies another problem we haven't been taught structures yet lol I know nothing of them yet . .. although thats not stopping me from figuring them out lol
Apr 3 '07 #5

Expert 10K+
P: 11,448
here in lies another problem we haven't been taught structures yet lol I know nothing of them yet . .. although thats not stopping me from figuring them out lol
Good, that's the spirit; I like your positive attitude. The code snippet from my
previous reply defined an alias 'fraction_t' for two ints 'n' and 'd'. The fraction_t
almost works as if it was a simple thing such as a double or char etc.

This implies that I can say this:
Expand|Select|Wrap|Line Numbers
  1. fraction_t frac;
Now I have a variable frac which has a type fraction_t (a name I came up with)
and this frac variable has two parts which can be used as 'frac.n' and 'frac.d'.

I'll do the multiplication of two fractions for you, you do the rest:
Expand|Select|Wrap|Line Numbers
  1. fraction_t mul(fraction_t a, fraction_t b) {
  2.    fraction_t m; /* m= a*b */
  3.    m.n= a.n*b*n;
  4.    m.d= a.d*b.d;
  5.  
  6.    return m;
  7. }
good luck and

kind regards,

Jos
Apr 3 '07 #6

xpun
P: 39
Good, that's the spirit; I like your positive attitude. The code snippet from my
previous reply defined an alias 'fraction_t' for two ints 'n' and 'd'. The fraction_t
almost works as if it was a simple thing such as a double or char etc.

This implies that I can say this:
Expand|Select|Wrap|Line Numbers
  1. fraction_t frac;
Now I have a variable frac which has a type fraction_t (a name I came up with)
and this frac variable has two parts which can be used as 'frac.n' and 'frac.d'.

I'll do the multiplication of two fractions for you, you do the rest:
Expand|Select|Wrap|Line Numbers
  1. fraction_t mul(fraction_t a, fraction_t b) {
  2.    fraction_t m; /* m= a*b */
  3.    m.n= a.n*b*n;
  4.    m.d= a.d*b.d;
  5.  
  6.    return m;
  7. }
good luck and

kind regards,

Jos
cool thanks alot
Apr 3 '07 #7

sicarie
Expert Mod 2.5K+
P: 4,677
(THREAD HIJACK ALERT!)

Jos-

Ever consider writing one of the tutorials? That was a good, concise explanation of a struct that was pretty easy to understand.
Apr 3 '07 #8

xpun
P: 39
Good, that's the spirit; I like your positive attitude. The code snippet from my
previous reply defined an alias 'fraction_t' for two ints 'n' and 'd'. The fraction_t
almost works as if it was a simple thing such as a double or char etc.

This implies that I can say this:
Expand|Select|Wrap|Line Numbers
  1. fraction_t frac;
Now I have a variable frac which has a type fraction_t (a name I came up with)
and this frac variable has two parts which can be used as 'frac.n' and 'frac.d'.

I'll do the multiplication of two fractions for you, you do the rest:
Expand|Select|Wrap|Line Numbers
  1. fraction_t mul(fraction_t a, fraction_t b) {
  2.    fraction_t m; /* m= a*b */
  3.    m.n= a.n*b*n;
  4.    m.d= a.d*b.d; 
  5.    return m;
  6. }
good luck and

kind regards,

Jos

ok now i keep getting problems saying that printf is undefined
Apr 3 '07 #9

sicarie
Expert Mod 2.5K+
P: 4,677
ok now i keep getting problems saying that printf is undefined
#include "stdio.h"

And I'm having a major brain freeze right now, I can't remember if that's supposed to be in quotes or carats, so try it like that and if that doesn't work, try it with < and > around it.
Apr 3 '07 #10

xpun
P: 39
yeah its <stdio.h>

that fixed the undefined printf but i have some other syntax issues

i am guessing i dont have the structure in the right place.. this code is getting sloppier by the second lol

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. char symbol;
  6. int num1=0;
  7. int num2=0;
  8. int denum1=0;
  9. int denum2=0;
  10. int ansnum=0;
  11. int denumans=0;
  12. long ans=0;
  13.  
  14. int add(int n1,int d1,int n2,int d2) 
  15. {
  16.  int hold1 = 0;
  17.  int hold2 = 0;
  18.  char  symhold = '/';   
  19.    hold1=((n1 * d2) + (n2 * d1)); 
  20.    hold2=(d1 * d2); 
  21. //printf ("%d / %d\n"),hold1,hold2;   
  22. //return (hold1,hold2);
  23. }
  24.  
  25.  
  26. int main(void)
  27. {
  28.   {
  29. typedef struct _fraction {
  30.    int n; /* the numerator */
  31.    int d; /* the denominator */
  32. } fraction_t;  
  33.  char   ra = 'y';
  34.  while (ra != 'n')   
  35.  {   
  36.  
  37.  
  38.  printf(" Eter the symbol ");
  39.    scanf ("%c",&symbol);  
  40.   printf("Enter The 1st Numerator  ");
  41.     scanf("%d",&num1);
  42.   printf("Enter the 1st Denomanator  ");
  43.     scanf("%d",&denum1);
  44.   printf("Enter the 2nd Numerator  ");
  45.    scanf("%d",&num2); 
  46.   printf("Enter the 2nd Denomanator  ");
  47.     scanf("%d",&denum2); 
  48.  
  49.  
  50.     if (symbol = '+')
  51.     {
  52.     ans= add(num1,denum1,num2,denum2);
  53.  
  54.  //    printf(" %d\n",ans);
  55.      }
  56.     if (symbol = '*')
  57.     {
  58.    fraction_t mul(fraction_t a, fraction_t b) {
  59.    fraction_t m; /* m= a*b */
  60.    m.n= a.n*b*n;
  61.    m.d= a.d*b.d;     
  62.  
  63.    return m; 
  64. }
  65.     }
  66.  
  67.   //  printf(" %d\n",ans);
  68.      }
  69. getchar();
  70. printf("would you like to use the fraction operator again? Y or N\n");
  71. ra = getchar();
  72. getchar();
  73. }
  74. }
  75.  
Apr 3 '07 #11

sicarie
Expert Mod 2.5K+
P: 4,677
Yeah, a struct is supposed to be declared outside the main(), where you would declare a function or global, so right above 'add' would be my recommendation.
Apr 3 '07 #12

xpun
P: 39
ok cool but what the heck is this talking about ..
no match for 'operator*' in 'a._fraction::n * b'
Apr 3 '07 #13

sicarie
Expert Mod 2.5K+
P: 4,677
ok cool but what the heck is this talking about ..
no match for 'operator*' in 'a._fraction::n * b'
I'm pretty sure that's saying that the compiler doesn't know how to do this operation:

m.n= a.n*b*n;
m.d= a.d*b.d;

You need to either 'overload' the * operator, or access those variables differently.
Apr 3 '07 #14

Expert 10K+
P: 11,448
i am guessing i dont have the structure in the right place.. this code is getting sloppier by the second lol
Yep, all I can say is that your totally right. You're just trying to copy and paste
code where it doesn't belong at all. And you even dare to feed that mess to
your poor compiler; tsk tsk. ;-)

C compilers use "one pass" parsers which basically means that you need to
declare something 100% before you can use it.

Typedefs (that mumbo jumbo dealing with the fraction_t) need to be defined
at the top level, i.e. outside any function and before any function uses it.

Reorganize your code first before you blindly feed it to your poor compiler.

I did that multiplication function for you; try to get that to work before you start
adding other sophisticated stuff to your program; and be absolutely sure that
you compile C code instead of C++ code.

kind regards,

Jos
Apr 3 '07 #15

Expert 10K+
P: 11,448
(THREAD HIJACK ALERT!)

Jos-

Ever consider writing one of the tutorials? That was a good, concise explanation of a struct that was pretty easy to understand.
Maybe somewhere in the near future I might when I can find the time. I'll do some
of the Java tips of the week first and then we'll see whether people like it or if
they get sick and tired of my babbling ;-)

In the mean time it's difficult enough not to let posters go astray with their own
code.

Thanks for the compliment and

kind regards,

Jos
Apr 3 '07 #16

xpun
P: 39
Maybe somewhere in the near future I might when I can find the time. I'll do some
of the Java tips of the week first and then we'll see whether people like it or if
they get sick and tired of my babbling ;-)

In the mean time it's difficult enough not to let posters go astray with their own
code.

Thanks for the compliment and

kind regards,

Jos
I was talking to my professor and he told me not to screw around with structures just yet so i did this instead . The program works i just need to figure out a way to reduce the resulting fraction.

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. char symbol;
  6. int num1=0;
  7. int num2=0;
  8. int denum1=0;
  9. int denum2=0;
  10. int ansnum=0;
  11. int denumans=0;
  12. int ans=0;
  13.  
  14.  
  15. int add(int n1,int d1,int n2,int d2, int*top,int*bottom) 
  16. {
  17.  int fnum = 0;
  18.  int fden = 0;
  19.  char  symhold = '/'; 
  20.  if (d1==d2)
  21.       { 
  22.       fnum=(n1+n2);
  23.       fden=(d2);
  24.       *top=fnum;
  25.       *bottom=fden;
  26.       }
  27.  else{       
  28.    fnum=((n1 * d2) + (n2 * d1)); 
  29.    fden=(d1 * d2); 
  30.  *top=fnum;
  31.  *bottom=fden;
  32.  }
  33. }
  34.  int sub(int n1,int d1,int n2,int d2, int*top,int*bottom) 
  35.  {
  36.  int fnum = 0;
  37.  int fden = 0;
  38.  char  symhold = '/';
  39.  if (d1==d2)
  40.       { 
  41.       fnum=(n1-n2);
  42.       fden=(d2);
  43.       *top=fnum;
  44.       *bottom=fden;
  45.       }
  46.  else{     
  47.    fnum=((n1 * d2)-(n2 * d1)); 
  48.    fden=(d1*d2) ; 
  49.  *top=fnum;
  50.  *bottom=fden;
  51. }
  52.  }
  53. int mul(int n1,int d1,int n2,int d2, int*top,int*bottom) 
  54. {
  55.  int fnum = 0;
  56.  int fden = 0;
  57.  char  symhold = '/';   
  58.    fnum=(n1 * n2); 
  59.    fden=(d1 * d2); 
  60.  *top=fnum;
  61.  *bottom=fden;
  62. }
  63. int div(int n1,int d1,int n2,int d2, int*top,int*bottom) 
  64. {
  65.  int fnum = 0;
  66.  int fden = 0;
  67.  int nh ;
  68.  int dh ;
  69.  char  symhold = '/';   
  70.    nh=d2;
  71.    dh=n2;
  72.     fnum=(n1 * nh); 
  73.    fden=(d1 * dh); 
  74.  *top=fnum;
  75.  *bottom=fden;
  76. }
  77. int main(void)
  78. {
  79. int numerator;
  80. int denomonator;
  81.  char   ra = 'y';
  82.  while (ra != 'n')   
  83.  {   
  84.  
  85.  
  86.  printf(" Eter the symbol ");
  87.    scanf ("%c",&symbol);  
  88.   printf("Enter The 1st Numerator  ");
  89.     scanf("%d",&num1);
  90.   printf("Enter the 1st Denomanator  ");
  91.     scanf("%d",&denum1);
  92.   printf("Enter the 2nd Numerator  ");
  93.    scanf("%d",&num2); 
  94.   printf("Enter the 2nd Denomanator  ");
  95.     scanf("%d",&denum2); 
  96.  
  97.  
  98.     if (symbol == '+')
  99.     {
  100.     ans = add(num1,denum1,num2,denum2,&numerator,&denomonator);
  101.     printf(" %d/%d\n",numerator,denomonator);
  102.      }
  103.     if (symbol == '-')
  104.     {
  105.     ans = sub(num1,denum1,num2,denum2,&numerator,&denomonator);
  106.     printf(" %d/%d\n",numerator,denomonator);
  107.      }
  108.    if (symbol == '*')
  109.     {
  110.     ans = mul(num1,denum1,num2,denum2,&numerator,&denomonator);
  111.     printf(" %d/%d\n",numerator,denomonator);
  112.     }
  113.     if (symbol == '/')
  114.     {
  115.     ans = div(num1,denum1,num2,denum2,&numerator,&denomonator);
  116.     printf(" %d/%d\n",numerator,denomonator);
  117.     }
  118. getchar();
  119. printf("would you like to use the fraction operator again? Y or N\n");
  120. ra = getchar();
  121. getchar();
  122. }
  123. }
Apr 3 '07 #17

Post your reply

Sign in to post your reply or Sign up for a free account.