# how to find sum of each column ?

• 01-14-2014, 06:33 PM
Dlovan
how to find sum of each column ?
hello all programmers

i want to write a program have a array 2d like that :

int array [][] = new int [3][3]

after that the user input value by using scanner then give summation of each column in matrix , i wrote this one

Code:

``` class MatrixSum{ public static void main(String[] args)  { int matrix1[][]= {{7,8,9},{1,5,2}}; int matrix2[][]= {{1,6,4},{2,7,3}}; System.out.println("Number of Row= " + matrix1.length); System.out.println("Number of Column= " + matrix1[1].length); int l = matrix1.length; System.out.println("Matrix 1 : "); for(int i = 0; i < l; i++) { for(int j = 0; j <= l; j++) { System.out.print(" "+ matrix1[i][j]); } System.out.println(); } int m = matrix2.length; System.out.println("Matrix 2 : "); for(int i = 0; i < m; i++) { for(int j = 0; j <= m; j++) { System.out.print(" "+matrix2[i][j]); } System.out.println(); } System.out.println("Addition of both matrix : "); for(int i = 0; i < m; i++) { for(int j = 0; j <= m; j++) { System.out.print(" "+(matrix1[i][j]+matrix2[i][j])); } System.out.println(); } } }```
but i want to change that the user input the value by using scanner , please anyone can help me ?
• 01-14-2014, 06:55 PM
jim829
Re: how to find sum of each column ?
The best way is to first, read the Scanner class. When you think you understand it, try some examples outside of your current program. Then re-read the scanner class to find out why your program didn't work as expected. Then try more examples. This has been my approach and it has served me well. After doing this if you need help, the forum will still be here.

Regards,
Jim
• 01-14-2014, 07:06 PM
Dlovan
Re: how to find sum of each column ?
thanks for you reply @Jim , my friend this homework college i want to do it i read more more but i don't no how this example change to that the user input the value look i do it by using method but i don't no how input method connect with above example

Code:

``` int x[][]=new int[3][3]; void input() { System.out.println("Please Enter number " ); Scanner s1=new Scanner (System.in); for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { x[i][j]=s1.nextInt(); }}} void out() { for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { System.out.print(x[i][j]); } System.out.println(); } System.out.println("----------------"); }```

regards
• 01-14-2014, 07:17 PM
JosAH
Re: how to find sum of each column ?
Quote:

Originally Posted by Dlovan

What does your example code do? Does it run? Does it throw Exceptions as you? Does it produce incorrect results?

kind regards,

Jos
• 01-14-2014, 07:22 PM
jim829
Re: how to find sum of each column ?
You're doing it correctly. Just put the print statement inside the inner most input loop so you can prompt for each number. Then call input(), followed by out().

Regards,
Jim
• 01-14-2014, 07:24 PM
Dlovan
Re: how to find sum of each column ?
Quote:

Originally Posted by JosAH
What does your example code do? Does it run? Does it throw Exceptions as you? Does it produce incorrect results?

kind regards,

Jos

look this is my code by using method you know that

Code:

```int x[][]=new int[3][3]; void input() { System.out.println("Please Enter number " ); Scanner s1=new Scanner (System.in); for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { x[i][j]=s1.nextInt(); }}} void out() { for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { System.out.print(x[i][j]); } System.out.println(); } System.out.println("----------------"); }```
then i want to create method by any name using this function find sum of each column

Code:

``` for(int i = 0; i < m; i++) { for(int j = 0; j <= m; j++) { System.out.print(" "+(matrix1[i][j]+matrix2[i][j]));```
this function i doing it by ready value look

Code:

```class MatrixSum{ public static void main(String[] args)  { int matrix1[][]= {{7,8,9},{1,5,2}}; int matrix2[][]= {{1,6,4},{2,7,3}}; System.out.println("Number of Row= " + matrix1.length); System.out.println("Number of Column= " + matrix1[1].length); int l = matrix1.length; System.out.println("Matrix 1 : "); for(int i = 0; i < l; i++) { for(int j = 0; j <= l; j++) { System.out.print(" "+ matrix1[i][j]); } System.out.println(); } int m = matrix2.length; System.out.println("Matrix 2 : "); for(int i = 0; i < m; i++) { for(int j = 0; j <= m; j++) { System.out.print(" "+matrix2[i][j]); } System.out.println(); } System.out.println("Addition of both matrix : "); for(int i = 0; i < m; i++) { for(int j = 0; j <= m; j++) { System.out.print(" "+(matrix1[i][j]+matrix2[i][j])); } System.out.println(); } } }```

but know i want doing it by Scanner how to do it ?

regards
• 01-14-2014, 08:40 PM
JosAH
Re: how to find sum of each column ?
Scanners can't sum elements of a column, you have to do that yourself, i.e. you have to sum the values array[0][column], array[1][column], array[2][column] ... up to the element in the last row of the matrix; this begs for a simple loop.

kind regards,

Jos
• 01-14-2014, 09:37 PM
Dlovan
Re: how to find sum of each column ?
can you give me a simple i.e ?
• 01-14-2014, 09:47 PM
JosAH
Re: how to find sum of each column ?
Quote:

Originally Posted by Dlovan
can you give me a simple i.e ?

No, you show what you have tried yourself and then we can discuss the problems you have encountered.

kind regards,

Jos
• 01-14-2014, 10:01 PM
Dlovan
Re: how to find sum of each column ?
Quote:

Originally Posted by JosAH
No, you show what you have tried yourself and then we can discuss the problems you have encountered.

kind regards,

Jos

also i do it in #1 , but my professor said you can do it by using Scanner Method not by using input value !
• 01-14-2014, 10:23 PM
JosAH
Re: how to find sum of each column ?
Quote:

Originally Posted by Dlovan
also i do it in #1 , but my professor said you can do it by using Scanner Method not by using input value !

Again, Scanners can't sum matrix columns and a Scanner does take input values so I don't understand your remark; you have to elaborate a bit on your problem and don't shout.

kind regards,

Jos
• 01-14-2014, 10:39 PM
gimbal2
Re: how to find sum of each column ?
Quote:

Originally Posted by Dlovan
also i do it in #1 , but my professor said you can do it by using Scanner Method not by using input value !

I believe you that he said something like that, but that does not guarantee that you understood his intentions.
• 01-14-2014, 10:56 PM
Dlovan
Re: how to find sum of each column ?
Quote:

Originally Posted by gimbal2
I believe you that he said something like that, but that does not guarantee that you understood his intentions.

thanks my friend i don't no !! tomorrow i take to professor but i need give another answer :(whew): i'm very tired can any one tell me what is wrong in this code

Code:

```class SingleArray {  private static int[] array = new int[] { 5, 10, 15, 25}; int  a []={{1,2,3}}; int sum = 0; int idx = -1; for (int i = 0; i < a.length; i++) {     if (a[i] == 0) {         idx = i;     } else {         sum += a[i];     } } // the total sum of numbers between 1 and arr.length. int total = (a.length + 1) * a.length / 2; System.out.println("missing number is: " + (total - sum) + " at index " + idx);     }   } }```