mercredi 2 août 2017

Design and develop a C++ program for Calculating e(n) when delta <= 0.000001

Design and develop a C++ program for Calculating e(n) when delta <= 0.000001

Your output should be something like this:

N = 2 e(1) = 2 e(2) = 2.5 delta = 0.5

N= 3 e(2) = 2.5 e(3) = 2.565 delta = 0.06

I'm having Problems with the math portion of my code...this is the input that i get

N = 2 e(1) = 2.00 e(2) = 2.50 delta = 0.50
N = 3 e(2) = 2.50 e(3) = 2.67 delta = 0.17

here's my code:

int factorial(int number) 
{
  //if number is <= 1, return 1
  if (number <= 1)
{
    return 1;
}
// otherwise multiply number by factorial(number - 1)
else
{
        //otherwise multiply number by factorial(number - 1) and return it
    int temp = number * factorial(number - 1);

        return temp;
}
 }

  double sumOfFactorials(int n) 
{
   double sum = 0;
//loop from 1..n, adding the factorial division to a sum
for (int i = 1; i <= n; i++)
  {
     double dividedValue = 1.00000 / factorial(i);
    cout << fixed;
    sum = sum + dividedValue;
}
return sum;
 }

   double e(int n) 
{
double value = 1 + sumOfFactorials(n);
return value;
   }

int main()
{


for(int i = 2; i<4; i++){

double en_1 = e(i-1);
double en = e(i);
double delta = en - en_1;

cout << "N = "<<i;
cout << setprecision(2) << " e("<< (i-1) <<") = " << en_1;
cout << setprecision(2) << " e("<< (i) <<") = " << en;

cout << " delta = " << delta;
cout << "\n";
 }   



  }

Aucun commentaire:

Enregistrer un commentaire