わかりましたので、私は正式に知恵の終わりです。私はこのプログラムをコンパイルしてほぼ完了するまで実行しています。欠けている最後のものは、適切に戻ることです (大まかに言えば、i を 0 に戻して、すべての独身男性を再テストし、それらを適切にペアリングしようとします)。これが私のアルゴリズムです。while ループの最後が機能しない理由がわかりません。誰かが独身であるという条件が満たされた場合、すべての男性をテストした後、「i」を0に戻そうとするより良い方法はありますか?
何でも大助かりです。何が起こっているかの図は次のとおりです 。
//Check availability
i=0;
while ( i < ROWS /*(checkStatus(manStatus) && checkStatus(womanStatus))*/ ) {
//Loop through man #"i"
//for(i=0; i< ROWS ; i++) {
if(manStatus[i] == -1) /*if man i is free, then:*/ {
for(w=0; w<COLS; w++) {
//Assign him the first woman on his list that is free:
if (womanStatus[mPref[i][w+1]] == -1) { //if the woman in question is free (i.e -1 value), then :
E[i] = mPref[i][w+1]; //Row i, starting from index 1;
printf("Man #%d proposes to Woman#%d\n", i, mPref[i][w+1]);
printf("Woman #%d is single, and accepts Man #%d\n\n", mPref[i][w+1], i);
manStatus[i] = 0;
womanStatus[mPref[i][w+1]] = 0;
//Print the Women's Availability
for(j=0; j<ROWS; j++) {
printf("Woman #%d is currently:%d\n",j, womanStatus[j]);
}
//Print Men's Availability
for(j=0; j<ROWS; j++) {
printf("Man #%d is currently:%d\n",j, manStatus[j]);
}
printf("\nMan #%d is now engaged to Woman #%d\n", i, mPref[i][w+1]);
i++;
break;
}
//If the woman he wants is taken, check to see if she likes him more than her current engagement:
if(womanStatus[mPref[i][w+1]] != -1) {
printf("Man #%d proposes to Woman#%d\n", i, mPref[i][w+1]);
printf("Sorry, Woman #%d is taken. ", mPref[i][w+1]);
//Check to see who the other guy is:
printf("She is married to Man #%d\n", E[mPref[i][w+1]]);
int oldGuy = E[mPref[i][w+1]];
int newGuy = i;
//printf("New Guy: %d\n", newGuy);
//If the newGuy comes first in the array, replace the old guy with newGuy
if(preferanceCheck(mPref, wPref, oldGuy, newGuy, mPref[i][w+1])) { //If this returns true, then new guy is better liked
//Set newGuy as w's current engagement, and then set oldGuy to -1
printf("Since Woman #%d prefers Man#%d, they get engaged!\n", mPref[i][w+1], newGuy);
E[i] = mPref[i][w+1];
manStatus[i] = 0;
manStatus[oldGuy] = -1;
womanStatus[mPref[i][w+1]] = 0;
printf("\nMan #%d is now engaged to Woman #%d\n", i, mPref[i][w+1]);
printf("Man #%d is now single\n\n", oldGuy);
//Print the Women's Availability
for(j=0; j<ROWS; j++) {
printf("Woman #%d is currently:%d\n",j, womanStatus[j]);
}
//Print Men's Availability
for(j=0; j<ROWS; j++) {
printf("Man #%d is currently:%d\n",j, manStatus[j]);
}
i++;
break;
}
}
}
printf("\n");
//Test to see if people are still single
for(j = 0; j < ROWS; j++) {
if(manStatus[j] == -1) {
printf("Sorry, but Man#%d is still single\n", j);
if(ROWS - i == 1) {
i=0;
}
}
if(womanStatus[j] == -1) {
printf("Sorry, but Woman#%d is still single\n", j);
if(ROWS - i == 1) {
i=0;
}
}
}
}
} //While Condition