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

List of all available Websheets


Viewing cpp/cs102/lab9/gcd-nested 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>Euclid's algorithm for computing the greatest common divisor (GCD) of
   two integers: <tt>x</tt> and <tt>y</tt> can be expressed in pseudocode as:</p>
<pre>
gcd(x,y) =>
    while y ≠ 0
       temp = y; 
       y := x mod y; 
       x := temp; 
    gcd = x;
</pre>
<p>Complete the code below to repeatedly read in two numbers at a time and compute 
   their GCD and output it to the screen, <strong>stopping only when both numbers input are 0</strong>,
   in which case you should stop and not output anything for that pair of integers.</p>
Public permissions remove
Remarks
Comments, history, license, etc.
 
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 <iomanip>
using namespace std;
int main() {
   int x, y, gcd;
   cin >> x >> y;
   
   while( \[ ! (x == 0 && y == 0) ]\ ) {
   \[      
      while(y != 0){
         int t = y;
         y = x % y;
         x = t;
      }
      gcd = x;
    ]\
      cout << gcd << endl;
    \[
      cin >> x >> y;
     ]\
   }
   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 [{}]
 
[
   {"stdin": "9 6 40 28 1071 462 0 0", "args": ""},
   {"stdin": "4 2 8 4 16 8 32 16 64 32 128 64 256 128 512 256 1024 512 0 0", "args": ""},
   {"stdin": "56 40 0 1 0 0 ", "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'.