Question
After the successful completion of this learning unit, you will be able to: Implement syntactically correct C++ 2-dimensional arrays. Solve a variety of standard problems
After the successful completion of this learning unit, you will be able to:
Implement syntactically correct C++ 2-dimensional arrays.
Solve a variety of standard problems using 2-dimensional arrays.
Phase 1:
Create a class named boolMatrix for storing and processing a two-dimensional array of bool values. The class should be saved in a specification file and an implementation file. A client file is not provided. You'll need to write your own client file to test your class. I'll be creating my own client program to test your class after you submit it.
You'll be defining two constants in your class, but you need to know a little bit about "static" members of classes first. With all of the class members we have used so far, each class object that the client declares has its own copy. With a "static" member, there is just one copy of the member that all objects share. Since constants don't change, it makes sense to just have one copy that all objects share. So, when you declare constants, you must make them static.
In the public area of your class you must define two public constants, NUM_ROWS AND NUM_COLS. I'll give you this code for free:
static const int NUM_ROWS = 20; static const int NUM_COLS = 20;
In your client program, if you need to use these constants, you can't access them in the usual way of using a calling object and a dot operator, because they aren't associated with one particular object. So you'll use the scope resolution operator instead. For example:
if (myRow < boolMatrix::NUM_ROWS)
There are a few places in the descriptions of the member functions below where you are required to use "assert()". Here is a quick tutorial on using assert. First, you need to place #include
assert(row >= 0 && row < NUM_ROWS);
In the functions below that state that assert() is required, the purpose is to ensure that the client has provided legal parameters. This means that it will be the first line of the function.
The class will have exactly one private data member that is an array of bool. The class will have seven public member functions:
default constructor: initialize all of the array elements to false
get: return the current contents of a single array element. Use arguments to indicate the row and column of the array element that should be returned. This function must use assert to exit the program if the row or column is out-of-bounds.
set: set the contents of a single array element. Use arguments to indicate the row and column of the array element that should be set, and the value to which it should be set. This function must use assert to exit the program if the row or column is out-of-bounds.
rowCount: return the number of true values that exist in any given row. This function must use assert to exit the program if the row is out-of-bounds.
colCount: return the number of true values that exist in any given column. This function must use assert to exit the program if the column is out-of-bounds.
totalCount: return the number of true values that exist in the entire array.
neighborCount:Given two arguments that indicate the row and column of a particular cell in the matrix, this function returns the number of neighbors that have the value "true". Most positions in the grid have 8 neighbors like the center square in a tic-tac-toe game. The four corner positions have only 3 neighbors each. The remaining positions around the edge of the grid have 5 neighbors each. This function must use assert to exit the program if the row or column is out-of-bounds.
Additional neighborCount() Requirement: In this function you must use your "get()" function to access the matrix, instead of accessing your 2D array data member directly. So, if your data member is named "m", you'll say "get(row, col)" instead of "m[row][col]". This will be a safer programming practice, since the get() function will do range checking for you (i.e., it will make sure that row and col are not out-of-bounds of the 2D array).
print: display the contents of the array, including the row and column indices. For each element of the array, a true value must be displayed as an asterisk ("*") and a false value must be displayed as a space. This member function is the only one that displays output. It should be formatted as demonstrated here. Make sure that the row and column labels still work correctly if the constants (NUM_ROWS and NUM_COLS) are set to something different, say, 30 instead of 20.
01234567890123456789 0 1 * * 2 * * *** * 3 * **** * 4 * * * ** 5 * * ***** * 6 ** * * *** * 7 * *** * * 8 **** 9 * ****** 10* * * 11 * * ** * 12** **** *** 13 * *** ** * 14 * * * *** 15 ** *** ** ** 16 * * ** 17 18 19
The above picture is just for illustration. It's not necessary to create a grid this complex for purposes of testing your class. Note that if you only have a constructor and a print() function, the picture above will be completely blank except for the row and column labels.
Hints and Additional Requirements:
The neighborCount() function is the most difficult part of this assignment. It's possible to write this function in 8 lines (not counting lines that have only a close curly brace). If you can keep it under about 20 lines that's still pretty good. Here's a hint: write the function so that it only works on cells in the middle of the matrix first. Then you should be able to just add a few lines to handle the boundaries. If you take this approach but then find that you are adding a lot of code to handle corner and edge cells, then you are thinking about it wrong.
Many students struggle with this assignment because you are not writing to produce any particular output. Unlike previous assignments, what you are creating here is a tool for someone else to use, not an actual program that does something. You will create a client program that does whatever you want it to do, but the sole purpose of that client program will be to test your class and make sure it works. You won't submit your client program, and no one else will ever see it.
As always when writing code, write as little code as possible and then test it to see whether what you have so far is working. For this assignment, one idea would be to just write your constructor, and then test it with a client program that does nothing other than declare a boolMatrix object. If that compiles, add a print() function so that you can then check to see if the constructor is initializing the boolMatrix correctly. And so on.
Use const to indicate const functions and unchangeable reference parameters whenever possible.
Here is a small client program that might help to get you started. It is far from exhaustive and you should do further testing before you submit your class.
#include#include "boolmatrix.h" using namespace std; int main() { boolMatrix matrix1; for (int i = 0; i < 50; i++) { matrix1.set(rand() % 20, rand() % 20, true); } matrix1.print(); cout << endl; cout << matrix1.rowCount(10) << endl; cout << matrix1.colCount(10) << endl; cout << matrix1.totalCount() << endl; for (int row = 0; row < boolMatrix::NUM_ROWS; row++) { for (int col = 0; col < boolMatrix::NUM_COLS; col++) { cout << matrix1.neighborCount(row, col); } cout << endl; } }
Do not submit the client program that you created to test your class.
Phase 2:
Your task is to write a program that plays the game of life. This game is a computer simulation of the life and death events in a population of single-cell organisms. Each position in a two-dimensional grid (Petri dish) can support one cell. The program determines whether each position will be able to support life in the next generation.
The grid of cells is represented by a boolMatrix object. In other words, you are writing a program that USES the boolMatrix class. No changes should be made to the code in boolmatrix.h or boolmatrix.cpp! (You'll be adding documentation to those files, of course, but no changes to the code unless you are correcting an error.) The starting grid is generation 0 and is read from a supplied input file. Most positions in the grid have 8 neighbors like the center square in a tic-tac-toe game. The four corner positions have only 3 neighbors each. The remaining positions around the edge of the grid have 5 neighbors each. The rules for the state of each position in the next generation of the grid are as follows:
If the cell is currently empty:
If the cell has exactly three living neighbors, it will come to life in the next generation.
If the cell has any other number of living neighbors, it will remain empty.
If the cell is currently living:
If the cell has one or zero living neighbors, it will die of loneliness in the next generation.
If the cell has four or more living neighbors, it will die of overcrowding in the next generation.
If the cell has two or three neighbors, it will remain living.
All births and deaths occur simultaneously. This point is critical to the correct result.
The following three webpages are also available: the data file, the correct output, and the output including intermediate generations. The last webpage is available to help you debug your code.
The data file supplies the data for generation 0. Each line of data gives a pair of coordinates (row# column#) for a living cell in the original grid. Assume that every number in the text file is between 0 and 19. All other grid positions are empty. Please name your file "life.txt".
After your program has created the boolMatrix object that represents generation 0, your program must allow life to proceed for the number of generations specified by the user. Start with five generations. Your program should then display a grid on the screen to show the final results. Use a star (*) to represent a live cell and a space to represent a dead cell. After the grid, display the following statistical information:
The number of living cells in the entire board.
The number of living cells in row 10.
The number of living cells in column 10.
Additional Requirements
Make sure that your output matches the correct output exactly.
To repeat, this program must use the boolMatrix class to represent the grid, rather than declaring its own arrays. You must not use any arrays in your client program.
In this program, a minimum of four functions is required in addition to main(). In my solution there are five functions besides main(). Remember our discussions about stepwise refinement, value-returning vs. void functions, and value parameters vs. reference parameters.
Be sure to document this program thoroughly, as always.
The output that you provide with your submission should show generation 5 for a 20 by 20 grid. And also generation 8 for a grid of 21 rows and 22 columns. The change in grid size should be accomplished by changing only the two named constants in the specification file.
Hints about reading input files:
Creating a text file:
I suggest that you copy the text from the input file webpage(s) and paste it into a file that you have created using your IDE. The files you create when you type in your IDE are always text files (even if they don't end with .txt). If you're using Windows, you could also use Notepad, but there's no reason to open another application when you are already working in your IDE. I strongly suggest that you don't use TextEdit (Mac) or Word, because these do not store files as text files by default.
As an example, creating a text file in Xcode would be File > New > File > Other > Empty, and then save with the .txt extension.
Where to save your input file so that your IDE can find it:
In Visual C++, right click on the name of the project in the solution explorer. It appears in bold there. Unless you chose a different name for the project, it will be ConsoleApplication1. From the drop-down menu, choose "show folder in windows explorer". The folder that opens up will be the correct place to save your file.
In Xcode, while in the Project Navigator (View menu > Navigators > Show Project Navigator) you'll see a yellow folder called Products. Click the expansion triangle next to it and your project executable should show up. Right click it and choose Show in Finder. The folder it's in pops up and you can add your input file to that folder.
Here's a video to demonstrate the process in Xcode (created by James O'Hara). The video references a specific assignment, but the process is the same.
Extensions are part of the file name. If you want to count the words in a file named "myfile.txt", typing "myfile" or "myfile.cpp" won't work.
For Windows users, before you start this assignment, I suggest that you make sure that Windows is showing you the complete file name of your files, including the extensions. Windows hides this from you by default. In Windows 7 and 8 the procedure is as follows:
Choose "control panel" from the start menu
Choose "Folder Options" from the list of control panel items. In Windows 8 you may have to type "Folder Options" into the control panel search bar.
Choose the "view" tab from the Folder Options window
Find the checkbox that says "Hide extensions for known file types".
Uncheck that checkbox.
I've received two different suggestions from student about how to do this in Windows 10. One student says that the procedure is exactly the same as that outlined above, except that the "Folder Options" window is now the "File Explorer Options" window. The other student says this: go to file explorer and click "View" on the toolbar, then go to "Options" on the furthest right, click on the drop down arrow and choose "Change folder and search options". Click "View" on the top and uncheck "Hide extensions for known file types".
Please let me know if you find that this doesn't work. Also let me know if you think that one of the two Windows 10 options is clearly better.
Submit Your Work
Name your source code files a10_1.cpp, boolMatrix.h and boolMatrix.cpp. Execute the program and copy/paste the output into the bottom of the client file, making it into a comment. Use the Assignment Submission link to submit the three files. When you submit your assignment there will be a text field in which you can add a note to me (called a "comment", but don't confuse it with a C++ comment). In this "comments" section of the submission page let me know whether the program works as required.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started