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

List of all available Websheets


Viewing cpp/pointers/int-2D 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
 
Demo program to see how an array of integer pointers (to 1D arrays) creates a 2D array structure
Public permissions remove
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/dynamic_mem/nxmboard (author: redekopp@usc.edu)
Originally by Mark Redekopp (redekopp@usc.edu) and Dave Pritchard (daveagp@gmail.com)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <iomanip>
using namespace std;
// Creates a 2D array structure
int main() {
   int scores1[] = {0,0,0,0,0};
   int scores2[] = {0,0,0,0,0};
   int scores3[] = {0,0,0,0,0};
   int scores4[] = {0,0,0,0,0};
   
   // You could create a 2D array structure with
   // int scores[4][5]; but this array-of-pointers
   // approach will be useful when we add in dynamic memory
   // allocation
\[
   int* scores[4] = {scores1, scores2, scores3, scores4};
   scores[0][3] = 85;
   scores[3][2] = 90; 
\show:
   int* scores[4] = {scores1, scores2, scores3, scores4};
   scores[0][3] = 85;
   scores[3][2] = 90; 
]\
   
   for(int i=0; i < 4; i++){
      for(int j=0; j < 5; j++){
         cout << setw(3) << scores[i][j];
      }
      cout << 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


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'.