Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

7. The value e can be approximated by the sum 1+X + Write a program that takes a

ID: 3725840 • Letter: 7

Question

7. The value e can be approximated by the sum 1+X + Write a program that takes a value x as input and outputs this sum for n taken to be each of the values 1 to 100. The program should also output e calculated using the predefined function exp. The function exp is a predefined function such that exp(x) returns an approximation to the value e. The function exp is in the library with the header file cmath, Your program should repeat the calculation for new values of x until the user says she or he is through. Use variables of type double to store the factorials or you are likely to produce integer overflow (or arrange your calculation to avoid any direct calculation of factorials). 100 lines of output might not fit comfortably on your screen. Output the 100 output values in a format that will fit all 100 values on the screen. For example, you might output 10 lines with 10 values on each line

Explanation / Answer

program from the given information:

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main() {
// Initialize variables
int n;
double x;
double sum;
double exp;

// Sets the output to display up to 4 decimal places
cout << fixed;
cout << setprecision(4);

// Prompts user for x value
cout << "Please enter an x value: ";
cin >> x;

// Calculates the value of e^x
exp = exp(x);

// Outputs the value of e^x and displays the line of the sums but not the values yet
cout << "e^"<< x << " = " << exp << end1;
cout << "The sums of e^" << x << " from 1 to 100 are: " << endl;

// A for loop that takes the sum and then adds the next sum as (n) increases
for ( n = 1; n =< 100; n++) {
sum += (x * i) / i;
/* This sets the width of a collum to 10 spaces and then outputs the sum for that number of n's
*/
cout << setw(10) << sum;
}
getchar();
getchar();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote