Results 1 to 13 of 13
  1. #1
    Dlovan is offline Member
    Join Date
    Jan 2014
    Posts
    16
    Rep Power
    0

    Default 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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,329
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Dlovan is offline Member
    Join Date
    Jan 2014
    Posts
    16
    Rep Power
    0

    Default 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()
    {
    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("----------------");
    }


    , please if you can do it please help me and doing it


    regards
    Last edited by Dlovan; 01-14-2014 at 06:09 PM.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,308
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to find sum of each column ?

    Quote Originally Posted by Dlovan View Post
    , please if you can do it please help me and doing it
    What does your example code do? Does it run? Does it throw Exceptions as you? Does it produce incorrect results?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,329
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Dlovan is offline Member
    Join Date
    Jan 2014
    Posts
    16
    Rep Power
    0

    Default Re: how to find sum of each column ?

    Quote Originally Posted by JosAH View Post
    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()
    {
    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

    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. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,308
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Dlovan is offline Member
    Join Date
    Jan 2014
    Posts
    16
    Rep Power
    0

    Default Re: how to find sum of each column ?

    can you give me a simple i.e ?

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,308
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to find sum of each column ?

    Quote Originally Posted by Dlovan View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Dlovan is offline Member
    Join Date
    Jan 2014
    Posts
    16
    Rep Power
    0

    Default Re: how to find sum of each column ?

    Quote Originally Posted by JosAH View Post
    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. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,308
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to find sum of each column ?

    Quote Originally Posted by Dlovan View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,594
    Rep Power
    5

    Default Re: how to find sum of each column ?

    Quote Originally Posted by Dlovan View Post
    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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    Dlovan is offline Member
    Join Date
    Jan 2014
    Posts
    16
    Rep Power
    0

    Default Re: how to find sum of each column ?

    Quote Originally Posted by gimbal2 View Post
    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);
    
        }
    
      }
    
    }

Similar Threads

  1. Replies: 0
    Last Post: 10-18-2013, 06:03 PM
  2. Replies: 0
    Last Post: 01-17-2013, 12:43 PM
  3. insert row and column and delete row and column
    By daredavil82 in forum New To Java
    Replies: 13
    Last Post: 09-22-2011, 06:10 PM
  4. Replies: 9
    Last Post: 02-15-2011, 01:13 AM
  5. Replies: 1
    Last Post: 07-06-2010, 04:50 PM

Posting Permissions

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