Question

In: Computer Science

in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array....

in java

Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array. When the array contains {{10, 15, 30, 40},{15, 5, 8, 2}, {20, 2, 4, 2},{1, 4, 5, 0}}, Your output should look like:

{10 15 30 40} {15 5 8 2}{ 20 2 4 2}{ 1450}

Now, implement another function print2DList(ArrayList<ArrayList<Integer>> list) to print a formatted 2D list.

Solutions

Expert Solution

void print2Darray(int[][] array) {
   for(int i = 0; i < array.length; i++) {
       System.out.print("{");
       for(int j = 0; j < array[0].length; j++) {
           System.out.print(array[i][j]);
           if(j < (array[0].length -1))
               System.out.print(" ");      
       }
       System.out.print("}");  
   }
}

void print2Darray(ArrayList<ArrayList<Integer>> list) {
   for(int i = 0; i < list.size(); i++) {
       System.out.print("{");
       for(int j = 0; j < list.get(0).size(); i++) {
           System.out.print(list.get(i).get(j));
           if(j < (list.get(0).size() -1))
               System.out.print(" ");  
       }
       System.out.print("}");  
   }
}


Related Solutions

Implement a method/function in java that produces running sum runningSum2DArray(int[][] array, int dir) across rows (left...
Implement a method/function in java that produces running sum runningSum2DArray(int[][] array, int dir) across rows (left to right or right to left) or columns (top to bottom or bottom to top) Input to the method: A 4x4 two dimensional int array and an integer (1, 2, 3 or 4 for left, right, up,down respectively). Output: The modified array after producing the running sums according to the direction. For example: If the input to the method is the same as the...
*****IN JAVA**** Implement a theater seating chart  as a two-dimensional array of ticket prices, like this: {10,...
*****IN JAVA**** Implement a theater seating chart  as a two-dimensional array of ticket prices, like this: {10, 10, 10, 10, 10, 10, 10, 10, 10, 10} {10, 10, 10, 10, 10, 10, 10, 10, 10, 10} {10, 10, 10, 10, 10, 10, 10, 10, 10, 10} {10, 10, 20, 20, 20, 20, 20, 20, 10, 10} {10, 10, 20, 20, 20, 20, 20, 20, 10, 10} {10, 10, 20, 20, 20, 20, 20, 20, 10, 10} {20, 20, 30, 30, 40,...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print the array double the value in the array at position start recMethod(array, start+1, end) print the array } else { print "done" } } Assume the method is invoked with array = [3, 4, 6, 7, 8, 10, 4], start = 2, and end = 5 1.How many times is the array printed before the word "done" is printed? 2.How many times is the...
Write a java script function that accepts integer array as input, and display a new array...
Write a java script function that accepts integer array as input, and display a new array by performing fol lowing modifications, • The values in odd index positions must be incremented by 1 • The values in even index positions must be decremented by 1. • Assume the array index starts from 0. Input 1: arr = [1,2,3,4] Output 1: arr = [0,3,2,5 it done html and javascript and plz do it in simple way
Sum all the elements in the two-dimensional array below. Print the // result to the console....
Sum all the elements in the two-dimensional array below. Print the // result to the console. var arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] javascript
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[],...
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search...
Ceate a two dimensional array of int type to hold scores (on a scale of 0...
Ceate a two dimensional array of int type to hold scores (on a scale of 0 to 100) for 10 students (row) for 3 courses (columns) and initialize the array with your preferred data. All the students get 10 bonus points for course #2 (index 1) . Use for a loop to add the bonus points. c programming language
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
Find the runtime of this function, where n is an integer. int function(int n) { int...
Find the runtime of this function, where n is an integer. int function(int n) { int a = 0; for (int i = n; i > 0; i--) { for (int j = 0; j < i; j++) { a = a + i + j; } } return a; } Find the runtime of this function, where m and n are two integers. int f(int m, int n) { if (m==1 || n==1) { return 1; } return f(m,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT