Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/functions/etox-fact by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
<p>Recall the approximation of e<sup>x</sup> is:</p>
$$\Large e^x = 1 + \frac{x^1}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + ...$$
<p>Convert this program that computes e<sup>x</sup> to now use a function to compute <em>n!</em> as well as <em>x<sup>n</sup></em></p>
<p>You will write the function <tt>int fact(int n)</tt> that computes and returns <em>n!</em></p>
<p>We have started you with the code to compute an approximation of e<sup>x</sup> for a single value of <tt>x</tt>. Modify it to now produce the desired output.</p>
Public permissions remove
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/nestedloops/etox-range (author: redekopp@usc.edu)
Copied from problem cpp/for/sum-mult-2-5 (author: redekopp@usc.edu)
Copied from problem cpp/for/blastoff (author: redekopp@usc.edu)
Copied from problem cpp/while/blastoff (author: redekopp@usc.edu)
Copied from problem cpp/while/goldilocks (author: redekopp@usc.edu)
Copied from problem cpp/while/sum50 (author: redekopp@usc.edu)
Copied from problem cpp/conditionals/rps (author: redekopp@usc.edu)
Copied from problem cpp/conditionals/taxbrackets (author: redekopp@usc.edu)
Copied from problem cpp/conditionals/nestedec (author: redekopp@usc.edu)
Copied from problem cpp/conditionals/extracredit (author: redekopp@usc.edu)
Copied from problem cpp/control/flag (author: daveagp@gmail.com)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <cmath>
using namespace std;
// prototype
int fact(int n);
// definition
int fact(int n)
{
 \[
    int res = 1.0;
    for(int i=2; i <= n; i++){
      res *= i;  
    }
    return res;
    ]\
}
int main()
{
   double x;
   cin >> x;
   double etox = 1;
\[
   for(int i = 1; i < 10; i++){
      etox += pow(x,i)/fact(i);
   }
   \show:
   double x_to_n = 1;
   int fact = 1;
   for(int i = 1; i < 10; i++){
      x_to_n *= x;
      fact *= i;
      etox += x_to_n / fact;
   }
   ]\
   cout << etox << endl;         
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {"stdin": "1.0", "args": ""},
   {"stdin": "0.5", "args": ""},
   {"stdin": "2.2", "args": ""}
]
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.