•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 425,979 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,636 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 776 | Replies: 14
![]() |
•
•
Join Date: May 2008
Posts: 22
Reputation:
Rep Power: 1
Solved Threads: 0
i have fixed the mistakes...
#include <stdio.h>
#include <stdlib.h>
// Structure and Global Variables
//-------------------------------------------------
//VARIABLES AND NAMES THAT YOU CAN CHANGE
//Number of Men and Women you need to match
#define MWTOTAL 4
// Add Women and Men names below to match the MWTOTAL
char *wnames[50] = {
"Sarah",
"Michelle",
"Liz",
"Estella"
};
char *mnames[50] = {
"John",
"Travis",
"Don",
"Launnie"
};
//VARIABLES AND NAMES CHANGE ENDS HERE
//--------------------------------------------------
struct women {
int free;
int womenPreference[MWTOTAL];
} wset[MWTOTAL];
struct men {
int free;
int menPreference[MWTOTAL];
int proposeToW;
} mset[MWTOTAL];
int engageSet[MWTOTAL][MWTOTAL];
int totalEngaged=0;
// Function Definitions and Declarations
void initializeSets() {
int i;
for(i=0; i<MWTOTAL; i++) {
mset[i].free = -1; //Initially all men are free to propose to every women
mset[i].proposeToW = 0; //Gives the next free women that a man can propose
wset[i].free = -1;
}
}
void startStableMatch() {
int i,j,k,l,temp,temp1=0,cnt=0;
while (1) {
for (i=0; i<MWTOTAL; i++) {
if (mset[i].free == -1) { // A man is free to propose in decreasing order of preference
if (wset[mset[i].menPreference[mset[i].proposeToW]].free == -1) { //If Women is free, engage the Man/Women
engageSet[totalEngaged][0] = i;
engageSet[totalEngaged][1] = mset[i].menPreference[mset[i].proposeToW];
totalEngaged++;
wset[mset[i].menPreference[mset[i].proposeToW]].free = 0; //Women is not free anymore
mset[i].free = 0;
mset[i].proposeToW++;
}
else {
for (j=0; j<MWTOTAL; j++) {
if ( i == wset[mset[i].menPreference[mset[i].proposeToW]].womenPreference[j] ) break;
}
for (k=0; k<totalEngaged; k++) {
if ( engageSet[k][1] == mset[i].menPreference[mset[i].proposeToW] ) {
for (l=0; l<MWTOTAL; l++) {
if (engageSet[k][0] == wset[mset[i].menPreference[mset
[i].proposeToW]].womenPreference[l]) {
if ( j < l ) {
temp = engageSet[k][0];
engageSet[k][0] = i; // Women is engaged to the man that just proposed
mset[temp].free = -1; // Free the previously engaged man of W
mset[i].free = 0;
mset[i].proposeToW++;
}
else {
mset[i].free = -1; // Else the Women rejects the man's proposal.. free the man
mset[i].proposeToW++;
}
break;
}
}
break;
}
}
}
}
}
for (temp1=0; temp1<MWTOTAL; temp1++) {
if (mset[temp1].free == 0) cnt++;
}
if (cnt == 4) break; // No men is free, the algorithm terminates here.
cnt = 0;
}
}
void printEngageSet() {
int i;
printf ("\n\n\tThe stable pairs are listed below\n\n");
printf ("----------------------------------------------------------");
for (i=0; i<totalEngaged; i++) {
printf ("\n\t( %s , %s )\n",mnames[engageSet[i][0]],wnames[engageSet[i][1]]);
}
printf ("----------------------------------------------------------\n\n");
}
int main(void) {
printf ("\n\nPlease Wait while the Algorithm computes the Stable Set\n\n");
initializeSets();
startStableMatch();
printEngageSet();
return 0;
} Last edited by Punkis448 : May 9th, 2008 at 2:03 pm.
is it normal for you to write your code in this manner? what i mean is, do you intentionally refuse to indent your code, and purposefully line all your brackets up against the left wall?
because you see, im trying to debug your code -- I truly think this in an interesting puzzle -- but damned if it isnt frustrating as all hell to try and read it all smashed up against the left margin.
so rather than thinking about your code, all i can think of is, "why did he do this? do i want to go and insert 100 <tabs> in this guy's program, just so i can read it? ... is this what i want to do with my time?"
already I've spent 15 minutes in frustration plus typing this complaint. If i wasn't intrigued bythe problem i would have just quit looking at it and ignored this thread altogether.
because you see, im trying to debug your code -- I truly think this in an interesting puzzle -- but damned if it isnt frustrating as all hell to try and read it all smashed up against the left margin.
so rather than thinking about your code, all i can think of is, "why did he do this? do i want to go and insert 100 <tabs> in this guy's program, just so i can read it? ... is this what i want to do with my time?"
already I've spent 15 minutes in frustration plus typing this complaint. If i wasn't intrigued bythe problem i would have just quit looking at it and ignored this thread altogether.
Last edited by jephthah : May 10th, 2008 at 3:01 am.
one problem jumps out:
your "mset[i].proposeToW" evaluates to the value 4, and then is used as an index for another arrays with only four elements.
that's a run-time ARRAY OVERFLOW error.
.
if (wset[mset[i].menPreference[mset[i].proposeToW]].free ...
your "mset[i].proposeToW" evaluates to the value 4, and then is used as an index for another arrays with only four elements.
that's a run-time ARRAY OVERFLOW error.
.
Last edited by jephthah : May 10th, 2008 at 3:28 am.
•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation:
Rep Power: 11
Solved Threads: 185
@ jephthah :
If you are using Visual studio 200(x) you can let VS do all the hard work of indenting by pressing:
You can even change a setting so that the indention becomes 1 tab, or 4 spaces or 2 spaces or...
If you are using Visual studio 200(x) you can let VS do all the hard work of indenting by pressing:
ctrl-a ctrl-k ctrl-f
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
do NOT pm me for help, in the best case, you'll get ignored
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Recursivity (C++)
Other Threads in the C Forum
- Previous Thread: Ranking Array Failure?
- Next Thread: ftell() fclose() and FILE* f = NULL




Linear Mode