CA 3 - Grid Generation
in CS 102 on C++, Assignments
Write a C++ program that allows the user to select an option from the following menus and generates an appropriate grid:
----------------------------------
 WELCOME TO GRID GENERATOR: MENU 1
----------------------------------
Select the format of your grid:
1. Create an n by n grid [square]
2. Create a x by y grid [rectangle]
3. Create a random grid size
----------------------------------
 WELCOME TO GRID GENERATOR: MENU 2
----------------------------------
Select the design of your grid:
1. Points  [.]
2. Stars   [*]
3. Lines   [|]
4. Boxes   [_]
5. Custom  [?]
In-Class Progress
#include <iostream>
using namespace std;
int getDimension(){
  int dimension;
  cout << "Enter a dimension: ";
  cin >> dimension;
  return dimension;
}
void printStars(int x, int y){
  for (int row = 0; row < x; row++){
    for (int col = 0; col < y; col++){
      cout << "* ";
    }
    cout << endl;
  }
}
int main() {
  int format;
  cout << "----------------------------------" << endl;
  cout << "    WELCOME TO GRID GENERATOR     " << endl;
  cout << "----------------------------------" << endl;
  cout << "Select a format: " << endl;
  cout << "1. Create an n by n grid [square]" << endl;
  cout << "2. Create an x by y grid [rectangle]" << endl;
  cout << "3. Create a random sized grid" << endl;
  cin >> format;
  int design;
  cout << "Select a design: "<< endl;
  cout << "1. Stars *" << endl;
  cout << "2. Crosses + " << endl;
  cout << "3. Custom ?" << endl;
  cin >> design;
  cout << "You have selected format #" << format << " and design #" << design << ". " << endl;
  int length; 
  int width;
  switch(format) {
    case 1: // square grid
      length = getDimension();
      width = length;
      break;
    case 2:
      length = getDimension();
      width = getDimension();
      break;
    case 3:
      srand(time(0));
      length = rand() % 11 + 1; // # between 1 & 10
      width = rand() % 11 + 1;
      break;
  }
  switch (design){
    case 1: // Stars
      printStars(length, width);
      break;
  }
}
Sample Transcripts
Showing the generation of a rectangular grid (6 rows, 4 columns) made of ‘boxes’.
/*
----------------------------------
    WELCOME TO GRID GENERATOR
----------------------------------
Select the format of your grid:
1. Create an n by n grid [square]
2. Create a x by y grid [rectangle]
3. Create a random grid size
2
----------------------------------
    WELCOME TO GRID GENERATOR
----------------------------------
Select the design of your grid:
1. Points  [.]
2. Stars   [*]
3. Lines   [|]
4. Boxes   [_]
5. Custom  [?]
4
Enter a dimension: 6
Enter a dimension: 4
----------------------------------
       BOXES GRID (6 x 4)
----------------------------------
[__][__][__][__]
[__][__][__][__]
[__][__][__][__]
[__][__][__][__]
[__][__][__][__]
[__][__][__][__]
*/
Showing the generation of a square grid (10 rows, 10 columns) made of ‘points’.
/*
----------------------------------
    WELCOME TO GRID GENERATOR
----------------------------------
Select the format of your grid:
1. Create an n by n grid [square]
2. Create a x by y grid [rectangle]
3. Create a random grid size
1
----------------------------------
    WELCOME TO GRID GENERATOR
----------------------------------
Select the design of your grid:
1. Points  [.]
2. Stars   [*]
3. Lines   [|]
4. Boxes   [_]
5. Custom  [?]
1
Enter a dimension: 10
----------------------------------
       POINTS GRID (10 x 10)
----------------------------------
 .  .  .  .  .  .  .  .  .  .
 .  .  .  .  .  .  .  .  .  .
 .  .  .  .  .  .  .  .  .  .
 .  .  .  .  .  .  .  .  .  .
 .  .  .  .  .  .  .  .  .  .
 .  .  .  .  .  .  .  .  .  .
 .  .  .  .  .  .  .  .  .  .
 .  .  .  .  .  .  .  .  .  .
 .  .  .  .  .  .  .  .  .  .
 .  .  .  .  .  .  .  .  .  .
*/
Showing the generation of a grid of randomized dimensions, made using a custom symbol.
/*
----------------------------------
    WELCOME TO GRID GENERATOR
----------------------------------
Select the format of your grid:
1. Create an n by n grid [square]
2. Create a x by y grid [rectangle]
3. Create a random grid size
3
----------------------------------
    WELCOME TO GRID GENERATOR
----------------------------------
Select the design of your grid:
1. Points  [.]
2. Stars   [*]
3. Lines   [|]
4. Boxes   [_]
5. Custom  [?]
5
----------------------------------
       CUSTOM GRID (5 x 8)
----------------------------------
Enter a symbol to denote a cell: R
 R  R  R  R  R  R  R  R
 R  R  R  R  R  R  R  R
 R  R  R  R  R  R  R  R
 R  R  R  R  R  R  R  R
 R  R  R  R  R  R  R  R
 */
Things to Consider
- Your program should follow the “pretty code” guidelines
- How can you test that your program will work for most (or perhaps all) cases?
- Which cases are the exceptions?