the Assignment was to: Write a program that prompts the user for two inputs in this order, a value for x, and a positive integer for the number of terms to compute. The program should then display the resulting of computing the series.
here is my code: how can i have this code perform the assignment without using a function, and to 10 decimal figures
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int factorial(int); // prototype
int main()
{
double valx, term;
cout<< "Enter a value for x: ";
cin >> valx;
cout<< "Enter a number for terms: ";
cin>> term;
while ( term <=0)
{
cout<< "Input Error \n";
cout<< "Enter a number for terms: ";
cin>> term;
}
OK I don't remember the code for calculating factorial (so I can't check for correctness) but I can advise you not to use double variables for for loops (or generally any loop). Use int instead. Also for term you also need int. Term refers to the number of loops correct?
Are you intending to use the Maclaurin series expansion to estimate sin(x)?
Your equation for sum is incorrect on its face because it omits factorials in the denominator. I believe the equation also requires a loop that increments by 2 rather than 1. (Double check that because I am not a math major.) Two nested loops are not needed, one is sufficient.
You have not written the test for exiting the loop; a sufficiently small remainder term.