7.9 Two-Dimensional Arrays

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

True or False: A two-dimensional array has multiple length fields.

True

ragged array

When the rows of a two-dimensional array are of different lengths

True or False: The first size declarator in the declaration of a two-dimensional array represents the number of columns. The second size declarator represents the number of rows.

False

When initializing a two-dimensional array, you enclose each row's initialization list in _____.

braces

one-dimensional arrays

can hold only one set of data.

Declare and create a two-dimensional array of chars, tictactoe, with 3 rows, each with 3 elements, and initialize it to all space characters.

char[][] tictactoe = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };

Assume that chessboard has been declared to be a two-dimensional array of strings.Write a statement that instantiates an 8x8 array of strings and assign it to chessboard.

chessboard = new String[8][8];

Assume that tictactoe has been declared to be a two-dimensional array of integers.Write a statement that instantiates a 3x3 two-dimensional array of integers and assign it to tictactoe.

tictactoe = new int[3][3];

A 2-dimensional 3x3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is that of the first element in the first row.

tictactoe[0][0]

A two-dimensional 3x3 array of ints, has been created and assigned to tictactoe.Write an expression whose value is that of the last element in the first row.

tictactoe[0][2]

A two-dimensional 3x3 array of ints, has been created and assigned to tictactoe.Write an expression whose value is that of the first element in the last row.

tictactoe[2][0]

two-dimensional array

which are sometimes called 2D arrays, can hold multiple sets of data.

You are given the following array declaration. Write a statement that assigns 18 to the last column of the last row of this array. int[][] numberArray = new int[9][11];

numberArray[8][10] = 18;

A program provides the following declarations below. Write code that sums each column in the array sales and displays the results. import java.util.Random; public class SalesArray { public static void main(String[] args) { double[][] sales = new double [5][4]; double colTotal = 0; //populate the array with values for (int r=0; r<5; r++) { for (int c=0; c<4; c++) { sales[r][c]= Math.random() * 2000 + 800; System.out.println("Value of row " + r + " column " + c + " is "+sales[r][c] ); } } // Write a nested for loop to iterate through the array // Remember to reset the accumulator to running zero. // Write a statement to calculate the total. // Display the column totals in the appropriate loop. } }

import java.util.Random; public class SalesArray { public static void main(String[] args) { double[][] sales = new double [5][4]; double colTotal = 0; //populate the array with values for (int r=0; r<5; r++) { for (int c=0; c<4; c++) { sales[r][c]= Math.random() * 2000 + 800; System.out.println("Value of row " + r + " column " + c + " is "+sales[r][c] ); } } for (int col = 0; col < 4; col++) { // Set the accumulator to 0. colTotal = 0; // Sum a column. for (int row = 0; row < 5; row++) colTotal += sales[row][col]; // Display the column's total. System.out.println("Total of column " + col + " is " + colTotal); } } }

Declare and instantiate a 3x3 two-dimensional array of integers named tictactoe.

int [][] tictactoe = new int[3][3];

Declare a two-dimensional int array named grades. It should have 30 rows and 10 columns.

int[][] grades = new int[30][10];

You are given the following array declaration. Write a statement that assigns 145 to the first column of the first row of this array. int[][] numberArray = new int[9][11];

numberArray[0][0] = 145;

Assume the declarations below. Write a nested for loop to total all the elements in the grades array. Then write a statement that calculates the average of these elements. public class GradesArray { public static void main(String[] args) { final int NUM_ROWS = 30; final int NUM_COLS = 10; int total = 0; double average; int[][] grades = new int[NUM_ROWS][ NUM_COLS]; //Write a nested for loop to total all the elements in the grades array. //Then write a statement that calculates the average of these elements. } }

public class GradesArray { public static void main(String[] args) { final int NUM_ROWS = 30; final int NUM_COLS = 10; int total = 0; double average; int[][] grades = new int[NUM_ROWS][ NUM_COLS]; for (int row = 0; row < grades.length; row++) { for (int col = 0; col < grades[row].length; col++) { total += grades[row][col]; } } average = (double) total / (NUM_ROWS * NUM_COLS); } }

In the code below, a two-dimensional array named values of type double has 10 rows and 20 columns. Fill in the statement that sums all the elements in the array and stores the sum in the variable total. public class ValuesArray { public static void main(String[] args) { double total = 0.0; // accumulator double[][] values = new double[10][ 20]; for (int row = 0; row < 10; row++) { for (int col = 0; col < 20; col++) // Sum the values in the array here. } } }

public class ValuesArray { public static void main(String[] args) { double total = 0.0; // accumulator double[][] values = new double[10][ 20]; for (int row = 0; row < 10; row++) { for (int col = 0; col < 20; col++) total += values[row][col]; } } }

Assume you are given an int variable named sum and a 2-dimensional array of ints that has been created and assigned to a2d. Write some statements that compute the sum of all the elements in the entire 2-dimensional array and assign the value to sum.

sum = 0; for (int i = 0; i < a2d.length; i++) for (int j = 0; j < a2d[i].length; j++) sum += a2d[i][j];


संबंधित स्टडी सेट्स

Basic Research and Applied Research: Definitions and Differences (Chapter 16)

View Set

Quiz: Chapter 18, Drugs Used for Seizure Disorders

View Set