Array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perryd
    New Member
    • Oct 2006
    • 3

    #1

    Array?

    I'm trying to write a program in c language asking the user for an operation symbol and four intergers that are two fractions. The program needs to compute the specified operation on the two fractions and then print out the answer as a fraction? Do I use an array?

    Perry
  • perryd
    New Member
    • Oct 2006
    • 3

    #2
    This program that i wrote doesn't quite work correctly?Its a program to calclate the exponential of x when the user puts in a value for x.

    #include <stdio.h>
    double expon(double xth, double nth);
    double factorial(doubl e num);
    int main()
    {
    int n;
    int value;
    double x;
    double total;
    total=0.0;
    printf("e**, enter a number for x (0t to exit): ");
    scanf("%f", &x);
    if(x==0) return 0;
    n=0;
    while(n<25)
    {
    total=total+exp on(x, n)/factorial(n);
    n++;
    }
    printf("e** %d is %f \n", (int)x, total);
    }
    return 0;
    }
    double factorial(doubl e num)
    {
    int i;
    double fact;
    fact = 1.0;
    for(i=1; i<=num; i++)
    fact=fact*i;
    return fact;
    }
    double expon(double xth, double nth)
    }
    double ans;
    int exp;
    exp=nth;
    if(nth<0)
    nth=-1*nth;
    ans=1.0;
    while(nth>0)
    {
    ans=ans*xth;
    nth--;
    }
    if(exp<0)
    ans=1.0/ans;
    else ans=ans;
    return ans;
    }

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by perryd
      Do I use an array?
      The choice is entirely yours but personally I would use a structure or class.

      Code:
      class Fraction
      {
      public:
          int Denominator;
          int Numerator;
      
          operator*(Fraction &op);
      }
      etc.

      In the second question any reason why you aren't using

      double exp( double x );

      defined in math.h?

      Comment

      Working...