Reputation: 3491
I am running a basic loop to check values on the upper diagonal elements of a two dimensional array:
//Check upper diagonal
for (i = 0; i < n; i++){
for (j = i+1; j<(n-1); j++){
printf("n: %d i: %d j: %d\n",n, i, j);
if (myA[i][j] > pow(10,-13)) return 0;
}
}
However, this code does not accurately check the elements I want it to. The print statement I placed in the inner loop gives the following output:
n: 4 i: 0 j: 1
n: 4 i: 0 j: 1
n: 4 i: 0 j: 2
n: 4 i: 1 j: 2
The particularly challenging part is between the first and second lines. It seems j is not being incremented after the first iteration of the middle loop as I would expect it to be.
Why doesn't the second line of my output show j: 2
?
Upvotes: 0
Views: 3371
Reputation: 70382
The second line of your output does not show j: 2
because your loop was terminated by the return 0
. The second line of your output is actually the result of another call on the function that contains this code.
You can easily test this by commenting out the if
statement in the inner loop:
//Check upper diagonal
for (i = 0; i < n; i++){
for (j = i+1; j<(n-1); j++){
printf("n: %d i: %d j: %d\n",n, i, j);
//if (myA[i][j] > pow(10,-13)) return 0;
}
}
Upvotes: 4