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 9:29am
Edited 5-Nov-12 9: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 } 
				
			};
		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 < a.length; i++) {//upto rowlength	
                    for (int j = 0; j < a[i].length; j++) { //upto column length
		       System.out.print(" " + a[i][j]);//get particular index data
			}
			System.out.println();
		}
		System.out.println("First Matrix B is:-");
		for (int i = 0; i < b.length; i++) {
			for (int j = 0; j < b[i].length; j++) {
				System.out.print(" " + b[i][j]);
			}
			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 George Jonsson 359
1 Sergey Alexandrovich Kryukov 355
2 CPallini 335
3 BillWoodruff 324
4 OriginalGriff 237
0 OriginalGriff 5,050
1 CPallini 4,225
2 Sergey Alexandrovich Kryukov 3,639
3 George Jonsson 2,911
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2014
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