Java Tutorial › Forums › Chapter Multidimensional Arrays Homework
This topic contains 0 replies, has 1 voice, and was last updated by kobe73er 6 months, 3 weeks ago.

AuthorPosts

February 27, 2019 at 8:59 am #1393
*8.2 (Sum the major diagonal in a matrix) Write a method that sums all the numbers in the major diagonal in an n * n matrix of double values using the following header:
public static double sumMajorDiagonal(double[][] m)
Write a test program that reads a 4by4 matrix and displays the sum of all its elements on the major diagonal. Here is a sample run:
Enter a 4by4 matrix row by row: 1 2 3 4.0 5 6.5 7 8 9 10 11 12 13 14 15 16 Sum of the elements in the major diagonal is 34.5
**8.4 (Compute the weekly hours for each employee) Suppose the weekly hours for all employees are stored in a twodimensional array. Each row records an employee’s sevenday work hours with seven columns. For example, the following array stores the work hours for eight employees. Write a program that displays employees and their total hours in decreasing order of the total hours.
**8.8 (All closest pairs of points) Revise FindNearestPoints.java, to display all closest pairs of points with the same minimum distance. Here is a sample run:
Enter the number of points: 8 Enter 8 points: 0 0 1 1 1 1 2 2 2 2 3 3 4 4 5 5 The closest two points are (0.0, 0.0) and (1.0, 1.0) The closest two points are (0.0, 0.0) and (1.0, 1.0) The closest two points are (1.0, 1.0) and (2.0, 2.0) The closest two points are (1.0, 1.0) and (2.0, 2.0) The closest two points are (2.0, 2.0) and (3.0, 3.0) The closest two points are (3.0, 3.0) and (4.0, 4.0) Their distance is 1.4142135623730951
*8.10 (Largest row and column) Write a program that randomly fills in 0s and 1s into a 4by4 matrix, prints the matrix, and finds the first row and column with the most 1s. Here is a sample run of the program:
0011 0011 1101 1010 The largest row index: 2 The largest column index: 2
**8.12 (Financial application: compute tax) Rewrite ComputeTax.java in chapter 3, using arrays. For each filing status, there are six tax rates. Each rate is applied to a certain amount of taxable income. For example, from the taxable income of $400,000 for a single filer, $8,350 is taxed at 10%, (33,950 – 8,350) at 15%,(82,250 – 33,950) at 25%, (171,550 – 82,550) at 28%, (372,550 – 82,250) at 33%, and (400,000 – 372,950) at 36%. The six rates are the same for all filing statuses, which can be represented in the following array:
double[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};
The brackets for each rate for all the filing statuses can be represented in a twodimensional array as follows:
int[][] brackets = { {8350, 33950, 82250, 171550, 372950}, // Single filer {16700, 67900, 137050, 20885, 372950}, // Married jointly // or qualifying widow(er) {8350, 33950, 68525, 104425, 186475}, // Married separately {11950, 45500, 117450, 190200, 372950} // Head of household };
Suppose the taxable income is $400,000 for single filers. The tax can be computed as follows:
tax = brackets[0][0] * rates[0] + (brackets[0][1] – brackets[0][0]) * rates[1] + (brackets[0][2] – brackets[0][1]) * rates[2] + (brackets[0][3] – brackets[0][2]) * rates[3] + (brackets[0][4] – brackets[0][3]) * rates[4] + (400000 – brackets[0][4]) * rates[5]
**8.14 (Explore matrix) Write a program that prompts the user to enter the length of a square matrix, randomly fills in 0s and 1s into the matrix, prints the matrix, and finds the rows, columns, and diagonals with all 0s or 1s. Here is a sample run of the program:
Enter the size for the matrix: 4 0111 0000 0100 1111 All 0s on row 1 All 1s on row 3 No same numbers on a column No same numbers on the major diagonal No same numbers on the subdiagonal
*8.16 (Sort twodimensional array) Write a method to sort a twodimensional array using the following header:
public static void sort(int m[][])
The method performs a primary sort on rows and a secondary sort on columns.
For example, the following array
{{4, 2},{1, 7},{4, 5},{1, 2},{1, 1},{4, 1}}
will be sorted to
{{1, 1},{1, 2},{1, 7},{4, 1},{4, 2},{4, 5}}.
*8.18 (Shuffle rows) Write a method that shuffles the rows in a twodimensional int array using the following header:
public static void shuffle(int[][] m)
Write a test program that shuffles the following matrix:
int[][] m = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
*8.22 (Even number of 1s) Write a program that generates a 6by6 twodimensional matrix filled with 0s and 1s, displays the matrix, and checks if every row and every column have an even number of 1s.

AuthorPosts
You must be logged in to reply to this topic.