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