Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 3: Two-dimensional arrays 10 points total; pair-optional This is one of only two problems in this assignment that you may complete with a partner.

Problem 3: Two-dimensional arrays

10 points total; pair-optional

This is one of only two problems in this assignment that you may complete with a partner. See the rules for working with a partner on pair-optional problems for details about how this type of collaboration must be structured.

Overview Two-dimensional arrays in Java are extremely similar to two-dimensional lists in Python.

In Python, a 2-D list is a list of lists:

grid = [ [2, 4], [5, 7], [8, 1] ] 

In the same way, a 2-D array in Java is an array of arrays:

int[][] grid = { {2, 4}, {5, 7}, {8, 1} }; 

(Note: When we declare a variable for a 2-D array, the type of the elements is followed by two pairs of square brackets: int[][])

Here is a comparison of other key operations for 2-D sequences:

operation

in Python

in Java

determining the number of rows

len(grid)

grid.length

determining the number of elements in row r

len(grid[r])

grid[r].length

determining the number of columns in a rectangular grid/matrix (in which all rows have the same length)

len(grid[0])

grid[0].length

accessing the element at row r, column c

grid[r][c]

grid[r][c]

Problems Assume that you have a variable twoD that refers to a two-dimensional array of integers. Here is another example of such an array:

int[][] twoD = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; 

In your answers below, you may assume that the array is rectangular i.e., that all rows have the same number of elements.

  1. (2 points) Write an assignment statement that replaces the value of 7 in the above array with a value of 14.

  2. (3 points) Write a code fragment that prints the values in the rightmost column of the array to which twoD refers. Print each value on its own line. For the array above, the following values would be printed:

    3 6 9 

    Make your code general enough to work on any two-dimensional array named twoD that has at least one value in each row. You may also assume that all rows in twoD have the same number of elements.

    Make your code general enough to work on any two-dimensional array named twoD that has at least one value in each row.

  3. (5 points) Write a code fragment that prints the values in the diagonal of the array to which twoD refers i.e., the values along a diagonal line from the upper-left corner to the lower-right corner. Print each value on its own line. For the array above, the following values would be printed:

    1 5 9 

    Make your code general enough to work on any two-dimensional array in which the dimensions are equal i.e., the number of rows is the same as the number of columns.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions