Consider the case where a programmer needs to keep track of a number of students in a particular course. So a naive approach would be to create a variable for each student. This may look like:
int student1_id = 101;
int student2_id = 203;
int student3_id = 317;
int student4_id = 234;
It becomes more and more difficult to keep track of the number of variables as the number of students increases. Arrays provide a compact representation by holding multiple variables of same data type. The above example now may look like the following when represented by arrays:
int student_id[4] = {101, 203, 317, 234};
Arrays can be declared as any other variables in C++. We have already seen an example of array declaration above. Typically, a declaration looks like one of the following:
data_type array_name[length_of_array];
data_type array_name[length_of_array] = {x1, x2, x3, ..., xn};
The first one justs declares an array; second one declares and individually initializes first n elements of the array. Note that not all elements of the array need to be initialized.
If A is an array, then A[i] accesses the (i+1)th element of A. i is called index. In C++, the index value starts from 0 and goes until length_of_array - 1. We can access and change the values in arrays as follows:
student_id[0] = 5;
int id2 = student_id[1];
Arrays in C++ can have multiple dimensions. For example, we can represent a table of student grades for each homework as a two-dimensional array:
const int number_of_students = 2;
const int number_of_hws = 3;
int grade[number_of_students][number_of_hws] = {{100, 95, 99}, {97,
99, 99}};
This array stores the homework grades for two students. The grade for 2nd homework for the first student can be obtained as follows:
int grade_hw2 = grade[0][1];
Arrays can be passed to functions as any other variables with additional parameter(s) that will specify the size of the array. The following lines describe the declaration of functions and passing of one and two dimensional arrays, we declared earlier:
Function Prototypes:
void modifyStudentID( int student_id[], int num_students );
void modifyGrade( int grade[][number_of_hws], int num_students, int
num_hws );
Function Calls:
modifyStudentID( student_id, 4 );
modifyGrade( grade, number_of_students, number_of_hws );
Remember that arrays are always passed by reference. So any changes done to an array element inside a function will be retained after returning from that function.
Today, we will implement the Game of Life. The Game of Life is played on an infinite 2-dimensional world (so that means our world will be a two dimensional array). Each cell of the world is in one of two possible states: living or dead.
The game is an ecological simulation that proceeds in discrete steps, or generations. A cell can change state from living to dead, or vice versa, based on the states of its neighbors (the 8 adjacent cells). The following rules determine the state of a cell in the next generation.
Your task is to simulate the Game of Life for a 20x20 world. You have been provided with code that declares the array world; initializes it by reading in which cells are occupied from the user and finally prints the present world. You need to fill in the functions nextGeneration and neighborCount that will take the array world as parameter
and will generate the next generation based on the set of rules described above. The second function is required to count the number of living neighbors of a particular cell, which is used to determine if the cell is going to be alive or dead in the next generation. Here is a sample output, which you can use to verify, once you are done.