-1
public int Gauss_Jordan(double[][] matrix, int numOfRows, int numOfCols) {
    for (int col_j = 0; col_j<numOfCols; col_j++) {
        row_i = nonzeros ++;
        System.out.println(row_i+" and "+nonzeros);
    }
    //return matrix;

    return 0;
}

up above in the method called "Gauss_Jordan", you can see a for loop where it iterates until a certain condition is met. (duh.. lol sorry).

so i set row_i = nonzeros++ but here's the thing, when I print out each iteration i get

  • 0 and 1,
  • 1 and 2,
  • 2 and 3

. I would expect the output to be:

  • 1 and 1,
  • 2 and 2,
  • 3 and 3.

How come this is not the case?

3 Answers3

4

You'd need ++nonzeros instead of nonzeros++ to get what you expect.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
4

Thats called post-increment;

When you say row_i = nonzeros ++;

first the row_i will get assigned with the value of nonzeros and the nonzero will get incremented.

try pre-increment

row_i = ++nonzeros; 
sanbhat
  • 17,522
  • 6
  • 48
  • 64
0

If pre-increment is not what you wanted. Check the initialization of nonzeros and change it into '1` for it to show up as you want. Your codes are functioning as they should.

Starx
  • 77,474
  • 47
  • 185
  • 261