Fraction problem in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xpun
    New Member
    • Apr 2007
    • 39

    Fraction problem in C

    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..

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    
    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 ...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You seem to be using C++ and I'd at least expected a Fraction class then.
    Besides that, the following:
    Code:
    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

    Comment

    • xpun
      New Member
      • Apr 2007
      • 39

      #3
      Originally posted by JosAH
      You seem to be using C++ and I'd at least expected a Fraction class then.
      Besides that, the following:
      Code:
      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

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by xpun
        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:
        Code:
        typedef struct _fraction {
           int n; /* the numerator */
           int d; /* the denominator */
        } fraction_t;
        kind regards,

        Jos

        Comment

        • xpun
          New Member
          • Apr 2007
          • 39

          #5
          Originally posted by JosAH
          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:
          Code:
          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

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by xpun
            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:
            Code:
            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:
            Code:
            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

            Comment

            • xpun
              New Member
              • Apr 2007
              • 39

              #7
              Originally posted by JosAH
              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:
              Code:
              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:
              Code:
              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

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                (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.

                Comment

                • xpun
                  New Member
                  • Apr 2007
                  • 39

                  #9
                  Originally posted by JosAH
                  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:
                  Code:
                  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:
                  Code:
                  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

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    Originally posted by xpun
                    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.

                    Comment

                    • xpun
                      New Member
                      • Apr 2007
                      • 39

                      #11
                      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

                      Code:
                      #include <cstdlib>
                      #include <stdio.h>
                      #include <math.h>
                      
                      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();
                      }
                      }

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #12
                        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.

                        Comment

                        • xpun
                          New Member
                          • Apr 2007
                          • 39

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

                          Comment

                          • sicarie
                            Recognized Expert Specialist
                            • Nov 2006
                            • 4677

                            #14
                            Originally posted by xpun
                            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.

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by xpun
                              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

                              Comment

                              Working...