Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Adding / Multiplying two matrices

Adding / Multiplying two matrices

Scheduled Pinned Locked Moved Java
javawpftoolshelptutorial
17 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N nt_virus

    Hello. I did this..

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package javaapplication6;
    import java.io.*;
    /**
    *
    * @author Sachin
    */
    public class Main {

    /\*\*
     \* @param args the command line arguments
     \*/
    public static void main(String\[\] args) throws IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    

    try{
    System.out.println("Enter number of rows");
    int m = Integer.parseInt(br.readLine());
    System.out.println("Enter number of columns");
    int n = Integer.parseInt(br.readLine());
    int[][] first = new int[m][n];
    int[][] second = new int[m][n];
    int[][] sum = new int[m][n];
    System.out.println("Enter Value of first matrix");
    {
    for (int i=0;i<m;m++)
    {
    // System.out.println("Row no. " + (i+1));
    for (int j=0; j<n;j++)
    {
    // System.out.println("Enter value for column no. " + (j+1));
    first[m][n] = Integer.parseInt(br.readLine());

            }
        }
    }
    System.out.println("Enter value of second matrix");
    {
        for (int i=0;i<m;m++)
        {
            System.out.println("Row no. " + (i+1));
            for (int j=0;j<n;j++)
            {
                System.out.println("Enter value for column no. " + (j+1));
                second\[m\]\[n\] = Integer.parseInt(br.readLine());
            }
        }
        }
    System.out.println("Sum of two matrices are:");
    {
        for (int i=0;i<m;i++)
        {
            for (int j = 0; j<n;j++)
            {
                sum\[m\]\[n\] = first\[m\]\[n\] + second\[m\]\[n\];
            }
        }
        System.out.println(sum\[m\]\[n\]);
    }
    }
    catch (IOException e)
    {
       System.out.println(e);
    }
    }
    

    }

    Here in bold text.. first[m][n] = Integer.parseInt(br.readLine()); throws ArrayIndexOutOfBoundexception .... When I replace first[m][n] with first[i][j] .. it taking value .. how to rectify this error any suggestion..

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    nt_virus wrote:

    how to rectify this error any suggestion.

    I think this statement for (int i=0;i<m;m++) has a slight error.

    N 1 Reply Last reply
    0
    • L Lost User

      nt_virus wrote:

      how to rectify this error any suggestion.

      I think this statement for (int i=0;i<m;m++) has a slight error.

      N Offline
      N Offline
      nt_virus
      wrote on last edited by
      #3

      but that line add column values for 2nd row and so on till

      L 1 Reply Last reply
      0
      • N nt_virus

        but that line add column values for 2nd row and so on till

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        nt_virus wrote:

        but that line add column values for 2nd row and so on till

        Look closely, at the end of the loop you are incrementing the wrong variable :((

        N 1 Reply Last reply
        0
        • L Lost User

          nt_virus wrote:

          but that line add column values for 2nd row and so on till

          Look closely, at the end of the loop you are incrementing the wrong variable :((

          N Offline
          N Offline
          nt_virus
          wrote on last edited by
          #5

          poor me... I modified as following ,, and I m done around 90%

          /*
          * To change this template, choose Tools | Templates
          * and open the template in the editor.
          */

          package javaapplication6;
          import java.io.*;
          /**
          *
          * @author Sachin
          */
          public class Main {

          /\*\*
           \* @param args the command line arguments
           \*/
          public static void main(String\[\] args) throws IOException
          {
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          

          try{
          System.out.println("Enter number of rows");
          int m = Integer.parseInt(br.readLine());
          System.out.println("Enter number of columns");
          int n = Integer.parseInt(br.readLine());
          int[][] first = new int[m][n];
          int[][] second = new int[m][n];
          int[][] sum = new int[m][n];
          System.out.println("Enter Value of first matrix");
          {
          for (int i=0;i<first.length;i++)
          {
          System.out.println("Row no. " + (i+1));
          for (int j=0; j<first[i].length;j++)
          {
          System.out.println("Enter value for column no. " + (j+1));
          first[i][j] = Integer.parseInt(br.readLine());

                  }
              }
          }
          System.out.println("Enter value of second matrix");
          {
              for (int i=0;i<second.length;i++)
              {
                  System.out.println("Row no. " + (i+1));
                  for (int j=0;j<second\[i\].length;j++)
                  {
                      System.out.println("Enter value for column no. " + (j+1));
                      second\[i\]\[j\] = Integer.parseInt(br.readLine());
                  }
              }
              }
          System.out.println("Sum of two matrices are:");
          {
              for (int i=0;i<first.length;i++)
              {
                  for (int j = 0; j<second.length;j++)
                  {
                      sum\[m\]\[n\] = first\[i\]\[j\] + second\[i\]\[j\];
                  }
              }
              System.out.println(sum\[m\]\[n\]);
          }
          }
          catch (IOException e)
          {
             System.out.println(e);
          }
          }
          

          }

          same error is showing when i m adding two arrays.. :(

          L N 2 Replies Last reply
          0
          • N nt_virus

            poor me... I modified as following ,, and I m done around 90%

            /*
            * To change this template, choose Tools | Templates
            * and open the template in the editor.
            */

            package javaapplication6;
            import java.io.*;
            /**
            *
            * @author Sachin
            */
            public class Main {

            /\*\*
             \* @param args the command line arguments
             \*/
            public static void main(String\[\] args) throws IOException
            {
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            

            try{
            System.out.println("Enter number of rows");
            int m = Integer.parseInt(br.readLine());
            System.out.println("Enter number of columns");
            int n = Integer.parseInt(br.readLine());
            int[][] first = new int[m][n];
            int[][] second = new int[m][n];
            int[][] sum = new int[m][n];
            System.out.println("Enter Value of first matrix");
            {
            for (int i=0;i<first.length;i++)
            {
            System.out.println("Row no. " + (i+1));
            for (int j=0; j<first[i].length;j++)
            {
            System.out.println("Enter value for column no. " + (j+1));
            first[i][j] = Integer.parseInt(br.readLine());

                    }
                }
            }
            System.out.println("Enter value of second matrix");
            {
                for (int i=0;i<second.length;i++)
                {
                    System.out.println("Row no. " + (i+1));
                    for (int j=0;j<second\[i\].length;j++)
                    {
                        System.out.println("Enter value for column no. " + (j+1));
                        second\[i\]\[j\] = Integer.parseInt(br.readLine());
                    }
                }
                }
            System.out.println("Sum of two matrices are:");
            {
                for (int i=0;i<first.length;i++)
                {
                    for (int j = 0; j<second.length;j++)
                    {
                        sum\[m\]\[n\] = first\[i\]\[j\] + second\[i\]\[j\];
                    }
                }
                System.out.println(sum\[m\]\[n\]);
            }
            }
            catch (IOException e)
            {
               System.out.println(e);
            }
            }
            

            }

            same error is showing when i m adding two arrays.. :(

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            nt_virus wrote:

            sum[m][n] = first[i][j] + second[i][j];

            Sorry but I stupidly missed this first time round, but you are using the wrong indices again. sum[m][n] is an array of size m by n, so the highest cell that can be addressed is sum[m-1][n-1]. Change it to sum[i][j] and I think it should work.

            N 1 Reply Last reply
            0
            • N nt_virus

              poor me... I modified as following ,, and I m done around 90%

              /*
              * To change this template, choose Tools | Templates
              * and open the template in the editor.
              */

              package javaapplication6;
              import java.io.*;
              /**
              *
              * @author Sachin
              */
              public class Main {

              /\*\*
               \* @param args the command line arguments
               \*/
              public static void main(String\[\] args) throws IOException
              {
                  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
              

              try{
              System.out.println("Enter number of rows");
              int m = Integer.parseInt(br.readLine());
              System.out.println("Enter number of columns");
              int n = Integer.parseInt(br.readLine());
              int[][] first = new int[m][n];
              int[][] second = new int[m][n];
              int[][] sum = new int[m][n];
              System.out.println("Enter Value of first matrix");
              {
              for (int i=0;i<first.length;i++)
              {
              System.out.println("Row no. " + (i+1));
              for (int j=0; j<first[i].length;j++)
              {
              System.out.println("Enter value for column no. " + (j+1));
              first[i][j] = Integer.parseInt(br.readLine());

                      }
                  }
              }
              System.out.println("Enter value of second matrix");
              {
                  for (int i=0;i<second.length;i++)
                  {
                      System.out.println("Row no. " + (i+1));
                      for (int j=0;j<second\[i\].length;j++)
                      {
                          System.out.println("Enter value for column no. " + (j+1));
                          second\[i\]\[j\] = Integer.parseInt(br.readLine());
                      }
                  }
                  }
              System.out.println("Sum of two matrices are:");
              {
                  for (int i=0;i<first.length;i++)
                  {
                      for (int j = 0; j<second.length;j++)
                      {
                          sum\[m\]\[n\] = first\[i\]\[j\] + second\[i\]\[j\];
                      }
                  }
                  System.out.println(sum\[m\]\[n\]);
              }
              }
              catch (IOException e)
              {
                 System.out.println(e);
              }
              }
              

              }

              same error is showing when i m adding two arrays.. :(

              N Offline
              N Offline
              nt_virus
              wrote on last edited by
              #7

              99% done..

              for (int i=0;i<first.length;i++)
              {
              for (int j = 0; j<second.length;j++)
              {
              sum[i][j] = first[i][j] + second[i][j];
              System.out.println(sum[i][j]);
              }

              But result are showing in vertical manner.. not like matrix way..

              L 1 Reply Last reply
              0
              • L Lost User

                nt_virus wrote:

                sum[m][n] = first[i][j] + second[i][j];

                Sorry but I stupidly missed this first time round, but you are using the wrong indices again. sum[m][n] is an array of size m by n, so the highest cell that can be addressed is sum[m-1][n-1]. Change it to sum[i][j] and I think it should work.

                N Offline
                N Offline
                nt_virus
                wrote on last edited by
                #8

                We both post posted at the same time. haha.

                1 Reply Last reply
                0
                • N nt_virus

                  99% done..

                  for (int i=0;i<first.length;i++)
                  {
                  for (int j = 0; j<second.length;j++)
                  {
                  sum[i][j] = first[i][j] + second[i][j];
                  System.out.println(sum[i][j]);
                  }

                  But result are showing in vertical manner.. not like matrix way..

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #9

                  nt_virus wrote:

                  But result are showing in vertical manner.. not like matrix way..

                  I can never remember what the order is supposed to be (I stick to one-dmensional arrays), so I'm afraid you will have to find it by trial and error. I would give you a hand but my Java system is not usable today.

                  N 1 Reply Last reply
                  0
                  • L Lost User

                    nt_virus wrote:

                    But result are showing in vertical manner.. not like matrix way..

                    I can never remember what the order is supposed to be (I stick to one-dmensional arrays), so I'm afraid you will have to find it by trial and error. I would give you a hand but my Java system is not usable today.

                    N Offline
                    N Offline
                    nt_virus
                    wrote on last edited by
                    #10

                    please do it in c# if you can, I will convert. I did my all possible things.

                    L 1 Reply Last reply
                    0
                    • N nt_virus

                      please do it in c# if you can, I will convert. I did my all possible things.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #11

                      OK, but it may take some time.

                      N 1 Reply Last reply
                      0
                      • L Lost User

                        OK, but it may take some time.

                        N Offline
                        N Offline
                        nt_virus
                        wrote on last edited by
                        #12

                        yes, no problem. do you use messenger ?

                        L 1 Reply Last reply
                        0
                        • N nt_virus

                          yes, no problem. do you use messenger ?

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #13

                          OK, I got the answer in C#, so I think the Java code needs to be:

                          for (int i=0;i < first.length;i++)
                          {
                              for (int j = 0; j < first\[i\].length;j++)
                              {
                                  sum\[i\]\[j\] = first\[i\]\[j\] + second\[i\]\[j\];
                                  System.out.print(sum\[i\]\[j\] + "  ");
                              }
                              System.out.println("");
                          }
                          

                          I'm not sure about the expression first.length, but basically i needs to iterate the rows, and j the columns. You also need to print each column value without the newline (print rather than println?) and then put the System.out.println(""); statement after the last column. It probably needs some tidying up to get the columns lined up, but I'm sure you will be able to manage it. No, not on messenger.

                          N 1 Reply Last reply
                          0
                          • L Lost User

                            OK, I got the answer in C#, so I think the Java code needs to be:

                            for (int i=0;i < first.length;i++)
                            {
                                for (int j = 0; j < first\[i\].length;j++)
                                {
                                    sum\[i\]\[j\] = first\[i\]\[j\] + second\[i\]\[j\];
                                    System.out.print(sum\[i\]\[j\] + "  ");
                                }
                                System.out.println("");
                            }
                            

                            I'm not sure about the expression first.length, but basically i needs to iterate the rows, and j the columns. You also need to print each column value without the newline (print rather than println?) and then put the System.out.println(""); statement after the last column. It probably needs some tidying up to get the columns lined up, but I'm sure you will be able to manage it. No, not on messenger.

                            N Offline
                            N Offline
                            nt_virus
                            wrote on last edited by
                            #14

                            fantastic.. It works.. you made my day (night here actually) .. Thanks a lot.

                            L 1 Reply Last reply
                            0
                            • N nt_virus

                              fantastic.. It works.. you made my day (night here actually) .. Thanks a lot.

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #15

                              Cheers, happy to help. It also gave me some incentive to get back on Java which I haven't touched for a while. It's early evening here (UK) so are you west or east?

                              N 1 Reply Last reply
                              0
                              • L Lost User

                                Cheers, happy to help. It also gave me some incentive to get back on Java which I haven't touched for a while. It's early evening here (UK) so are you west or east?

                                N Offline
                                N Offline
                                nt_virus
                                wrote on last edited by
                                #16

                                Java is very nice. I m on east. India. +5.30

                                L 1 Reply Last reply
                                0
                                • N nt_virus

                                  Java is very nice. I m on east. India. +5.30

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #17

                                  You may find this hard to believe but that's the country of my birth! I was born in Mussoorie (in the hills), my dad and my brother in Naini Tal. But my only return visit was in 2005 to Bangalore, which also was nice - at least the people.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups