Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Homework NoEffort
package javaapplication3;
public class thread
{
public static void main(String[] args)
    {
    int a[][]={{3,4,2,9},{2,3,4,5},{2,2,3,4}};
    int b[][]={{6,7,8,3},{4,2,1,3},{8,3,5,6}};
    int sum[][]=new int[4][4];
    System.out.println("First Matrix A is:-");
    for (int i=0;i<=3;i++)
    {
        for (int j=0;j<=3;j++)
        {
            System.out.print(" "+a[i]);
        }
        System.out.println();
    }
    System.out.println("First Matrix B is:-");
    for (int i=0;i<=3;i++)
    {
        for (int j=0;j<=3;j++)
        {
            System.out.print(" "+b[j]);
        }
        System.out.println();
    }
    }
 
}
Posted 5-Nov-12 10:29am
Edited 5-Nov-12 10:30am
v2
Comments
Marcus Kramer at 5-Nov-12 15:30pm
   
It would help if you told us what problem you are having. (It's actually incredibly easy to see, but you need to do it)
If this is a homework question(which I suspect) then I would suggest doing it yourself so that you actually learn something.
Sergey Alexandrovich Kryukov at 5-Nov-12 15:30pm
   
Invalid question. The code can only be wrong or right in regards to its purpose; and you keep silence about that.
--SA
pasztorpisti at 5-Nov-12 15:56pm
   
Exactly, this code should compile and run.
Sandeep Mewara at 5-Nov-12 15:33pm
   
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It is wrong (for instance) here
Quote:
System.out.print(" "+a[i]);
Change to:
System.out.print(" "+a[i][j]);

and here
Quote:
System.out.print(" "+b[j]);
Change to:
System.out.print(" "+b[i][j]);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this. and see the bold marked text.
                int a[][] = 
            { 
                { 3, 4, 2, 9 }, 
                { 2, 3, 4, 5 }, 
                { 2, 2, 3, 4 } 
 
<pre>
        };
    int b[][] = 
        { 
            { 6, 7, 8, 3 }, 
            { 4, 2, 1, 3 }, 
            { 8, 3, 5, 6 } 
        };
    // int sum[][]=new int[4][4];
    System.out.println("First Matrix A is:-");
    for (int i = 0; i &lt; <b>a.length</b>; i++) {//upto rowlength  
                for (int j = 0; j &lt; <b>a[i].length</b>; j++) { //upto column length
           System.out.print(" " + <b>a[i][j]</b>);//get particular index data
        }
        System.out.println();
    }
    System.out.println("First Matrix B is:-");
    for (int i = 0; i &lt; <b>b.length</b>; i++) {
        for (int j = 0; j &lt; <b>b[i].length</b>; j++) {
            System.out.print(" " + <b>b[i][j]</b>);
        }
        System.out.println();
    }

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 310
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210


Advertise | Privacy | Mobile
Web04 | 2.8.150331.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100