With the following code:
void pivot(int n, int m, int evi, int lvi, float a[m][n]) {
int i,j;
float s = a[lvi][evi];
for (i = 0; i < n; i++) a[lvi][i] /= s;
for (j = 0; (j < m) && (j != lvi); j++) {
s = a[j][evi];
for (i = 0; i < n; i++) {
a[j][i] -= s * a[lvi][i];
printf("tab[%d][%d] = %f\n", j, i, a[j][i]);
}
}
}
float tab[3][6] = {{0.0}};
tab[0][0] = 200;
tab[0][1] =-200;
tab[0][2] = 60;
tab[0][3] =-60;
tab[0][4] = 1;
tab[1][0] =-100;
tab[1][1] = 100;
tab[1][2] =-150;
tab[1][3] = 150;
tab[1][5] = 1;
When I call
pivot(6,3,0,0,tab)
the second for loop in the pivot function (with && operator) never gets entered, why?
答案 0 :(得分:6)
A for
loop stops as soon as the condition becomes false. If it's false when the loop starts, the loop is never entered at all.
If you want to skip certain rows, you should put the check in the loop body.
for (j = 0; j < m; j++) {
if (j == lvi) {
continue;
}
s = a[j][evi];
for (i = 0; i < n; i++) {
a[j][i] -= s * a[lvi][i];
printf("tab[%d][%d] = %f\n", j, i, a[j][i]);
}
}