Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package CSCE145;
- //written by tariq scott
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in);
- //telling user to input the size of the matrix
- System.out.println("Enter a length for matrix 1");
- int length = keyboard.nextInt();
- System.out.println("Enter a width for matrix 1");
- int width = keyboard.nextInt();
- System.out.println("Enter a length for matrix 2");
- int lengthTwo = keyboard.nextInt();
- System.out.println("Enter a width for matrix 2");
- int widthTwo = keyboard.nextInt();
- int matrixOne[][] = new int[length][width];
- int matrixTwo[][] = new int[lengthTwo][widthTwo];
- if (length != width) {
- System.out.println("Cannot add these! Dimensions Mismatch");
- }
- if (lengthTwo != widthTwo) {
- System.out.println("Cannot add these! Dimensions Mismatch");
- }
- //matrix one numbers
- for (int i = 0; i < length; i++) {
- for (int j = 0; j < width; j++) {
- System.out.println("Enter a value for matrix 1 space " + i + "," + j);
- matrixOne[i][j] = keyboard.nextInt();
- }
- }
- //matrix two numbers
- for (int i = 0; i < lengthTwo; i++) {
- for (int j = 0; j < widthTwo; j++) {
- System.out.println("Enter a value for matrix 2 space " + i + "," + j);
- matrixTwo[i][j] = keyboard.nextInt();
- }
- }
- int matrixThree[][] = new int[length][width];
- for (int i = 0; i < length; i++) {
- for (int j = 0; j < width; j++) {
- matrixThree[i][j] = matrixOne[i][j] + matrixTwo[i][j];
- }
- }
- for (int i = 0; i < length; i++) {
- for (int j = 0; j < width; j++) {
- System.out.print(matrixThree[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement