Java Program
Write a method called “fillMatrix” that takes two integers rows and cols and returns a 2D array of integers of size rows x cols where the value of element [row][col] is row + col. Write a short program to test your method.
//TestCode.java public class TestCode { public static int[][] fillMatrix(int rows, int cols){ int[][] result = new int[rows][cols]; for(int i = 0;i<rows;i++){ for(int j = 0;j<cols;j++){ result[i][j] = i+j; } } return result; } public static void main(String[] args) { int[][] result = fillMatrix(3,4); for(int i = 0;i<result.length;i++) { for(int j = 0;j<result[i].length;j++){ System.out.print(result[i][j]+" "); } System.out.println(); } } }
0 1 2 3
1 2 3 4
2 3 4 5
Get Answers For Free
Most questions answered within 1 hours.