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

List of all available Websheets


Viewing cpp/nestedloops/sphere 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>Write a program that takes an integer input, <tt>r</tt> representing
the radius of a sphere.  Your program should compute and output <tt>z(x,y)</tt>
where <strong>z<sup>2</sup> = r<sup>2</sup> - x<sup>2</sup> - y<sup>2</sup></strong> since a sphere is defined as
   x<sup>2</sup> + y<sup>2</sup> + z<sup>2</sup> = r<sup>2</sup>. </p>
<p>Essentially, output the height (z-value) of a eigth of a sphere such as shown in the following image.</p>
<img src="https://i.imgur.com/wosqVg4.png" height="150">
<p>For x,y values outside the sphere (i.e. the distance x^2 + y^2 > r^2) do not output any value.
<p>Output <strong>z(x,y)</strong> for each value of x,y ranging from (0,0) to (r,r) in a coordinate fashion:</p>
<pre>
z(0,r)   z(1,r)   z(2,r)   ... z(r,r)
z(0,r-1) z(1,r-1) z(2,r-1) ... z(r,r-1)
...
z(0,1)   z(1,1)   z(2,1) ...   z(r,1)
z(0,0)   z(1,0)   z(2,0) ...   z(r,0)
</pre>
<p>Show each value with 2 decimal points and each column with a width of 6.</p> 
Public permissions remove
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/nestedloops/rectangle (author: redekopp@usc.edu)
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>
#include <iomanip>
using namespace std;
int main()
{
   int r;
   cin >> r;
   \[
   for(int y = r; y >= 0; y--){
      for(int x=0; x <= r; x++) {
         int zsq = r*r - x*x  - y*y;
         if(zsq >= 0)
         {
            double z = sqrt(r*r - x*x  - y*y);
            cout << setprecision(2) << fixed << setw(6) << z;
         }
         
      }
      cout << 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": "5", "args": ""},
   {"stdin": "12", "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'.