440,086 Members | 1,450 Online 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

 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 #include  #include  #include    char symbol; int num1=0; int num2=0; int denum1=0; int denum2=0; int ansnum=0; int denumans=0; long ans=0;   int add(int n1,int d1,int n2,int d2)  {  int hold1 = 0;  int hold2 = 0;  char  symhold = '/';       hold1=((n1 * d2) + (n2 * d1));     hold2=(d1 * d2);  printf ("%d / %d\n"),hold1,hold2;    return (hold1); return (hold2);   } int mult(int n1,int d1,int n2,int d2)  {   int hold1 = 0;   int hold2 = 0;  char  symhold = '/';       hold1=(n1 * n2);     hold2=(d1 * d2);  printf ("%d / %d\n"),hold1,hold2;    return (hold1); return (hold2);  } int main(void) {     char   ra = 'y';  while (ra != 'n')     {       printf(" Eter the symbol ");    scanf ("%c",&symbol);     printf("Enter The 1st Numerator  ");     scanf("%d",&num1);   printf("Enter the 1st Denomanator  ");     scanf("%d",&denum1);   printf("Enter the 2nd Numerator  ");    scanf("%d",&num2);    printf("Enter the 2nd Denomanator  ");     scanf("%d",&denum2);          if (symbol = '+')     {     ans= add(num1,denum1,num2,denum2);  //    printf(" %d\n",ans);      }     if (symbol = '*')     {     ans= mult(num1,denum1,num2,denum2);     //  printf(" %d\n",ans);      } getchar(); printf("would you like to use the fraction operator again? Y or N\n"); ra = getchar(); getchar(); } }   any help is appreciated ... Apr 3 '07 #1
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 return hold1; 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

 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 return hold1; 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 typedef struct _fraction {    int n; /* the numerator */    int d; /* the denominator */ } fraction_t; kind regards, Jos Apr 3 '07 #4

 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 typedef struct _fraction {    int n; /* the numerator */    int d; /* the denominator */ } 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 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 fraction_t mul(fraction_t a, fraction_t b) {    fraction_t m; /* m= a*b */    m.n= a.n*b*n;    m.d= a.d*b.d;      return m; } good luck and kind regards, Jos Apr 3 '07 #6

 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 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 fraction_t mul(fraction_t a, fraction_t b) {    fraction_t m; /* m= a*b */    m.n= a.n*b*n;    m.d= a.d*b.d;      return m; } good luck and kind regards, Jos cool thanks alot Apr 3 '07 #7

 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

 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 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 fraction_t mul(fraction_t a, fraction_t b) {    fraction_t m; /* m= a*b */    m.n= a.n*b*n;    m.d= a.d*b.d;     return m; } good luck and kind regards, Jos ok now i keep getting problems saying that printf is undefined Apr 3 '07 #9

 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

 P: 39 yeah its 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 #include  #include  #include    char symbol; int num1=0; int num2=0; int denum1=0; int denum2=0; int ansnum=0; int denumans=0; long ans=0;   int add(int n1,int d1,int n2,int d2)  {  int hold1 = 0;  int hold2 = 0;  char  symhold = '/';       hold1=((n1 * d2) + (n2 * d1));     hold2=(d1 * d2);  //printf ("%d / %d\n"),hold1,hold2;    //return (hold1,hold2); }     int main(void) {   { typedef struct _fraction {    int n; /* the numerator */    int d; /* the denominator */ } fraction_t;    char   ra = 'y';  while (ra != 'n')     {         printf(" Eter the symbol ");    scanf ("%c",&symbol);     printf("Enter The 1st Numerator  ");     scanf("%d",&num1);   printf("Enter the 1st Denomanator  ");     scanf("%d",&denum1);   printf("Enter the 2nd Numerator  ");    scanf("%d",&num2);    printf("Enter the 2nd Denomanator  ");     scanf("%d",&denum2);          if (symbol = '+')     {     ans= add(num1,denum1,num2,denum2);    //    printf(" %d\n",ans);      }     if (symbol = '*')     {    fraction_t mul(fraction_t a, fraction_t b) {    fraction_t m; /* m= a*b */    m.n= a.n*b*n;    m.d= a.d*b.d;           return m;  }     }     //  printf(" %d\n",ans);      } getchar(); printf("would you like to use the fraction operator again? Y or N\n"); ra = getchar(); getchar(); } }   Apr 3 '07 #11

 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

 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

 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

 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 #include  #include  #include    char symbol; int num1=0; int num2=0; int denum1=0; int denum2=0; int ansnum=0; int denumans=0; int ans=0;     int add(int n1,int d1,int n2,int d2, int*top,int*bottom)  {  int fnum = 0;  int fden = 0;  char  symhold = '/';   if (d1==d2)       {        fnum=(n1+n2);       fden=(d2);       *top=fnum;       *bottom=fden;       }  else{           fnum=((n1 * d2) + (n2 * d1));     fden=(d1 * d2);   *top=fnum;  *bottom=fden;  } }  int sub(int n1,int d1,int n2,int d2, int*top,int*bottom)   {  int fnum = 0;  int fden = 0;  char  symhold = '/';  if (d1==d2)       {        fnum=(n1-n2);       fden=(d2);       *top=fnum;       *bottom=fden;       }  else{         fnum=((n1 * d2)-(n2 * d1));     fden=(d1*d2) ;   *top=fnum;  *bottom=fden; }  } int mul(int n1,int d1,int n2,int d2, int*top,int*bottom)  {  int fnum = 0;  int fden = 0;  char  symhold = '/';       fnum=(n1 * n2);     fden=(d1 * d2);   *top=fnum;  *bottom=fden; } int div(int n1,int d1,int n2,int d2, int*top,int*bottom)  {  int fnum = 0;  int fden = 0;  int nh ;  int dh ;  char  symhold = '/';       nh=d2;    dh=n2;     fnum=(n1 * nh);     fden=(d1 * dh);   *top=fnum;  *bottom=fden; } int main(void) { int numerator; int denomonator;  char   ra = 'y';  while (ra != 'n')     {         printf(" Eter the symbol ");    scanf ("%c",&symbol);     printf("Enter The 1st Numerator  ");     scanf("%d",&num1);   printf("Enter the 1st Denomanator  ");     scanf("%d",&denum1);   printf("Enter the 2nd Numerator  ");    scanf("%d",&num2);    printf("Enter the 2nd Denomanator  ");     scanf("%d",&denum2);          if (symbol == '+')     {     ans = add(num1,denum1,num2,denum2,&numerator,&denomonator);     printf(" %d/%d\n",numerator,denomonator);      }     if (symbol == '-')     {     ans = sub(num1,denum1,num2,denum2,&numerator,&denomonator);     printf(" %d/%d\n",numerator,denomonator);      }    if (symbol == '*')     {     ans = mul(num1,denum1,num2,denum2,&numerator,&denomonator);     printf(" %d/%d\n",numerator,denomonator);     }     if (symbol == '/')     {     ans = div(num1,denum1,num2,denum2,&numerator,&denomonator);     printf(" %d/%d\n",numerator,denomonator);     } getchar(); printf("would you like to use the fraction operator again? Y or N\n"); ra = getchar(); getchar(); } } Apr 3 '07 #17 