19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER SUMMARY<br />

1. A two-dimensional array can be used <strong>to</strong> s<strong>to</strong>re a table.<br />

2. A variable for two-dimensional arrays can be declared using the syntax:<br />

elementType[][] arrayVar.<br />

3. A two-dimensional array can be created using the syntax: new elementType<br />

[ROW_SIZE][COLUMN_SIZE].<br />

4. Each element in a two-dimensional array is represented using the syntax:<br />

arrayVar[rowIndex][columnIndex].<br />

5. You can create and initialize a two-dimensional array using an array initializer with the<br />

syntax: elementType[][] arrayVar = {{row values}, . . . , {row values}}.<br />

6. You can use arrays of arrays <strong>to</strong> form multidimensional arrays. For example, a variable<br />

for three-dimensional arrays can be declared as elementType[][][] arrayVar, and<br />

a three-dimensional array can be created using new elementType[size1][size2]<br />

[size3].<br />

<strong>Programming</strong> Exercises 281<br />

TEST QUESTIONS<br />

Do the test questions for this chapter online at www.cs.armstrong.edu/liang/intro9e/test.html.<br />

PROGRAMMING EXERCISES<br />

*7.1 (Sum elements column by column) Write a method that returns the sum of all the<br />

elements in a specified column in a matrix using the following header:<br />

public static double sumColumn(double[][] m, int columnIndex)<br />

Write a test program that reads a 3-by-4 matrix and displays the sum of each column.<br />

Here is a sample run:<br />

Enter a 3-by-4 matrix row by row:<br />

1.5 2 3 4<br />

5.5 6 7 8<br />

9.5 1 3 1<br />

Sum of the elements at column 0 is 16.5<br />

Sum of the elements at column 1 is 9.0<br />

Sum of the elements at column 2 is 13.0<br />

Sum of the elements at column 3 is 13.0<br />

*7.2 (Sum the major diagonal in a matrix) Write a method that sums all the numbers in<br />

the major diagonal in an n * n matrix of integers using the following header:<br />

public static double sumMajorDiagonal(double[][] m)<br />

Write a test program that reads a 4-by-4 matrix and displays the sum of all its elements<br />

on the major diagonal. Here is a sample run:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!