Write a Java class Diag with a method diagon that takes atwo-dimensional integer
ID: 3618963 • Letter: W
Question
Write a Java class Diag with a method diagon that takes atwo-dimensional integer array array arr; that is, the i-th entry of ti is (i,i)-th entry ofarr if it exists or 0 if not (here i is any integer from i=0 to i=arr.length - 1). An i-th row of arrwill have no (i,i)-th entry if its length is less than i+1. The method should be applied to arrayfff = {{2,3}, {2,3,1}, {3,1,5,6}, {2,3}}. Hint: The resulting array will be ti = {2, 3, 5, 0}.Answer: The class might be as follows: public class Diag{ public static void main(String[] args){ int[][] fff={{2,3}, {2,3,1},{3,1,5,6},{2,3}}; int[] ti=diagon(fff); for (int i=0; i<ti.length; i++) System.out.print(ti[i]+" "); System.out.println(); } public static int[] diagon(int[][] arr){ int[] tti=new int[arr.length]; for (int i=0; i<arr.length;i++) if (arr[i].length < i+1) tti[i]=0; else tti[i]=arr[i][i]; return tti; } }
Write a Java class Diag with a method diagon that takes atwo-dimensional integer array array arr; that is, the i-th entry of ti is (i,i)-th entry ofarr if it exists or 0 if not (here i is any integer from i=0 to i=arr.length - 1). An i-th row of arrwill have no (i,i)-th entry if its length is less than i+1. The method should be applied to arrayfff = {{2,3}, {2,3,1}, {3,1,5,6}, {2,3}}. Hint: The resulting array will be ti = {2, 3, 5, 0}.
Answer: The class might be as follows: public class Diag{ public static void main(String[] args){ int[][] fff={{2,3}, {2,3,1},{3,1,5,6},{2,3}}; int[] ti=diagon(fff); for (int i=0; i<ti.length; i++) System.out.print(ti[i]+" "); System.out.println(); } public static int[] diagon(int[][] arr){ int[] tti=new int[arr.length]; for (int i=0; i<arr.length;i++) if (arr[i].length < i+1) tti[i]=0; else tti[i]=arr[i][i]; return tti; } }
Explanation / Answer
//Hope this will helpyou /* This class find the diagnal elements in 2d array */ public class Diag{ /*Main function*/ public static void main(String[] args){ /* Declaration of 2d array */ int[][] fff={{2,3}, {2,3,1},{3,1,5,6},{2,3}}; /* call diagon and store restult in ti */ int[] ti=diagon(fff); /*Print all element in ti array */ for (int i=0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.