java multidimensional arrays row sum and column sum
<pre><code>public static void main(String[] args) {
    int rows, cols, j, i;
    Scanner sc = new Scanner(System.in);
    System.out.println("enter no of rows and columns respectively");
    rows = sc.nextInt();
    cols = sc.nextInt();
    int[][] matrix = new int[rows+1][cols+1];
    System.out.println("enter elements");
    for(i = 0; i &lt; rows; i++)
    {
        for(j = 0; j &lt; cols; j++)
        {
            matrix[i][j] = sc.nextInt();
        }
    }
    System.out.println("after sum");
    int grandtotal = 0;
    for(i = 0; i &lt; rows; i++)
    {
        int rowsum = 0;
        int colsum = 0;
        for(j = 0; j &lt; cols; j++)
        {
            rowsum += matrix[i][j];
            colsum += matrix[j][i];
        }
        matrix[i][j] = rowsum;
        grandtotal += rowsum;
        matrix[j][i] = colsum;
    }
    j = cols;
    matrix[i][j] = grandtotal;
</code></pre>

<p>The above code is working fine, but my question is: using this logic, how can I find the <code>grandtotal</code> for a matrix if the number of rows and columns are not the same? And tell me if there are any flaws with my current code.</p>