*編集は投稿の末尾に追加されます。なぜ、ループしている間に無限ループするのですか?
10行2列の2D配列を生成するC言語でコードを生成しようとしました。行が生成されたときに、それが別の既存の行と同じであれば、新しい一意の行になるまで値を変更し続けるかどうかを確認するためのチェックを作成しました。
私は現在、ループを終了させるべきでないことを確認するために、最大変数を1に設定しています。このコードは実際に重複を見つけてそれに応じて変更しますが、最終的にはチェックをスキップし、無限ループの代わりに終了するように見えます。 printf関数は、自分のコードの動作を調べるために使用されます。
なぜwhileループが終了しますか?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char *argv[]) {
int max_x = 1, max_y = 1;
int num_pt = 10;
int array[num_pt][2];
srand(time(0));
//Generate a new file
if (argc == 1) {
printf("Generating instances\n");
for (int i = 0 ; i < num_pt ; i++) {
array[i][0] = rand()%(max_y+1);
array[i][1] = rand()%(max_x+1);
for (int j = i-1 ; j >= 0 ; j--) {
while ((array[i][0] == array[j][0]) && (array[i][1] == array[j][1])) {
printf("\narray[%d][0]/[%d][1] and array[%d][0]/[%d][1]: Duplicate exists", i, i, j, j);
array[i][0] = rand()%(max_y+1);
array[i][1] = rand()%(max_x+1);
}
}
}
for (int j = 0 ; j < num_pt ; j++) {
printf("\nRow[%d] = [%d][%d]", j, array[j][0], array[j][1]);
}
return 1;
}
}
サンプル出力:助けを
Generating instances
array[2][0]/[2][1] and array[0][0]/[0][1]: Duplicate exists
array[2][0]/[2][1] and array[0][0]/[0][1]: Duplicate exists
array[3][0]/[3][1] and array[1][0]/[1][1]: Duplicate exists
array[4][0]/[4][1] and array[1][0]/[1][1]: Duplicate exists
array[5][0]/[5][1] and array[1][0]/[1][1]: Duplicate exists
array[6][0]/[6][1] and array[4][0]/[4][1]: Duplicate exists
array[6][0]/[6][1] and array[2][0]/[2][1]: Duplicate exists
array[6][0]/[6][1] and array[0][0]/[0][1]: Duplicate exists
array[7][0]/[7][1] and array[6][0]/[6][1]: Duplicate exists
array[7][0]/[7][1] and array[0][0]/[0][1]: Duplicate exists
array[8][0]/[8][1] and array[4][0]/[4][1]: Duplicate exists
array[8][0]/[8][1] and array[2][0]/[2][1]: Duplicate exists
array[8][0]/[8][1] and array[0][0]/[0][1]: Duplicate exists
array[9][0]/[9][1] and array[8][0]/[8][1]: Duplicate exists
array[9][0]/[9][1] and array[6][0]/[6][1]: Duplicate exists
array[9][0]/[9][1] and array[6][0]/[6][1]: Duplicate exists
array[9][0]/[9][1] and array[0][0]/[0][1]: Duplicate exists
array[9][0]/[9][1] and array[0][0]/[0][1]: Duplicate exists
Row[0] = [1][1]
Row[1] = [1][0]
Row[2] = [0][0]
Row[3] = [0][1]
Row[4] = [0][1]
Row[5] = [0][0]
Row[6] = [1][0]
Row[7] = [0][0]
Row[8] = [0][1]
Row[9] = [0][1]
------------------
(program exited with code: 1)
Press return to continue
感謝。私は昨晩それについて考えてベッドに横たわっているように、ついに私の上に浮かんだ。私の解決策は、冗長性のある行が見つかった場合には、チェック全体を再実行するカウンタを追加することです。
int main (int argc, char *argv[]) {
int max_x = 2, max_y = 2;
int num_pt = 9;
int array[num_pt][2];
srand(time(0));
int rerun = 1;
//Generate a new file
if (argc == 1) {
printf("Generating instances\n");
for (int i = 0 ; i < num_pt ; i++) {
array[i][0] = rand()%(max_y+1);
array[i][1] = rand()%(max_x+1);
rerun++;
while (rerun != 0) {
rerun = 0;
for (int j = i-1 ; j >= 0 ; j--) {
if (array[i][0] == array[j][0] && array[i][1] == array[j][1]) {
printf("\narray[%d][0]/[%d][1] and array[%d][0]/[%d][1]: Duplicate exists", i, i, j, j);
array[i][0] = rand()%(max_y+1);
array[i][1] = rand()%(max_x+1);
rerun++;
}
}
}
}
for (int j = 0 ; j < num_pt ; j++) {
printf("\nRow[%d] = [%d][%d]", j, array[j][0], array[j][1]);
}
return 1;
}
}
この方法では動作しますが、誰かがこれを行うより効率的な方法を持っている場合は、私はすべて耳にします。
デバッガで1行ずつコードをステップ実行してみませんか?そうすれば、実際に起こっていることを簡単に見ることができます。 –