Advertisement
scaawt

Lab10

Oct 9th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. package CSCE145;
  2.  
  3. //written by tariq scott
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         Scanner keyboard = new Scanner(System.in);
  9.  
  10.         //telling user to input the size of the matrix
  11.         System.out.println("Enter a length for matrix 1");
  12.         int length = keyboard.nextInt();
  13.         System.out.println("Enter a width for matrix 1");
  14.         int width = keyboard.nextInt();
  15.         System.out.println("Enter a length for matrix 2");
  16.         int lengthTwo = keyboard.nextInt();
  17.         System.out.println("Enter a width for matrix 2");
  18.         int widthTwo = keyboard.nextInt();
  19.  
  20.         int matrixOne[][] = new int[length][width];
  21.         int matrixTwo[][] = new int[lengthTwo][widthTwo];
  22.  
  23.         if (length != width) {
  24.             System.out.println("Cannot add these! Dimensions Mismatch");
  25.         }
  26.         if (lengthTwo != widthTwo) {
  27.             System.out.println("Cannot add these! Dimensions Mismatch");
  28.         }
  29.  
  30.         //matrix one numbers
  31.         for (int i = 0; i < length; i++) {
  32.  
  33.             for (int j = 0; j < width; j++) {
  34.  
  35.                 System.out.println("Enter a value for matrix 1 space " + i + "," + j);
  36.                 matrixOne[i][j] = keyboard.nextInt();
  37.             }
  38.         }
  39.  
  40.         //matrix two numbers
  41.         for (int i = 0; i < lengthTwo; i++) {
  42.  
  43.             for (int j = 0; j < widthTwo; j++) {
  44.                 System.out.println("Enter a value for matrix 2 space " + i + "," + j);
  45.                 matrixTwo[i][j] = keyboard.nextInt();
  46.             }
  47.         }
  48.         int matrixThree[][] = new int[length][width];
  49.  
  50.  
  51.         for (int i = 0; i < length; i++) {
  52.  
  53.             for (int j = 0; j < width; j++) {
  54.  
  55.                 matrixThree[i][j] = matrixOne[i][j] + matrixTwo[i][j];
  56.             }
  57.         }
  58.         for (int i = 0; i < length; i++) {
  59.  
  60.             for (int j = 0; j < width; j++) {
  61.  
  62.                 System.out.print(matrixThree[i][j] + " ");
  63.  
  64.  
  65.             }
  66.             System.out.println();
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement