In: Computer Science
OBJECTIVE: In the second assignment, you started coding the NineGaps game by creating the skeleton of the program including the loops, inputs/outputs, and some conditions without going to the details of the game. In this third assignment, we are going to complete the game by coding the details, using what we learned so far, especially working with arrays. YAY! At the end of this assignment, we can actually play with the very first game that we created by ourselves (at least for some of us!). Here You Go…! Complete the NineGaps game: Problem 1: Simulating a two-dimensional array using one-dimensional array. In COMP-1400, we only talk about one-dimensional arrays, and two-dimensional (or more) arrays will be discussed in COMP-1410. However, in NineGaps game, we need to have a table of digits as well as table of operators for the game board. In this problem we would like to simulate a two-dimensional array using a one-dimensional array. For instance, to create a n by m table (a table with n rows and m columns) of digits, we need to create a one-dimensional array with size of n*m. Therefore, row 0 and column 0 of the table (black color) will be represented by position 0 of our array. Or row 1 and column 1of the table (yellow color) will be mapped to the cell#4 in our array. Or row 3 and column 0 of the table (dark blue) is represented by cell#9 in our array. The first step to simulate the process is to find and implement the mapping rule. Assuming we want to add an integer value in a particular row and column of the table, where 0. The language is C.
If you wanted to ask on how to simulate 2d array using 1d array then here is the solution.
If the 2d array is of size n x m, then the 1d array will be of size [nxm] as shown below:
Given: row number(i), column number(j)
Then the position in 1d array of these coordinates (i,j) will be calculated as follows:
where, 1dPosition is position in 1d array, i is row index given, j is column index given, m is totol number of columns in 2d array.
For eg. Let n=3, m=4 then
It can be validated as follows:
as expected.
If you need more than this, you may write in the comment section.