Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1.  
  2. public class Lab1 {
  3. public static void main(String[] args) {
  4. int[][] bossman = {{6,9,7}, {4,2,5}, {3,2,70}, {47,7,6}};
  5. int row = 3;
  6. System.out.println("The sum of the 2D array is: "+sum(bossman));
  7. System.out.println("The sum of row " +row +" (visually row " +(1+row) +") is: "+rowsum(bossman, row));
  8. System.out.println("The sum2 of the 2D array is: "+sum(bossman));
  9. System.out.println("The largest number is: "+largest(bossman));
  10. System.out.println("The largest number in row " +row +" (visually " +(1+row) +") is: "+largestByRow(bossman, row));
  11. System.out.println("The largest2 number is: "+largest2(bossman));
  12. printRegular(bossman);
  13. printTranspose(bossman);
  14. }
  15. public static int sum(int[][] array) {
  16. int sum = 0;
  17. for (int i = 0; i < array.length; i++) {
  18. for (int j = 0; j < array[0].length; j++) {
  19. sum += array[i][j];
  20. }
  21. }
  22. return sum;
  23. }
  24. public static int rowsum(int[][] array, int row) {
  25. int sum = 0;
  26. for (int i = 0; i < array[row].length; i++) {
  27. sum += array[row][i];
  28. }
  29. return sum;
  30. }
  31. public static int sum2(int[][] array) {
  32. int sum = 0;
  33. for (int i = 0; i < array.length; i++) {
  34. sum += rowsum(array, i);
  35. }
  36. return sum;
  37. }
  38. public static int largest(int[][] array) {
  39. int largest = 0;
  40. for (int i = 0; i < array.length; i++) {
  41. for (int j = 0; j < array[0].length; j++) {
  42. if (array[i][j] > largest) {
  43. largest = array[i][j];
  44. }
  45. }
  46. }
  47. return largest;
  48. }
  49. public static int largestByRow(int[][] array, int row) {
  50. int largest = 0;
  51. for (int i = 0; i < array[row].length; i++) {
  52. if (array[row][i] > largest) {
  53. largest = array[row][i];
  54. }
  55. }
  56. return largest;
  57. }
  58. public static int largest2(int[][] array) {
  59. int largest = 0;
  60. for (int i = 0; i < array.length; i++) {
  61. if (largestByRow(array, i) > largest) {
  62. largest = largestByRow(array, i);
  63. }
  64. }
  65. return largest;
  66. }
  67. public static void printRegular(int[][] array) {
  68. System.out.println();
  69. for (int i = 0; i < array.length; i++) {
  70. for (int j = 0; j < array[i].length; j++) {
  71. System.out.print(array[i][j] +" ");
  72. }
  73. System.out.println();
  74. }
  75. System.out.println();
  76. }
  77. public static void printTranspose(int[][] array) {
  78. for (int j = 0; j < array[0].length; j++) {
  79. for (int i = 0; i < array.length; i++) {
  80. System.out.print(array[i][j] +" ");
  81. }
  82. System.out.println();
  83. }
  84. System.out.println();
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement