Denis_Hristov

MaxRowSum

Feb 25th, 2021 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MaxRowSum {
  4.     public static void PrintMatrix(int arr[][]){
  5.         for (int i = 0; i < arr.length; i++) {
  6.             for (int j = 0; j < arr[i].length; j++) {
  7.                 System.out.print(arr[i][j] + " ");
  8.             }
  9.             System.out.println();
  10.         }
  11.     }
  12.     public static void MaxRowSum(int arr[][]){
  13.         int MaxSum = 0;
  14.         int row = 0;
  15.  
  16.         for (int i = 0; i < arr.length; i++) {
  17.             int currentSum = 0;
  18.             int [] currentElement = arr[i];
  19.             for (int j = 0; j < arr[i].length; j++) {
  20.                 currentSum += currentElement[j];
  21.             }
  22.  
  23.             if(MaxSum < currentSum){
  24.                 MaxSum = currentSum;
  25.                 row += i + 1;
  26.             }
  27.             System.out.println(currentSum);
  28.         }
  29.         System.out.println("Row: " + row + " Max sum: " + MaxSum);
  30.     }
  31.     public static void main(String[] args) {
  32.         Scanner scan = new Scanner(System.in);
  33.  
  34.         System.out.println("Enter rows: ");
  35.  
  36.         int r = scan.nextInt();
  37.  
  38.         System.out.println("Enter columns: ");
  39.  
  40.         int c = scan.nextInt();
  41.  
  42.         int [][] arr = new int [r][c];
  43.  
  44.         for (int i = 0; i < arr.length; i++) {
  45.             for (int j = 0; j < arr[i].length; j++) {
  46.                 System.out.print("[" + i + "][" + j + "]: ");
  47.                 arr[i][j] = scan.nextInt();
  48.             }
  49.             System.out.println();
  50.         }
  51.  
  52.  
  53.         PrintMatrix(arr);
  54.         MaxRowSum(arr);
  55.     }
  56. }
  57.  
Add Comment
Please, Sign In to add comment