วันพุธที่ 12 พฤศจิกายน พ.ศ. 2557

Class Of Matrix

void setup () {
  Add_Matrix m;
  int [][] Matrix1 = {
    {
      2, 4, 6
    }
    , {
      8, 10, 12
    }
    , {
      14, 16, 18
    }
  };
  int [][] Matrix2 = {
    {
      1, 3, 5
    }
    , {
      7, 9, 11
    }
    , {
      13, 15, 17
    }
  };
  m = new Add_Matrix (Matrix1, Matrix2);
  m.display(Matrix1, Matrix2);
}
////////////////////////////////////////
class Add_Matrix {
  int [][] x;
  int [][] y;

  Add_Matrix (int [][]a, int [][] b ) {
    this.x = a;
    this.y = b;
  }

  int [][]calculation (int[][]a, int [][]b) {
    int [][]additions = new int [a.length][a[0].length];
    for (int j = 0; j<a.length; j++) {
      for (int i =0; i<a[0].length; i++) {
        additions[j][i] = a[j][i]+b[j][i];
      }
    }
    return additions;
  }
  ///////////////////////////////////////////
  void display (int [][]a, int [][]b) {
    int [][]result = calculation (a, b);
    for (int j=0; j<a.length; j++) {
      for (int i =0; i<a[0].length; i++) {
        print((result[j][i]+ "\t" ));
      }
      println();
    }
  }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น