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

List of all available Websheets


Viewing cpp/arrays/pow2 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
 
Write a program that takes an input <tt>n</tt>, and 
fills out the array <tt>pows</tt> with the first 
<tt>n</tt> powers of 2.
<br>For example, <tt>pows[0]</tt> should be 1, <tt>pows[1]</tt> should be 2,
<tt>pows[2]</tt> should be 4, etc.
<br>Hint: you can't use <tt>pow</tt>, but what is the pattern in the numbers?
<br>The printing code is provided for you.
Public permissions remove
Engine
Template / Reference solution
 
#include <iostream>
using namespace std;
int main() {
   int n;
   cin >> n;
   // declare an array called "pows"
   int pows[30]; // works for inputs up to 30
   // fill out the array
\[
   pows[0] = 1;
   for (int i=1; i<n; i++)
      pows[i] = pows[i-1]*2;
]\
   // output
   for (int i=0; i<n; i++)
      cout << "2^" << i << " is " << pows[i] << 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 [{}]
 
[
   {"stdin": "4"},
   {"stdin": "10"},
   {"stdin": "30"}
]
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'.