Problem

As indicated in the text, a two-dimensional array is actually a one-dimensional array of r...

As indicated in the text, a two-dimensional array is actually a one-dimensional array of references to other (subordinate) one-dimensional arrays. In the text examples, all the subordinate arrays had the same length. But these subordinate arrays may have different lengths. The following code fragment creates a two-dimensional array whose five rows have lengths ranging from a minimum length of three elements to a maximum length of seven elements:

public static void main(String[] args){    int rows = 5;    int minCols = 3;    int[][] array2D = new int[rows][0];    for (int j=0; j

Output:

in row 0, number of columns = 3in row 1, number of columns = 4in row 2, number of columns = 5in row 3, number of columns = 6in row 4, number of columns = 7

This pattern in which the number of columns increases linearly with the number of rows is typical of many computer-science algorithms. In this particular case, the total number of elements equals 3 + 4 + 5 + 6 + 7 = 25. Suppose that you wanted a general formula for the total number of elements as a function of arbitrary values for

rows
and
minCols.
Here are two tricks worth remembering:

a) Factor out the minimum number of columns from the rest of the numbers in the series to obtain a series that starts at zero:

3 + 4 + 5 + 6 + 7 = (5 * 3) + (0 + 1 + 2 + 3 + 4)


b) Add the reversed sequence to the sequence that starts at zero to obtain a sequence of identical values:

0

1

2

3

4

+4

+3

+2

+1

+0

4

4

4

4

4 = 5 * 4

Use these two tricks and the fact that

5 = rows
and
3 = minCols
to write a general formula for the total number of array elements as a function of
rows
and
minCols.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 9
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT