Home > front end >  How can I print the sum of the columns as well?
How can I print the sum of the columns as well?

Time:11-28

package Matrix;

import java.util.Scanner;
public class Matrix
{
public static void main(String[] args) {
System.out.print("Enter 2D array size : ");
Scanner sc=new Scanner(System.in);
int rows=sc.nextInt();
int columns=sc.nextInt();

System.out.println("Enter array elements : ");    
         
int twoD[][]=new int[rows][columns];


for(int i=0; i<rows;i  )
{            
for(int j=0; j<columns;j  )
{
twoD[i][j]=sc.nextInt();
}
}
System.out.print("\n  \n");
for(int []x:twoD){
for(int y:x){
System.out.print(y "        ");
}
System.out.println();
}
System.out.println("");
for(int i=0;i<columns;i  ) {
int sum = 0;
for(int j=0;j<rows;j  )
{
sum=sum twoD[i][j];
}
System.out.print(sum " ");
sum=0;
System.out.print("       ");
}

}  

}

how can I print the sum of the columns as well? 
here is what I have so far
My expected output must be :
5     9      8   =   22
3     8      2   =   13
4     3      9   =   16
___________
12   20   19

how can I print the sum of the columns as well? here is what I have so far, And it seems like there is a problem with my code as well. Hope you can help me. I've been working on this for days and I'm new to java.

how can I print the sum of the columns as well? here is what I have so far, And it seems like there is a problem with my code as well. Hope you can help me. I've been working on this for days and I'm new to java.

CodePudding user response:

You already have added up the elements in the row. Now to add up the columns instead of trying to change the row in your for loop change the columns to add up.

Change

for(int i=0;i<columns;i  ) {
int sum = 0;
for(int j=0;j<rows;j  )
{
sum=sum twoD[i][j];
}
System.out.print(sum " ");
sum=0;
System.out.print("       ");

To

for(int i=0;i<columns;i  ) {
    int sum = 0;
    for(int j=0;j<rows;j  )
    {
    sum =twoD[j][i];
    }
    System.out.print(sum " ");
    System.out.print("  ");

Therefore you will have a loop that runs effectively for every column and has the sum at 0 in the beginning and at the accurate sum at the end of going through each column and all that is left is just to print it out.


If you want to have this implemented in JOption than instead of using Scanner in Java, you would have to import the JOption Library with this import statement

import javax.swing.JOptionPane;

Then you would change your input of 2D Array Size to using the method of showInputDialog. Remember that showInputDialog returns a String and so you would need to use parseInt to turn it to a Integer. Here is an example

int rows;
rows = Integer.parseInt(JOptionPane.showInputDialog("Enter 2D Array 
Rows : "));
    
int columns;
columns = Integer.parseInt(JOptionPane.showInputDialog("Enter 2D 
Array Columns : "));  
             
int twoD[][]=new int[rows][columns];

Next change the input of the Array to also use this method. Like this

for(int i=0; i<rows;i  )
    {            
    for(int j=0; j<columns;j  )
{
    twoD[i][j]=Integer.parseInt(JOptionPane.showInputDialog("Enter 
Array Elements for Row " (i 1) " Column " (j 1)));  
}
    }

Resulting in the Final Code being

import javax.swing.JOptionPane;
public class Matrix{
public static void main(String[] args) {
    int rows;
    rows = Integer.parseInt(JOptionPane.showInputDialog("Enter 2D 
Array Rows : "));
    
    int columns;
    columns = Integer.parseInt(JOptionPane.showInputDialog("Enter 2D 
Array Columns : "));  
             
int twoD[][]=new int[rows][columns];


for(int i=0; i<rows;i  )
{            
    for(int j=0; j<columns;j  )
{
    twoD[i][j]=Integer.parseInt(JOptionPane.showInputDialog("Enter 
Array Elements for Row " (i 1) " Column " (j 1)));  
}
    }
    System.out.print("\n  \n");
    for(int []x:twoD){
    int cSum = 0;
    for(int y:x){
    System.out.print(y "        ");
    cSum =y;
    }
    System.out.println(" =  " cSum);
    }
    System.out.println("");
    System.out.println("___________");
    for(int i=0;i<columns;i  ) {
    int sum = 0;
    for(int j=0;j<rows;j  )
    {
    sum =twoD[j][i];
    }
    System.out.print(sum " ");
    System.out.print("  ");
    }
 }
    }
  • Related