User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: May 2008
Posts: 22
Reputation: Punkis448 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Punkis448 Punkis448 is offline Offline
Newbie Poster

Re: Stable marriage

  #11  
May 9th, 2008
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.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: Stable marriage

  #12  
May 10th, 2008
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.
Last edited by jephthah : May 10th, 2008 at 3:01 am.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: Stable marriage

  #13  
May 10th, 2008
one problem jumps out:

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.
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Stable marriage

  #14  
May 10th, 2008
@ jephthah :

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
You can even change a setting so that the indention becomes 1 tab, or 4 spaces or 2 spaces or...
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
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: Stable marriage

  #15  
May 10th, 2008
ah, that's a good hint

too bad i dont use visual studio.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 11:26 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC