In the last post arrays were discussed and implemented. The next step to arrays is multidimensional arrays. Multidimentsional arrays can be as large as needed but they do use up more and more memory depending on the number of dimensions. Declaring multidimensional arrays is similar to what was previously discussed. A bidimensional array is declared as follows: int newArray [2] [4]; In order to reference a location, specify the name of the array and the locations as follows: newArray [1] [2]; . This may be hard to visualize but what this means is [row] [column]. By calling newArray[1][2]; the user is trying to reference row 1, column 2. Row 1 column 2 yields a value of 4. Remember that declaring a variable such as int newArray [2] [4]; is defining the dimensions of the array. If a reference to the location newArray[2] [4] was made; the program would not work. The correct way is to reference newArray [1] [3] instead.

The following code will fill the table above. This is controlled by two for loops. The first loop controls which row is being filled. The second for loop is computing inside the first loop in order to increment a counter and fill the columns in the row. Example: first row is filed 1,2,3,4; second row is filled 2,4,6,8.
No comments:
Post a Comment