Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Lab1 {
- public static void main(String[] args) {
- int[][] bossman = {{6,9,7}, {4,2,5}, {3,2,70}, {47,7,6}};
- int row = 3;
- System.out.println("The sum of the 2D array is: "+sum(bossman));
- System.out.println("The sum of row " +row +" (visually row " +(1+row) +") is: "+rowsum(bossman, row));
- System.out.println("The sum2 of the 2D array is: "+sum(bossman));
- System.out.println("The largest number is: "+largest(bossman));
- System.out.println("The largest number in row " +row +" (visually " +(1+row) +") is: "+largestByRow(bossman, row));
- System.out.println("The largest2 number is: "+largest2(bossman));
- printRegular(bossman);
- printTranspose(bossman);
- }
- public static int sum(int[][] array) {
- int sum = 0;
- for (int i = 0; i < array.length; i++) {
- for (int j = 0; j < array[0].length; j++) {
- sum += array[i][j];
- }
- }
- return sum;
- }
- public static int rowsum(int[][] array, int row) {
- int sum = 0;
- for (int i = 0; i < array[row].length; i++) {
- sum += array[row][i];
- }
- return sum;
- }
- public static int sum2(int[][] array) {
- int sum = 0;
- for (int i = 0; i < array.length; i++) {
- sum += rowsum(array, i);
- }
- return sum;
- }
- public static int largest(int[][] array) {
- int largest = 0;
- for (int i = 0; i < array.length; i++) {
- for (int j = 0; j < array[0].length; j++) {
- if (array[i][j] > largest) {
- largest = array[i][j];
- }
- }
- }
- return largest;
- }
- public static int largestByRow(int[][] array, int row) {
- int largest = 0;
- for (int i = 0; i < array[row].length; i++) {
- if (array[row][i] > largest) {
- largest = array[row][i];
- }
- }
- return largest;
- }
- public static int largest2(int[][] array) {
- int largest = 0;
- for (int i = 0; i < array.length; i++) {
- if (largestByRow(array, i) > largest) {
- largest = largestByRow(array, i);
- }
- }
- return largest;
- }
- public static void printRegular(int[][] array) {
- System.out.println();
- for (int i = 0; i < array.length; i++) {
- for (int j = 0; j < array[i].length; j++) {
- System.out.print(array[i][j] +" ");
- }
- System.out.println();
- }
- System.out.println();
- }
- public static void printTranspose(int[][] array) {
- for (int j = 0; j < array[0].length; j++) {
- for (int i = 0; i < array.length; i++) {
- System.out.print(array[i][j] +" ");
- }
- System.out.println();
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement