May 15, 2013

I don't know why my variables are getting these values

User1552308’s Question:

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?

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.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!