i need help with a homework problem, it's writing a program that approximates the sum of


1 + x/1! + (x^2)/2! + (x^3)/3! + ... + (x^n)/n!

we're supposed to write a program that takes a value x as input and outputs this sum for n taken to be each of the values of 1 to 100. and we're supposed to have the ouput be in a 1o X 10 form, so like 10 values on 10 lines.

i know we are supposed to use a nested loop. a for loop in a a do while loop, but i just can't figure out how to write the code for the summation, i can do it mathematically on paper, but it just doesn't make sense to me in writing it in source code. please if you could help me i would greatly appreciate it.

-M

For information on the help you can expect here with homework problems, have a look here.

If you have a further look through the forums, there was a thread on your exact topic (more precisely, a discussion of how to evaluate that particular summation) a week or so back. However, you need to do some work for yourself, so I'll leave finding that thread as as something for you to do.

this is what i have:

#include <iostream>
#include <cmath>
using namespace std;  

int main ()
{
	double x, n, Real_result, Partial =	1, fact = 1; 
	char ans; 

	cout << "Input a value of 'X' for which you would like to calculate e^x\n"; 
	cin >> x; 
	
		Real_result = exp (x); 

		cout << "The Actual value of e^x is: " << Real_result << endl << endl;


		cout << "Would you like to approximate that same value? \n"; 
		cin >> ans; 
		
	if (ans = 'y' || 'Y')
	{	 

			do 
			{
				for (n = 1; n <= 100; n++)
				{
					Partial = Partial + pow(x, n)/fact; 
					n++; 
					fact++; 
					cout << Partial << endl; 
				}

			} while ( n <= 100 ); 
			
			
	}  
	
	
return 0 ;
}

and i know there is a problem with my for loop, i'm jsut not sure what.

as for finding the the other 'exact' topic. i found it, some time ago and it did me no good which is why i made my own post.

-M

as for finding the the other 'exact' topic. i found it, some time ago and it did me no good which is why i made my own post.

Well, you get nothing further from me. That thread contains all the information you need. It doesn't give a canned answer though: you need to put in a little effort rather than relying on spoon-feeding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.