Program 9.3: The Identity Matrix

Of course the algorithm you use to fill the array depends completely on the use to which the array is to be put. Program 9.3 calculates the identity matrix for a given dimension. The identity matrix of dimension N is a square matrix which contains ones along the diagonal and zeros in all other positions.

class IDMatrix {

  public static void main (String args[]) {
    double[][] ID;
    ID = new double[4][4];
    for (int row=0; row < 4; row++) {
      for (int col=0; col < 4; col++) {
        if (row != col) {
        else {
          ID[row][col] = 1.0;
Copyright 1996 Elliotte Rusty Harold
This Chapter