assuming that x^2 = pow(x,2) then is this the summ you are trying to computer?
F(x,n) = 1 + x + (x^2)/2 + (x^3)/3! + (x^4)/4! + ... + (x^n)/n!
or
G(x,n) = 1 + x + (x*2)/2 + (x*3)3! + (x*4)/4! + ... + (x*n)/n!
The original post makes it look like you are trying for the second, but your code looks like the first.
You ask for two values, but only read in 1 (entry) that causes the "DO you wish to continue..." part to be passed up.
other than that it seems to preform F(x, n) as defined above. You could improve the program with a little math and or precalculation. For example if you were to allocate an array large enough to hold all n numbers, then you can calculate out n! while saving all of the intermediate values in the table.
CODE
#include<iostream>
using namespace std;
double factorialTable(double *table, int n);
int main()
{
double facts[101];
int i;
factorialTable (facts, 100);
for (i=1; i<101;i++)
{
cout << i<< '\t' << facts[i] <<'\n';
}
return 0;
}
double factorialTable(double *table, int n)
{
double retValue;
if (n>=1)
{
if (n==1)
{
table[n] = 1;
retValue = 1;
} else
{
retValue = n * factorialTable(table, n-1);
table[n] = retValue;
}
} else { retValue = 0; }
return retValue;
}
The above code calculates a table containing all of the factorial values between n=1 and n=100. It does so all at once so you don't waste so much time calculateing the save values over and over. (5!=5*4!, 4!=4*3! so if you have a loop from 1 to 5, you calcuate 1! 5 time, 2! 4 times, 3! 3 times, 4! twice, and 5! once... the above program calculates all factorials once).
There is a neat little algorithm for calculating polynomials
say p(n) = a(0) + a(1)* x +a(2)*x^2 +a(3)*x^3 +... + a(n)*x^n
for the computer to calculate x^n it takes n multiplications, then x^(n-1) is n-1 multiplications... so this formula has n + (n-1) + (n-2)... +1 multiplications to preform.
We can speed things along by re-writing this as:
p(n) = a(0) + x*(a(1) + x*(a(2) + x*(a(3) + ... + x*(a(n)) ... )))
This preforms the same calculation in only n multiplications. That is significantly faster!
If you combine the two ideas you can make the calculation much faster... of course you need to really think about what they are doing, else when your teacher says, "oh very good! now why did you do it this way?" you will not look bad when you explain.