This book is now obsolete Please use CSAwesome instead.

10.17. Code Practice with 2D Arrays

Replace the “ADD CODE HERE” below with the code to declare and create a 3 by 3 two-dimensional int array named table. The finished code will print the values 0 to 8.

Declaring and creating a 3 by 3 two-dimensional int array only takes one line. To declare the array specify the type of values in the array followed by [][] to indicate a 2D array and then provide a name for the array. To create the array add an = new, followed by the same type as before and [num rows][num cols].

Show Comments

Replace the “ADD CODE HERE” below with the code to declare and initialize a two-dimensional String array called students with the names “Brice, Marvin, Anna” in the first row and “Kamal, Maria, Elissa” in the second. The finished code will print all the names in the array starting with all in the first row followed by all in the second row.

You can declare, create, and initialize a 3 by 3 two-dimensional String array on one line as shown below. Declare the array with type[][] name. Create and initialize an array with two rows and three columns using ={ {item1, item2, item3}, {item4, item5, item6} };. Be sure to separate the items with commas. Also separate the rows with a comma.

Show Comments

Print the values 47, 51, and 20 by accessing them in the the given two-dimensional array.

Use arr[row][col] to get the value at a particular row and column. Remember that the index for the first row is 0 and the index for the first column is also 0.

Show Comments

Print the values 8, 3, 87, and 34 by accessing them from the given two-dimensional array.

Use arr[row][col] to get the value at a particular row and column. Remember that the index for the first row is 0 and the index for the first column is also 0.

Show Comments

Print the number of rows in the given two-dimensional array, or the length of the outer array. Then print the number of columns, or the length of each inner array.

Ex. The array { {“hello”,”there”,”world”},{“how”,”are”,”you”} } should print:

Rows: 2

Columns: 3

To get the number of rows, or the length of the outer array, use arrayName.length . To get the number of columns, or the length of an inner array, use arrayName[0].length.

Show Comments

Loop through the given two-dimensional array, printing out the values in the first row followed by those in the second row and so on.

Create a loop that iterates through all of the outer arrays, or the rows using arrayName.length. Then iterate through the inner arrays, or columns, using arrayName[0].length.

Show Comments

Declare and create a two-dimensional array of strings named colors. Put the colors (“red”, “yellow”, “blue”) in the first row, and the colors (“orange”, “green”, “purple”) in the second row. Then print every value in the array.

Declare and initialize the array in one statement as shown below. Loop through the rows and columns and print each value.

Show Comments

Replace the “ADD CODE HERE” below with the code to count and print the number of 7’s that are in the 2d array. It should print 2.

Use a nested for loop to loop through all the elements in a 2d array. Initialize a count variable to zero before the loop, and every time there is a 7 at the current row and column, increment the count variable by 1.

Show Comments

Replace the “ADD CODE HERE” below with the code to print out the sum of the numbers in the second row of the “table” array. It should print 18.

Use a loop to find the sum of all of the values in the second row. Since we are only looping through one row, we do not need a nested for loop. Initialize the sum to 0 and then loop through each element in the second row and add it to the sum.

Show Comments

Replace the “ADD CODE HERE” below with the code to find the sum of the values on the diagonal from [0][0] to [num rows - 1][num rows - 1] Print the sum. It should print 5.

Create a variable to hold the total and loop through the rows in the array. Each time through the loop add the value at [row][row] to the total. Print the total.

Show Comments

Replace the “ADD CODE HERE” below with the code to declare and create a two-dimensional array of integers numbers with the numbers (1,2,3) in the first row, and the numbers (4,5,6) in the second row. Then loop through the two-dimensional array, printing out the values in the first row followed by those in the second row.

Declare and initialize the array in one statement as shown below. Loop through the rows and columns and print each value.

Show Comments

Replace the “ADD CODE HERE” below with the code to declare and create a two-dimensional array of integers numbers with the numbers (1,2,3) in the first row, the numbers (4,5,6) in the second row, and the numbers (7,8,9) in the third row. Then loop through the two-dimensional array, printing out the values in the first row followed by those in the second row and so on.

Declare and initialize the array in one statement as shown below. Loop through the rows and columns and print each value.

Show Comments

Given the following array, replace the “ADD CODE HERE” below with the code to replace the word “purple” with “yellow”.

Use arr[row][col] = value; to set the value at a particular row and column. Remember the index of the first row is 0 and the index of the first column is also 0.

Show Comments
You have attempted of activities on this page