An exercise working with a 2-dimensional array. Do the following: 1. Display the 2-D array so...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
An exercise working with a 2-dimensional array. Do the following: 1. Display the 2-D array so the rows are left justified. Use a nested for-loop. • System.out.printf("%4d", array2D[ rowIndex][columnIndex]) 2. Compute and display the total number of array elements. This requires a simple for-loop to sum the row lengths. • System.out.printf("Total number of array elements: %d\n", total); a. After the above logic is correct, add logic to find the longest row and its row index. 3. Sum all the array elements and display the total. This requires a nested for-loop. • System.out.printf("Sum of all the array elements: %d\n", total); 4. Prompt the user for a column index. Display the column, one number per line. Display the word "empty" when the column value is missing. o System.out.print("\nEnter a column to display: "); o System.out.printf("%d\n", array20[rowIndex][columnIndex]); System.out.println("empty"); Get started with the supplied template. import java.util.Scanner; public class Array20 { public static void main(String[] args) { int ir, ic; // loop counters. Scanner console new Scanner(System.in); int [][] array2D={{7, 2, 9, 8), // row e, columns @ to 3 (6, 5, 4, 0, 2), // row 1, columns @ to 5 {(1, 2, 3 }); // row 2, columns 0 to 2 // 1. Display all 2-D array elements as a left justified table // System.out.printf("%4d", array2D [rowIndex][columnIndex]); // 2. Display the number of array elements. Add logic to find the // longest row. /" int total = 0; // running sum total int longRow= 0; "/ // this is the length of the longest row System.out.printf("Total number of array elements: %d\n", total ); "/ // 3. Sum all array elements and display the total: // System.out.printf("Sum of all the array elements: %d\n", total); // 4. Select and display a column given its index: /" System.out.print("\nEnter a column index to display: "); console.nextInt(); ic ic = Math.min( longRow-1, Math.max(ic, 0)); System.out.printf("%d\n", array2D[rowIndex][columnIndex]); System.out.println("empty"); } // end main // end class An exercise working with a 2-dimensional array. Do the following: 1. Display the 2-D array so the rows are left justified. Use a nested for-loop. • System.out.printf("%4d", array2D[ rowIndex][columnIndex]) 2. Compute and display the total number of array elements. This requires a simple for-loop to sum the row lengths. • System.out.printf("Total number of array elements: %d\n", total); a. After the above logic is correct, add logic to find the longest row and its row index. 3. Sum all the array elements and display the total. This requires a nested for-loop. • System.out.printf("Sum of all the array elements: %d\n", total); 4. Prompt the user for a column index. Display the column, one number per line. Display the word "empty" when the column value is missing. o System.out.print("\nEnter a column to display: "); o System.out.printf("%d\n", array20[rowIndex][columnIndex]); System.out.println("empty"); Get started with the supplied template. import java.util.Scanner; public class Array20 { public static void main(String[] args) { int ir, ic; // loop counters. Scanner console new Scanner(System.in); int [][] array2D={{7, 2, 9, 8), // row e, columns @ to 3 (6, 5, 4, 0, 2), // row 1, columns @ to 5 {(1, 2, 3 }); // row 2, columns 0 to 2 // 1. Display all 2-D array elements as a left justified table // System.out.printf("%4d", array2D [rowIndex][columnIndex]); // 2. Display the number of array elements. Add logic to find the // longest row. /" int total = 0; // running sum total int longRow= 0; "/ // this is the length of the longest row System.out.printf("Total number of array elements: %d\n", total ); "/ // 3. Sum all array elements and display the total: // System.out.printf("Sum of all the array elements: %d\n", total); // 4. Select and display a column given its index: /" System.out.print("\nEnter a column index to display: "); console.nextInt(); ic ic = Math.min( longRow-1, Math.max(ic, 0)); System.out.printf("%d\n", array2D[rowIndex][columnIndex]); System.out.println("empty"); } // end main // end class
Expert Answer:
Answer rating: 100% (QA)
import javautilScanner public class Array2D public static void mainString args int ir ic loop counters Scanner console new ScannerSystemin int array2D ... View the full answer
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date:
Students also viewed these programming questions
-
Add row 43 to your matrix from Exercise 9. Calculate each month's total expenses. In Exercise 9 a. Draw a line graph to chart Laura's monthly budgeted expenses. Include horizontal lines to indicate...
-
Find exact values of missing side lengths for 19a-d. a.
-
For the determinant (13.112), add column 1 to column 3 and add column 2 to column 4. Show that the result is equal to In this determinant, subtract column 3 from column 1 and subtract column 4 from...
-
Match each of the following energy band structures with the type of material it represents. Show Work A B C Empty conduction band Band gap Filled valence band Empty conduction band Band gap Filled...
-
Assume that several European countries that use the euro as their currency experience higher inflation than the United States, while two other European countries that use the euro as their currency...
-
Slope problem with friction(5 m/s) (gor *->smaller forces net force acceleration vyda) A block slides down an inclined plane that makes 30 angle with horizontal. If the coefficient of kinetic...
-
State the advantages of chain drive over belt drive. Draw neat sketches of simple and compound gear train.
-
Park City experienced unusual volatility of taxable property values over a particular five-year period. For the first three years of this period, the pre-recession period, average property values in...
-
Evolving standards of decency is used to assess the progress of a maturing society in determining what is cruel and unusual punishment in the context of the Eighth Amendment (Merlo and Benekos,...
-
Annabelle Sizemore has cashed in some treasury bonds and a life insurance policy that her parents had accumulated over the years for her. She has also saved some money in certificates of deposit and...
-
I chose C but it's not correct The net effect on the corporation's books of the declaration and payment of a cash dividend are to decrease liabilities and decrease shareholders' equity. decrease...
-
You receive an e-mail from a consumer-advocacy group asking you to write to a large bookstore. In it, you are to complain about its habit of running local bookstores out of business by selling books...
-
Rohan decides to invest in bonds instead of stocks because he has heard that bonds are a lower-risk investment. He uses the bond's credit rating to make his investment decisions. What is true about...
-
How many shares will need to be issued to raise the desired amount of capital? Zach Kirkhorn, CFO 8,393.49 Incorrect. Hint: how many shares at the current price equal to the amount being raised? How...
-
Are all statistically significant findings meaningful? Provide a hypothetical example of a situation where a finding is statistically significant, but not substantively important. can you provide me...
-
A natural gas combined cycle plant is being designed to meet an annual demand of 1,387,584 MWhrs and a CO2 emission rate of 0.379 mtons/MWhr. The ACF will be 0.576. Calculate the heat rate needed to...
-
Determine whether the sequence converges or diverges. If it converges, find the limit. (If an answer does not exist, enter DNE (6n-1)! (6n + 1)! (6n (6n + 1)! Need Help? lim 316 (88 Submit Answer...
-
What can scientists learn by comparing the fossilized skeletons of extinct primates with the bones of modern species?
-
A language L is complete for a language class C with respect to polynomial-time reductions if L C and L P L for all L C. Show that ; and {0, 1} * are the only languages in P that are not complete...
-
Describe the generalization of the FFT procedure to the case in which n is a power of 3. Give a recurrence for the running time, and solve the recurrence.
-
Two events A and B are conditionally independent, given C, if Give a simple but nontrivial example of two events that are not independent but are conditionally independent given a third event. Pr {A...
-
Describe the relationship between dynamic modeling, behavioral modeling, and structural modeling.
-
Comment on this statement: Dynamic modeling is about interaction.
-
Explain how objects interact by exchanging messages.
Study smarter with the SolutionInn App