Question: 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


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 ", 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 ", 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(" Enter a column to display: "); o System.out.printf("%d ", 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 ", total ); "/ // 3. Sum all array elements and display the total: // System.out.printf("Sum of all the array elements: %d ", total); // 4. Select and display a column given its index: /" System.out.print(" Enter a column index to display: "); console.nextInt(); ic ic = Math.min( longRow-1, Math.max(ic, 0)); System.out.printf("%d ", array2D[rowIndex][columnIndex]); System.out.println("empty"); } // end main // end class
Step by Step Solution
3.39 Rating (146 Votes )
There are 3 Steps involved in it
import javautilScanner public class Array2D public static void mainString args int ir ic loop counters Scanner console new ScannerSystemin int array2D ... View full answer
Get step-by-step solutions from verified subject matter experts
