#include #include using namespace std; // Declare constant DIM = 7 // Inserte prototypes here int…
#include
// Declare constant DIM = 7
// Inserte prototypes here
int main()
{
// Declare a two-dimensional array (using the constant) to hold single characters
// Declare variable bsize to hold a whole number
// Declare variable answer to hold a character
// Use a do-loop to allow the user to try again
// Prompt the user “Please enter the size of the board [1-7]: “
// Get the size and store it in bsize
// Check the input stream. If it is invalid, display error message “Invalid entry” and jump out of the loop
// Check if bsize is within the range. If it is, call the functions
// Call the function to create the checkerboard
// Call the function to print the checkerboard
// Call the function to print the rotated checkerboard
// Otherwise, display error message “Invalid size”
// Prompt the user “Do you want to try again [y-n]?: “
// Get the answer and store in answer
// As long as the answer is ‘y’ (in upper or lower case), keep looping
return 0;
}
// Function definitions
// createBoard(): Receives the two-dimensional array and the size of the board (quantity of rows and columns)
// Using nested loops creates the board according to specifications: starting with ‘A’ inserts
// the letters in the array separated by a blank box
// printBoard1(): Receives the two-dimensional array and the size of the board (quantity of rows and columns)
// Using nested loops prints the board
// printBoard2(): Receives the two-dimensional array and the size of the board (quantity of rows and columns)
// Using nested loops prints the board rotated
Lab 19: Two-Dimensional Arrays
In this lab you will practice working with two-dimensional arrays.
Example Program
This program uses nested loops to process a squared two-dimensional array (has the same number of rows as columns) to represent a grid of two-digit numbers. First the array is filled with numbers generated so that their first and second digits correspond to the row and column respectively where the number is in the grid. The array is then passed to a function that displays as many rows and columns as specified by the user.
Your Program
Write a C++ program that creates a checkerboard like the one shown in Figure 1. It then prints the board as is (Figure 1) and rotated like the one shown in Figure 2.
Your program must fulfill the following requirements:
1) You must use a 7×7 (7 rows by 7 columns) two-dimensional char array to implement the board.
2) You must use a global constant variable (named constant) to specify the dimensions (rows and columns) of the array.
3) You must use nested loops to solve the problem.
4) Function main() must:
a) Create the array.
b) Allow the user to create as many checkerboards as desired.
c) Prompt the user to enter the size of the board (quantity of rows and columns) that he/she wants to
create. The size must be within the range 1-7.
d) Call the three functions specified below to create and print the board.
5) Function createBoard(): receives the array and the size of the checkerboard specified by the user from main() and returns it filled with the checkerboard.
6) Function printBoard1(): receives the array and the size of the checkerboard specified by the user from main() and prints it as is (see Figure 1).
7) Function printBoard2(): receives the array and the size of the checkerboard specified by the user from main() and prints it rotated (see Figure 2).
Example:
AGM DJP
GHI BHN JKL EKQ
MNO CIO PQR FLR
Figure 1 Figure 2
Note: Here the checkerboards are shown side by side to facilitate the comparison. The actual output will show one on top of the other.
The rows and columns are highlighted just to show they have to be rotated.
Open lab19_FML.cpp in your IDE and implement the algorithm provided in the source code as comments
according to the above specifications.
Tips:
1) You can assign to a char variable the ASCII code of the character that you want to store in it and do arithmetic with it. For example:
char x = 97;
cout << x;
++x;
cout << x;
2) Once you figured out how to create the checkerboard, printing it as in Figure 1 is pretty straight forward. To print it like in Figure 2 you need to make minor changes.
Important:
• You must choose the most appropriate data types for your variables, parameters, and functions.
• When you pass an array to a function make sure you pass it in the most appropriate way (const or not const).
• See below sample runs showing the behavior of my program for valid and invalid entries. Run my solution and try it with different values to see what your program must do.