answersLogoWhite

0

Java program on matrix addition in java?

Updated: 8/17/2019
User Avatar

Wiki User

13y ago

Best Answer

Here's a method I wrote in Java that should do what you want:

public static int[][] addMatrices(int[][] mat1, int[][] mat2) {

int biggestWidth = mat1.length > mat2.length ? mat1.length : mat2.length;

int biggestHeight = mat1[0].length > mat2[0].length ? mat1[0].length : mat2[0].length;

int[][] answer = new int[biggestWidth][biggestHeight];

for(int i = 0; i<biggestWidth; i++) {

for(int j = 0; j<biggestHeight; j++) {

int one = 0;

if(i < mat1.length && j < mat1[i].length) one = mat1[i][j];

int two = 0;

if(i < mat2.length && j < mat2[i].length) two = mat2[i][j];

answer[i][j] = one + two;

}

}

return answer;

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Java program on matrix addition in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a java program to find the ad joint of a matrix?

bgfygfrhjyuyhh


What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


Java Track?

Java Track is the java training program offered by SynergisticIT


What powder game doesnt need java?

Any program written for the Java technology needs Java. Any program NOT written for Java DOESN'T need it.


What is java virtucal mechine?

That refers to the program that runs the compiled Java program.


What is java program on computers?

#!/usr/bin/perl print 'java program';


What has the author Patrick Niemeyer written?

Patrick Niemeyer has written: 'Learning Java' -- subject(s): Java (Computer program language) 'Learning Java' -- subject(s): Java (Computer program language), Java (programmeertaal) 'Java Reference Library on the Web'


What is a Java applet?

PCH answer small Embedded Java Program.


How do you execute a Java program?

You can run a Java application from the command line using "java &lt;name of the class&gt;"


Differences between Java Applet and Java Beans?

Java applet is a program used to run java applications while beans is a compiler used to design java programs (IDE, GUI) :-) GilbertC


What is a java programming?

A java program is a program that is coded and run in the programming language called java. Java is similar to c++ in structure, and is more common in web apps. C++ is the equivalent for more heavy duty programs such as most software used to compose a java program.


What is java's purpose on my PC?

Programs that are specifically designed with Java require a Java runtime to work on a computer. (In the case of Java, the runtime is called "Java Virtual Machine".) If you have any such program, you need to have Java.