Programming Java

An Introduction to Computer Science



Coding Project 6 Solution

Matrix Addition

A matrix is a rectangular array of mathematical values. Two matrices of the same dimension can be added. Dimension is defined as number of rows by number of columns.

Figure A – Matrix Addition

Your assignment is to write the code for three methods: isSame(), addMatrix(), and printMatrix().

Here is the base program.

class Main {
  public static boolean isSame(int[][] a, int[][] b) {
    // return true if a and b have same dimension
    // else return false
    // dimension is defined as number of rows and number of columns
  }
  public static void addMatrix(int[][] a, int[][] b) {
    // precondition: a and b have same dimension
    // calculate: a = a + b
  }
  public static void printMatrix(int[][] a) {
    // output matrix by rows and columns, each value separated by a space
    // a1 a2 a3
    // a4 a5 a6
  }
  
  public static void main(String[] args) {
    int[][] m1 = {{1,2,3}, {4,5,6}};
    int[][] m2 = {{10,10,10}, {20,20,20}};

    System.out.println("-- m1 --");
    printMatrix(m1);
    System.out.println("-- m2 --");
    printMatrix(m2);
    
    if (isSame(m1,m2)) {
      addMatrix(m1, m2);
      System.out.println("-- m1 + m2 --");
      printMatrix(m1);
    } else {
      System.out.println("m1 and m2 have different dimensions.");
    }
  }
}

Solution


Step 1 – Write isSame() method.

public static boolean isSame(int[][] a, int[][] b) {
  // return true if a and b have same dimension
  // else return false
  // dimension is defined as number of rows and number of columns
}

Matrices a and b are arrays of arrays. In this example, matrix a has 2 elements. Each element of matrix a is also an array, containing 3 elements.

For any matrix a, the number of elements is retrieved by a.length. For any array element a[0], the number of elements is retrieved by a[0].length.

Two matrices have the same dimension if they have same number of rows and columns.

matrices a == b
if a.length == b.length         // same number of rows
and a[0].length == b[0].length  // same number of columns
and a[1].length == b[1].length
. . .
for every element a[n] and b[n]

Here is the code for isSame() method.

public static boolean isSame(int[][] a, int[][] b) {
  boolean result = true;
  if (a.length != b.length) {
    result = false;
  } else {
    for (int i=0; i<a.length; i++) {
      if (a[i].length != b[i].length) {
        result = false;
        break;
      }
    }
  }
  return result;
}

Line 3 – if a and b have different number of rows, return false.
Line 6 – iterate each element of a (and b)
Line 7 – if a[i] and b[i] have different number of elements, it means the columns don’t match. Return false.

You can test the method with a sample program.

public static void main(String[] args) {
  int[][] m1 = {{1,2,3}, {4,5,6}};
  int[][] m2 = {{10,10,10}, {20,20,20}};
  System.out.println(isSame(m1, m2));	// true

  int[][] a1 = {{1,2,3}, {4,5,6}, {7,8,9}};
  int[][] a2 = {{10,10,10}, {20,20,20}};
  System.out.println(isSame(a1, a2));	// false
    
  int[][] b1 = {{1,2,3,4}, {5,6,7,8}};
  int[][] b2 = {{10,10,10}, {20,20,20}};
  System.out.println(isSame(b1, b2));	// false
}

m1 and m2 have same dimension.
a1 has more rows than a2.
b1 has more columns than b2.

Step 2 – Write addMatrix() method.

public static void addMatrix(int[][] a, int[][] b) {
  // precondition: a and b have same dimension
  // calculate: a = a + b
}

To add matrices a and b, we add an element of a to the element of b in the matching [x][y] position.

In other words,
   a[i][j] + b[i][j]
where i iterates the rows and j iterates the columns.

The sum is stored in the matrix a.
   a[i][j] = a[i][j] + b[i][j]

Here is the code for addMatrix() method.

public static void addMatrix(int[][] a, int[][] b) {
  // a = a + b
  for (int i=0; i<a.length; i++) {
    for (int j=0; j<a[0].length; j++) {
      a[i][j] = a[i][j] + b[i][j];
    }
  }
}

Step 3 – Write printMatrix() method.

public static void printMatrix(int[][] a) {
  // output matrix by rows and columns, 
  // each value separated by a space
  // a1 a2 a3
  // a4 a5 a6
}

Where i iterates the rows and j iterates the columns,
print a[i][j] + " " for every j
and move the cursor to new line for very i.

Here is the code for printMatrix() method.

public static void printMatrix(int[][] a) {
  for (int i=0; i<a.length; i++) {
    for (int j=0; j<a[0].length; j++) {
      System.out.print(a[i][j] + " ");
    }
    System.out.println();
  }
}

Putting it all together, here is the complete program.

class Main {
  public static boolean isSame(int[][] a, int[][] b) {
    boolean result = true;
    if (a.length != b.length) {
      result = false;
    } else {
      for (int i=0; i<a.length; i++) {
        if (a[i].length != b[i].length) {
          result = false;
          break;
        }
      }
    }
    return result;
  }
  public static void addMatrix(int[][] a, int[][] b) {
    // a = a + b
    for (int i=0; i<a.length; i++) {
      for (int j=0; j<a[0].length; j++) {
        a[i][j] = a[i][j] + b[i][j];
      }
    }
  }
  public static void printMatrix(int[][] a) {
    for (int i=0; i<a.length; i++) {
      for (int j=0; j<a[0].length; j++) {
        System.out.print(a[i][j] + " ");
      }
      System.out.println();
    }
  }
  
  public static void main(String[] args) {
    int[][] m1 = {{1,2,3}, {4,5,6}};
    int[][] m2 = {{10,10,10}, {20,20,20}};
    
    System.out.println("-- m1 --");
    printMatrix(m1);
    System.out.println("-- m2 --");
    printMatrix(m2);
    
    if (isSame(m1,m2)) {
      addMatrix(m1, m2);
      System.out.println("-- m1 + m2 --");
      printMatrix(m1);
    } else {
      System.out.println("m1 and m2 have different dimensions.");
    }
  }
}

By:


Design a site like this with WordPress.com
Get started