title: C Fraction Arithmetic
body: <p>I'm having great difficulty with the following that I need to do for an assign:<br />
  <strong>a.</strong> Declare a data structure that contains a rational number.<br />
  <strong>b.</strong> Write f'xns that will +, -, *, / rational numbers.<br />
  All f'xns have to pass 3 parameters, each pointing to a data structure of the type I declared in part a; 2 of the parameters = operands, 3rd = result.<br />
  <strong>c.</strong> Write a f'xn that takes a pointer to your data structure as a parameter and returns the GCD of the numer. &amp; denom.<br />
  <strong>d.</strong> Use your f'xn from part c to write a f'xn that will reduce a fraction (rational number) to lowest terms. Pass in a pointer to the fraction and have the fraction modified by the f'xn.<br />
  <strong>e.</strong> Write input and output functions so that a user can enter a fraction in the form 1/5 for example.</p>
  
  <p>The user should be allowed to enter any number of problems, and the program should output the answer in lowest terms.</p>
  
  <p><strong>Am I on the right track? I believe I have a-c down, but not d and especially e. Can someone please guide me or help me correct my script?</strong></p>
  
  <pre><code>int GCD (int numer, int denom)
  {
      int result;
      while (denom &gt; 0) {
          result = numer % denom;
          numer = denom;
          denom = result;
      }
      return numer;
  }
  
  int getLCM (int numer, int denom)
  {
      int max;
      max = (numer &gt; denom) ? numer : denom;
      while (1) {
          if (max % numer == 0 &amp;&amp; max % denom == 0)
              break;
          ++max;
      }
      return max;
  }
  
  struct Fraction 
  {
      int numer;
      int denom;
  };
  
  typedef struct 
  {
      int numer;
      int denom; 
  };
  Fraction
  
  Fraction add_fractions (Fraction a, Fraction b)
  {
      Fraction sum;
      sum.numer = (a.numer * b.denom) + (b.numer * a.denom);
      sum.denom = a.denom * b.denom;
      return sum;
  }
  
  Fraction subtract_fractions (Fraction a, Fraction b)
  {
      Fraction sum;
      sum.numer = (a.numer * b.denom) - (b.numer * a.denom);
      sum.denom = a.denom * b.denom;
      return sum;
  }
  
  Fraction multiply_fractions (Fraction a, Fraction b)
  {
      Fraction sum;
      sum.numer = (a.denom * b.denom);
      sum.denom = (a.numer * b.numer);
      return sum;
  }
  
  Fraction divide_fractions (Fraction a, Fraction b)
  {
      Fraction sum;
      sum.numer = (a.denom * b.numer);
      sum.denom = (a.numer * b.denom);
      return sum;
  }
  
  int main ()
  {
      char response;
  
      printf ("FRACTION ARITHMETIC PROGRAM\n");
      printf ("Enter your problem (example 2/3 + 1/5):\n");
      scanf (, &amp;problem);
  
      if (denom == 0 || denom &lt; 0) {
          printf ("Illegal input!!\n");
          printf ("Another problem (y/n)?  ");
          scanf ("%c%*c", &amp;response);
      } else {
          printf ("The answer is  ");
  
          printf ("Another problem (y/n)?  ");
          scanf ("%c%*c", &amp;response);
      }
  
      while ((response == 'y') || (response == 'Y')) {
          printf ("\nWould you like to play again?\n");
          scanf ("%c%*c", &amp;response);
      }
  
      while ((response == 'n') || (response == 'N'))
          printf ("Goodbye and thank you");
  
      return 0;
  }
  </code></pre>
  
  <p><strong>Edit after removing typedef thanks to comment responses:</strong></p>
  
  <pre><code>struct Fraction {
      int numer;
      int denom;
  };
  
  struct Fraction add_fractions (struct Fraction a, struct Fraction b)
  {
      struct Fraction sum;
      sum.numer = (a.numer * b.denom) + (b.numer * a.denom);
      sum.denom = a.denom * b.denom;
      return sum;
  }
  
  struct Fraction subtract_fractions (struct Fraction a, struct Fraction b)
  {
      struct Fraction sum;
      sum.numer = (a.numer * b.denom) - (b.numer * a.denom);
      sum.denom = a.denom * b.denom;
      return sum;
  }
  
  struct Fraction multiply_fractions (struct Fraction a, struct Fraction b)
  {
      struct Fraction sum;
      sum.numer = (a.denom * b.denom);
      sum.denom = (a.numer * b.numer);
      return sum;
  }
  
  struct Fraction divide_fractions (struct Fraction a, struct Fraction b)
  {
      struct Fraction sum;
      sum.numer = (a.denom * b.numer);
      sum.denom = (a.numer * b.denom);
      return sum;
  }
  </code></pre>