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

List of all available Websheets


Viewing cpp/arrays/examples/arraybad 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
 
An example of unpredictable behaviour.
Public permissions remove
Engine
Template / Reference solution
 
#include <iostream>
#include <string>
using namespace std;
int main() {
\[
   
\show:
  int arr[3];        // declare an array
  int thirteen = 13; // never changes
  cout << "Badness: reading out of bounds!" << endl;
  cout << arr[3] << endl;
  cout << arr[-1] << endl;
  cout << "Worse: writing out of bounds!" << endl;
  arr[-1] = 34;
  cout << "thirteen is now: " << thirteen << endl;
  cout << "Writing out of bounds. C++ prevents nothing :(" << endl;
  arr[3] = 103;
  cout << arr[3] << endl;
  cout << "Let's crash the program!" << endl;
  arr[340912983] = 78676;
  cout << "This line won't execute." << 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 remove
Is example?
i.e., just a demo
remove
Add compiler flag(s)
json list of flags
e.g. ["-Wno-unused-variable"]
 
["-Wno-array-bounds"]
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'.