1

I'm trying to sum of rows of Matrix When I just put elements in 2D array output is right but when I'm trying using Scanner output result is different

SAMPLE INPUT

2
1 2 
3 4

Output:

3
7

Below code result correct

import java.io.*;
import java.util.*;

public class matrix {
    public static void main(String[] args) throws IOException {
       Scanner sc = new Scanner(System.in);

 int a[][] = {       
                        {1, 2,},    
                           
                        { 3, 4}    
                    };    

int rows, cols, sumRow, sumCol;    
            
        //Initialize matrix a  
              
          //Calculates number of rows and columns present in given matrix    
          rows = a.length;    
        cols = a[0].length;    
            
        //Calculates sum of each row of given matrix    
        for(int i = 0; i < rows; i++){    
            sumRow = 0;    
            for(int j = 0; j < cols; j++){    
              sumRow = sumRow + a[i][j];    
            }    
            System.out.println(sumRow);    
        }    
            
        //Calculates sum of each column of given matrix    
        for(int i = 0; i < cols; i++){    
            sumCol = 0;    
            for(int j = 0; j < rows; j++){    
              sumCol = sumCol + a[j][i];    
            }
        }
    }
}

Result incorrect if I'm trying with Scanner

import java.io.*;
import java.util.*;

public class matrix {
    public static void main(String[] args) throws IOException {
       Scanner sc = new Scanner(System.in);

int row = sc.nextInt();

int column = sc.nextInt();

int [][] a = new int[row][column];
for (int i = 0; i < row; i++)
{
    for(int j = 0; j < column; j++) {
       
    a[i][j] = sc.nextInt(); 
    }
}

int rows, cols, sumRow, sumCol;    
            
        //Initialize matrix a  
              
          //Calculates number of rows and columns present in given matrix    
          rows = a.length;    
        cols = a[0].length;    
            
        //Calculates sum of each row of given matrix    
        for(int i = 0; i < rows; i++){    
            sumRow = 0;    
            for(int j = 0; j < cols; j++){    
              sumRow = sumRow + a[i][j];    
            }    
            System.out.println(sumRow);    
        }    
            
        //Calculates sum of each column of given matrix    
        for(int i = 0; i < cols; i++){    
            sumCol = 0;    
            for(int j = 0; j < rows; j++){    
              sumCol = sumCol + a[j][i];    
            }           
        }
    }
} 
1
  • I can't reproduce your problem(link) show some example for which it gives incorrect answer. Your sample input is incorrect, you don't input column size Commented Jul 4, 2020 at 13:28

2 Answers 2

2

With the sample input you've provided, you shouldn't be reading the number of rows and columns, but just a single int for the number of both rows and columns:

int size = sc.nextInt();

int [][] a = new int[size][size];
for (int i = 0; i < size; i++) {
    for(int j = 0; j < size; j++) {       
        a[i][j] = sc.nextInt(); 
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

But how about if we have 2x3 matrix? Thanks by the way your answer works good for same size matrix
@User75432132 the sample input doesn't seem to allow a 2x3 matrix. Can you provide sample input to show how you' d like to receive such a matrix?
0

I do not see any logical problem with your code. However, it is equally important to keep your code clean and user friendly. I recommend you address the following points if you are serious about programming:

  1. The following declaration is unnecessary:

    rows = a.length;
    cols = a[0].length;
    

    You can simply use row and column instead of creating rows and cols for the same thing.

    You should remove all such unnecessary things which create noise in your code.

  2. You have missed printing the sum of each column i.e.

    System.out.println(sumCol);
    
  3. You do not need to declare throws IOException with main for this code.

  4. You should always display a message describing the input; otherwise, the user remains clueless about what he/she is supposed to input.

    Given below is the code incorporating these comments:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            System.out.print("Enter the number of rows: ");
            int row = sc.nextInt();
            System.out.print("Enter the number of columns: ");
            int column = sc.nextInt();
    
            int[][] a = new int[row][column];
            for (int i = 0; i < row; i++) {
                System.out.println("Enter " + column + " integers: ");
                for (int j = 0; j < column; j++) {
                    a[i][j] = sc.nextInt();
                }
            }
    
            int sumRow, sumCol;
    
            // Calculates sum of each row of given matrix
            for (int i = 0; i < row; i++) {
                sumRow = 0;
                for (int j = 0; j < column; j++) {
                    sumRow = sumRow + a[i][j];
                }
                System.out.println("Sum of row " + i + ": " + sumRow);
            }
    
            // Calculates sum of each column of given matrix
            for (int i = 0; i < column; i++) {
                sumCol = 0;
                for (int j = 0; j < row; j++) {
                    sumCol = sumCol + a[j][i];
                }
                System.out.println("Sum of column " + i + ": " + sumCol);
            }
        }
    }
    

    A sample run:

    Enter the number of rows: 3
    Enter the number of columns: 4
    Enter 4 integers: 
    1 9 2 8
    Enter 4 integers: 
    2 8 3 7
    Enter 4 integers: 
    3 7 4 6
    Sum of row 0: 20
    Sum of row 1: 20
    Sum of row 2: 20
    Sum of column 0: 6
    Sum of column 1: 24
    Sum of column 2: 9
    Sum of column 3: 21
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.