answersLogoWhite

0

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

14y ago

What else can I help you with?

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.


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 program on computers?

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


What is java virtucal mechine?

That refers to the program that runs the compiled 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.


Java Track?

Java Track is the comprehensive Java training program offered by Synergistic IT, designed to equip students with in-demand skills for real-world software development.


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


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;"


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.


Why main is used in java?

In java need main() method. without main() in java we won't run the java programe main() signals the entry point to the program - it tells the Java Virtual Machine which is the first program that should be executed.