# Thread: how to find sum of each column ?

1. Member
Join Date
Jan 2014
Posts
16
Rep Power
0

## 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

Java 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 ?

2. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,746
Rep Power
4

## 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

3. Member
Join Date
Jan 2014
Posts
16
Rep Power
0

## 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

Java Code:
```int x[][]=new int[3][3];
void input()
{
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
Last edited by Dlovan; 01-14-2014 at 07:09 PM.

4. ## Re: how to find sum of each column ?

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

5. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,746
Rep Power
4

## 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

6. Member
Join Date
Jan 2014
Posts
16
Rep Power
0

## Re: how to find sum of each column ?

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

Java Code:
```int x[][]=new int[3][3];
void input()
{
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

Java 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

Java 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

7. ## 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

8. Member
Join Date
Jan 2014
Posts
16
Rep Power
0

## Re: how to find sum of each column ?

can you give me a simple i.e ?

9. ## Re: how to find sum of each column ?

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

10. Member
Join Date
Jan 2014
Posts
16
Rep Power
0

## Re: how to find sum of each column ?

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 !

11. ## Re: how to find sum of each column ?

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

12. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
2,695
Rep Power
3

## Re: how to find sum of each column ?

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.

13. Member
Join Date
Jan 2014
Posts
16
Rep Power
0

## Re: how to find sum of each column ?

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 i'm very tired can any one tell me what is wrong in this code

Java 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);

}

}

}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•