0

i'm supposed to use nested for loops to estimate the value of e and display the estimation through each run. this is what i got so far, but i found out im not supposed to use setprecision so i've done it wrong but don't know what to do to fix it. and then i'm supposed to do the same thing with number e raised to x power

#include <iostream>
	 using std::endl;
     using std::cout;
     using std::cin;
	 using std::fixed;

#include <iomanip>
	 using std::setw;
	 using std::setprecision;
	 using std::setiosflags;
	 using std::ios;


int main ()
{
	//declaration block
	double numE = 1;
	double accuracyOfe;
	double count1;
	double count2 = 1;
	double x;
	double count3 = 1;
	double count4;

	
	//Prompt user for desired accuracy of number e
	cout << "Enter the number of decimal places (From 1 to 15)\n" 
		 << "you would like the number e produced to: ";
    cin >> accuracyOfe;
	cout << endl;
	

	//
	if (accuracyOfe >= 1 && accuracyOfe <= 15)
	{
		accuracyOfe += 3;

		for ( count1 = 1; accuracyOfe >= count1; count1++)
		{		
			count2 *= count1;
			numE += (1/count2);

			cout << "The estimation of e is now: " << numE << endl;


			for(count4 = 0;	accuracyOfe >= count4; count4++)
	

			
		}

		cout << endl << "The number e to the precision of accuracy of " 
			<< (accuracyOfe - 3) << " is: " << setprecision(accuracyOfe - 3)
			<< numE << "." << endl << endl;
	}

	else
	{
		cout << "The number you entered was in the range specified, Sorry." << endl;
	}


	//reset values of variables
	numE = 1;
	count2 =1;

	cout << "Enter the number of decimal places (From 1 to 15)\n" 
		 << "you would like the number e produced to: ";
	cin >> accuracyOfe;
	cout << endl;

	cout << "Enter the value of x: ";
	cin >> x;
	cout << endl;
	

	//

	if (accuracyOfe >= 1 && accuracyOfe <= 15)
	{
		if (x >= -1 && x <= 1)
		{
			accuracyOfe += 3;
			for ( count1 = 1; accuracyOfe >= count1; count1++)
			{
				count2 *= count1;
				count3 *= x;

				numE += (count3/count2);

				cout << "The estimation of e to the x is now: "
					<< setprecision(count1) << numE << endl;
			}

			cout << endl << "The number e to the x to the precision of " 
				<< (accuracyOfe - 3) << " decimal places is: " 
				<< setprecision(accuracyOfe - 3) << numE << "." << endl << endl;
		}
		else
			cout << "The number you entered for x was "
				<< "in the range specified, Sorry." << endl;
		
	}
	else
		cout << "The number you entered was in the range specified, Sorry." << endl;



	return 0;
}
2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by CrazyDieter
0

is it what you needed ?

#include <cstdlib>
 #include <cmath>
 #include <iostream>
 using namespace std;
 
 //Taylor formula applied to exp(x):
 //exp(x) = 1 + x + x^2/2! + x^3/3! + x^4/4! +...
 
 /**computes x!*/
unsigned long fact(unsigned long x)
{
         unsigned long acc=1;
         for(x;x>1;x--)acc*=x;
         return acc;
}
 
double taylor_exp(double x, double precision=0.0001)
{
       double exp=0;
       double termn;
       unsigned int rank=0;
       do
       {
              termn=std::pow(x,rank*1.0)/fact(rank);
              exp+=termn;
              rank++;     
       }while(termn>precision);
       return exp;
} 

int main()
{
    cout<<taylor_exp(1)<<endl;
    fgetc(stdin);
    return EXIT_SUCCESS;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.