## Java Beginners

Number of posts: 2739

This Forum Only

Matrix Problem Posted by Chuwy on 9 Jul 2008 at 9:11 AM
Hey guys! I'm a sophormore in highschool and I have this whole java summer course thing I have to take care of. The course, which I regrettably failed to pay full attention to, is over, but I don't really feel like I learned alot (or anything).

The thing is though, the teach left us with some random code problems which he left for us to solve. Here's one of them that I've been trying to figure out.

In one class, create 3 matrices of 3x3 grids, matrix a, matrix b, and matrix c.

Add the values of a to the values of b to create the values for c.

Please randomly generate the numbers in a and b. (Between 0 and 20)

a|--0--1--2-----b|--0--1--2-----c|--0--1--2

0|--1--2--3-----0|--10-11-12----0|--11-13-15
1|--4--5--6-----1|--13-14-15----1|--17-19-21
2|--7--8--9-----2|--16-17-18----2|--23-25-27

(I'm using all those dashes because the spacing goes wacky otherwise, the dashes aren't supposed to be in the program)

so the numbers in grid a are supposed to be all random, don't have to be only 1 digit, as long as its between 1-20. similarly with grid b, the numbers don't have to be 2 digits. and every value in c is the sum of the corresponding values in a and b.

any ideas?

and I'm supposed to print out a picture that looks something like that (don't need all the lines and dashes though)

Thanks.

Re: Matrix Problem Posted by zibadian on 9 Jul 2008 at 9:26 AM
: Hey guys! I'm a sophormore in highschool and I have this whole java
: summer course thing I have to take care of. The course, which I
: regrettably failed to pay full attention to, is over, but I don't
: really feel like I learned alot (or anything).
:
: The thing is though, the teach left us with some random code
: problems which he left for us to solve. Here's one of them that I've
: been trying to figure out.
:
: In one class, create 3 matrices of 3x3 grids, matrix a, matrix b,
: and matrix c.
:
: Add the values of a to the values of b to create the values for c.
:
: Please randomly generate the numbers in a and b. (Between 0 and 20)
:
: a|--0--1--2-----b|--0--1--2-----c|--0--1--2

: 0|--1--2--3-----0|--10-11-12----0|--11-13-15
: 1|--4--5--6-----1|--13-14-15----1|--17-19-21
: 2|--7--8--9-----2|--16-17-18----2|--23-25-27
:
: (I'm using all those dashes because the spacing goes wacky
: otherwise, the dashes aren't supposed to be in the program)
:
: so the numbers in grid a are supposed to be all random, don't have
: to be only 1 digit, as long as its between 1-20. similarly with grid
: b, the numbers don't have to be 2 digits. and every value in c is
: the sum of the corresponding values in a and b.
:
: any ideas?
:
: and I'm supposed to print out a picture that looks something like
: that (don't need all the lines and dashes though)
:
: Thanks.
:
:
This requires several nested for-loops. 1 for each a and b. 1 to add them together to form c. And 1 to display each matrix separately. Look up the for-loop in your notes/book and then start coding.
I would suggest that you create a method to create a matrix, one to perform the addition and one to display a given matrix.
Here's a start:
```public class MatrixAddition {

public int[][] createMatrix(int rows, int columns) {
}

public int[][] addMatrix(int[][] a, int[][] b) {
}

public void display(int[][] matrix) {
}

}
```
Re: Matrix Problem Posted by Chuwy on 9 Jul 2008 at 9:36 AM
Hahaha, sorry about the double post. I felt that this belonged more in the beginners section than anywhere else.

Re: Matrix Problem Posted by zibadian on 9 Jul 2008 at 9:38 AM
: Hahaha, sorry about the double post. I felt that this belonged more
: in the beginners section than anywhere else.
:
:
No problem. Moved to this thread to Java Beginners.
Re: Matrix Problem Posted by Chuwy on 9 Jul 2008 at 7:05 PM
The thing is I'm actually going on vacay in fiji islands for the next few weeks and I don't have internet access where I'm staying. I wanted to work on this while I'm there keep my mind sharp about what I learned.

would it be possible for you to post the solution here first? maybe put it in a .java file or something so I can check my answers after I finish my program. I promise I won't peek!

thanks

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic