List of all available Websheets
Viewing cpp/control/wallisapprox by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | This program should use a loop to compute the result of the first 10 terms of Wallis' approximation of PI/2. See the comment in the code for the exact terms. |
Public permissions | |
Remarks Comments, history, license, etc. | Originally by Mark Redekopp (redekopp@usc.edu) and Dave Pritchard (daveagp@gmail.com) |
Engine | |
Template / Reference solution |
using namespace std; int main() { double approx; // write code that includes a for loop to compute the first 10 // terms of Wallis' approximation to PI/2 which is: // (2/1)*(2/3)*(4/3)*(4/5)*(6/5)*(6/7)*(8/7)*(8/9)*(10/9)*(10/11) \[ approx = 1.0; for(int i=2; i <= 10; i+=2){ approx = approx * ( static_cast<double>(i) / (i-1) ); approx = approx * ( static_cast<double>(i) / (i+1) ); } ]\ cout << "The approximate value is "; cout << setprecision(3) << approx << endl; return 0; } |
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 [{}] | [ {} ] |
Solution visibility |
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'.