Matrix Addition

package program_arrays;

import java.util.Scanner;

public class DataStructure_Basic {
	public static void main(String[] args) {
		DataStructure_Basic arraysp = new DataStructure_Basic();
		 arraysp.matrix_Addition();
	}
private void matrix_Addition() {
		// TODO Auto-generated method stub
		// 00 01 10 11
		int[][] a = { { 3, 4 }, { 2, 1 } };
		int[][] b = { { 1, 5 }, { 3, 7 } };
		int row = 0;
		int len = a.length;
		int len2 = a[0].length;

		int[][] result = new int[len][len2];
		while (row < len) {
			int col = 0;
			while (col < a[row].length) {
				result[row][col] = a[row][col] + b[row][col];
				col++;
			}
			row++;
		}
		for (row = 0; row < len; row++) {
			for (int col = 0; col < a[row].length; col++) {
				System.out.print(result[row][col] + " ");
			}
			System.out.println();
		}
}

Leave a comment

Design a site like this with WordPress.com
Get started