Question
Ragged Arrays The following code defi nes a ragged two-dimensional array. The term ragged indicates that the rows are not all the same length. int[][]
Ragged Arrays The following code defi nes a ragged two-dimensional array. The term ragged indicates that the rows are not all the same length. int[][] triangle new int[5][]; // allocate array of rows for (int i 0; i triangle.length; i) triangle[i] new int[r 1]; a. Draw a picture of the ragged array created by this code fragment. b. Write a method int [][] buildRagged(int n) 292 Part 1 The Fundamental Tools that returns a (possibly) ragged array with n rows. The method reads n 1 lines of data from the console. The fi rst line contains the number of rows of a ragged array. Each succeeding line specifi es one row of a ragged array. The fi rst entry of each line gives the number of items in that row. For example, the input 3 3 1 5 7 6 5 6 8 9 3 2 2 5 8 indicates that there are 3 rows and that the fi rst row of the ragged array has 3 entries (1, 5, and 7), the second row 6 entries (5, 6, 8, 9, 3, and 2), and the third row 2 entries (5 and 8). c. Write a method void printArray(int [][] x) that displays a (possibly) ragged array. d. Test your methods in a program using the main(...) method: public static void main( String[] args) { Scanner input new Scanner(System.in); System.out.print("Enter the data for the array, begin with the number of rows: "); int rows nextInt(); int[][] array build(rows); printArray(array); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started