Προς το περιεχόμενο

Προτεινόμενες αναρτήσεις

  • Απαντ. 46
  • Δημ.
  • Τελ. απάντηση

Συχνή συμμετοχή στο θέμα

Συχνή συμμετοχή στο θέμα

Δημοσ.

Προφανώς και υπάρχει κάποιο πρόβλημα. Το ποιο και που θα το βρεις με τον gdb ή με κάποιον άλλον debugger... κάνε καφεδάκι, κάνε κι ένα τσιγαράκι και ξεκίνα με ηρεμία, σιγά-σιγά και θα το βρεις.

Δημοσ.

κάτι τελευταίο, έχουν γίνει πάνω απο 30000 κλήσεις της rec είναι πολλές αλλά η συνθήκη για να γίνει η όχι αναδρομή είναι σωστή

Δημοσ.

τον κωδικα δειξε μας... ;)

 

το πρόγραμμα μαζεμένο σε ένα αρχείο (την συνάρτηση cont δεν την έχει δώσει αλλά πιστεύω ότι την έγραψα σωστά)

 

>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int cont(int **,int,int,int);
int rec (int *,int *,int *,int**,int **,int,int,int);
int stmar  (int *,int *,int**,int **,int,int,int);
//void gshap (int *,int *,int **,int**,int);

int main(void){
   int n,**prefw,**prefm,*prm,*prw,i,j,k,ab,flag,*col,l;
   long curtime;
   curtime=time(NULL);
   srand((unsigned int) curtime);
   printf("Please give the number of men and women\n");
   scanf("%d",&n);getchar();
   if (n==0)      {
      printf("There aren't any men and women to marry\n");
      system("pause");
   }
   prefm = (int**)malloc(n * sizeof(int *)); /* preferences for each man */
   prefw = (int**)malloc(n * sizeof(int *)); /* preferences for each woman */
   prm = (int*)malloc(n * sizeof(int )); /* woman whom each man has married */
   prw = (int*)malloc(n * sizeof(int )); /* man whom each woman has married */
   col = (int*)malloc((n+1)* sizeof(int )); /* point which column from prefm is woman whom each man has married */
                                      /* col[n]: the number of solutions for part II of exercise 3 */
   if (prefm==NULL || prefw==NULL || prm==NULL || prw==NULL || col==NULL)  {
      printf("Sorry, cannot allocate memory\n");
      return -1;
   }
   for (i=0;i<=n;i++)
       col[i]=0;
   for (i=0;i<n;i++)     {        
       *(prefm+i)=(int*)malloc(n * sizeof(int));
       *(prefw+i)=(int*)malloc(n * sizeof(int));
        if (*(prefm+i)==NULL || *(prefw+i)==NULL) {
           printf("Sorry, cannot allocate memory\n");
           return -1;
        }
   }
   for (i=0;i<n;i++)    {
       prm[i]=0; /* 0: point that the man i+1 is free */
       //printf("Please give the order of preferences for  m%3.3d\n",i+1);
       for (j=0;j<n;j++) {
           do {
               prefm[i][j]=rand()%n+1;
           }   while (cont(prefm,i,j,prefm[i][j])!=0);
       }
   } 
   printf("\n"); 
   for (i=0;i<n;i++)    {
       prw[i]=0;  /* 0: point that the woman i+1 is free */
       //printf("Please give the order of preferences for  w%3.3d\n",i+1);
       for (j=0;j<n;j++)   {
           do         {
                      prefw[i][j]=rand()%n+1;
                      }   while (cont(prefw,i,j,prefw[i][j])!=0);
       }
   }
   printf("\n\n");
   for (i=0;i<n;i++) {
       printf("m%3.3d order of preferences: ",i+1);
       for (j=0;j<n;j++)
           printf("w%3.3d ",prefm[i][j]);
       printf("\n");
   }  
   printf("\n");
   for (i=0;i<n;i++) {
       printf("w%3.3d order of preferences: ",i+1);
       for (j=0;j<n;j++)
           printf("m%3.3d ",prefw[i][j]);
       printf("\n");
   }
      
   printf("\n\n\nFinding the male optimal solution with the Gale-Shapley algorithm\n\n\n");
   //gshap(prm,prw,prefw,prefm,n);

   for (i=0;i<n;i++)
       prm[i]=prw[i]=0;
   printf("\n\n\nFinding all solutions with backtracking via recursion\n\n\n\n");
   system("pause");
   rec(prm,prw,col,prefw,prefm,0,0,n);
   system("pause");
}

int rec(int *prm,int *prw,int *col,int **prefw,int **prefm,int i ,int s,int n)  {   
    /* s: point which column from prefm is the first woman whom man i proposes */  
    int k,flag=0,j;
    while(i<n && col[n]!=-10)         {                        
         for (j=s;j<n;j++)            {
             printf("Trying to marry man %d to woman %d \n",i+1,prefm[i][j]);
             if (prw[prefm[i][j]-1]==0)  {
                if (stmar(prm,prw,prefw,prefm,i,j,n)==0) {                               
                   printf("Succeeded!!\n");
                   s=0; /* in order to try to marry the next man with the first woman of his preferences */
                   col[i]=j;
                   prm[i]=prefm[i][j] ;
                   prw[prefm[i][j]-1]=i+1 ;
                   if (i==n-1)   {   /* if we marry and the last man */             
                      col[n]++;      /* increase by 1 the number of solutions */
                      printf("\nSolution %d: \n\n",col[n]);
                      for (k=0;k<n;k++) {     
                                      printf("m%3.3d - w%3.3d   ",k+1,prm[k]);
                                      if ((k+1)%4==0 && k!=0)
                                         printf("\n");
                      }
                      printf("\n\n");
                      if (n!=1) {
                         prw[prm[i]-1]=0;
                         prm[i]=0;
                      }
                   }
                   else
                              break;
                }                   
                else
                   printf("Sorry, marriage is unstable\n");                
             }
             else                  
                printf("Sorry, woman%d is already married\n",prefm[i][j]); 
         }
         if (prm[i]==0)             { /* if man i is free */    
            prw[prm[i-1]-1]=0;   /* free woman whom man i-1 has married */
            prm[i-1]=0;    /* free man i-1 */
            for (k=0;k<=i-1;k++)
                if (col[k]!=n-1) 
                   flag=1;       
            if (flag==0){        /* if each man before i has married the last woman of his preferences */
               printf("\n\nFound %d Solution(s)\n",col[n]);
               flag=1;
               col[n]=-10; /* -10: random value which doesn't affect the program */
            }
            else /* try to marry man i-1 */
               rec(prm,prw,col,prefw,prefm,i-1,1+col[i-1],n);
                }
         i++;           
    }

}
int cont(int ** pref,int i ,int j,int value){
       for (int k=0;k<j;k++)
          if (pref[i][k]==value)
               return -1;
   return 0;
};
void gshap (int *a,int *b,int **c,int**d,int e){};

int stmar (int *prm,int *prw,int **prefw,int **prefm,int i,int j,int n)  {   
   int k,flag1,p,flag2,ke,kr;
   for (k=1;k<=i;k++)  {
       flag1=0;
       for(p=0;p<n;p++) {
           if (prefw[prm[k-1]-1][p]== i+1) /* if woman of man k prefers man i+1 more than her man */
              flag1++;
           else if (prefw[prm[k-1]-1][p]== k)  /* if woman of man k prefers her man more than man i */         
              break;
       }
       for(p=0;p<n;p++) {
           if (prefm[i][p]==prm[k-1]) /*if man i prefers woman of man k more than his woman */  
              flag1++;
           else if (prefm[i][p] == prefm[i][j])        /*if man i prefers his woman more than woman of man k */
              break;
       }
       if (flag1==2 ) /* if there is a unstable marriage */
          break;
   }
   for (k=1;k<=i;k++)  {
       flag2=0;
       for(p=0;p<n;p++) {
           if (prefm[k-1][p]==prefm[i][j])  /*if man k prefers woman of man i more than his woman */
                   flag2++;
           else if (prefm[k-1][p]==prm[k-1])   /*if man k prefers his woman more than woman of man k */
               break;
       }
       for(p=0;p<n;p++) {
           if (prefw[prefm[i][j]-1][p]==k) /*if woman of man i prefers man k more than her man */
                   flag2++;                    
           else if (prefw[prefm[i][j]-1][p]==i+1) /*if woman of man i prefers her man more than man k */
                   break;
       }
       if (flag2==2)       /* if there is a unstable marriage */
           break;
   }
   if (flag1==2 || flag2==2)
      return 1;
   else
      return 0;
}


Δημοσ.

απλά είμαι καινούργιος στον προγραματισμό και έχω χαθεί.

 

Για να είμαι απολύτως ειλικρινής, η έλλειψη περιγραφής του τι προσπαθείς να κάνεις, το στυλ γραφής που χρησιμοποιείς, τα ακαταλαβίστικα ονόματα μεταβλητών, αλλά κυρίως η πολύπλοκη λογική που ακολουθείς στην υλοποίηση με περίπου... μυριάδες ορίσματα στις συναρτήσεις και ξεχωριστά αρχεία για κάθε συνάρτηση, προσωπικά με αποτρέπουν από το να κάτσω να ασχοληθώ με τον κώδικά σου (όχι μόνο με αποτρέπουν, αλλά και μόνο που τον βλέπω με πιάνει... ζαλάδα).

 

Τα σχόλια που έχεις συμπεριλάβει προφανώς βοηθάνε, αλλά βοηθάνε εσένα που λίγο-πολύ ξέρεις τι έχεις κάνει, όχι όμως κι εμάς.

 

Οπότε αρκούμαι μονάχα σε γενικού είδους παρατηρήσεις/επισημάνσεις (όπως έκανα με τον gdb χτες). Εφόσον έγραψες χτες πως έχεις θέμα με τις επαναλήψεις της αναδρομής, το 1ο που πρέπει να κοιτάξεις είναι το base case της (εκείνη δηλαδή τη συνθήκη που καθορίζει πότε θα τερματίσει η συνάρτηση).

 

ΥΓ. Οι συναρτήσεις πρέπει να είναι όσο το δυνατόν πιο λιτές και με όσο το δυνατόν λιγότερα ορίσματα. Ότι μπορεί να χρησιμοποιηθεί ως τοπική μεταβλητή μέσα στη συνάρτηση πρέπει να ορίζεται και να χρησιμοποιείται έτσι, τοπικά. Αυτό μπορεί να σημαίνει και ανασχεδιασμό της λογικής του προγράμματος.

 

Από την άλλη μεριά, στην αναδρομή είναι συχνά αναπόφευκτο να περαστούν πολλά ορίσματα (συνήθως μετρητές των επαναλήψεων) αλλά ακόμα και τότε το κυρίως σώμα της αναδρομικής συνάρτησης πρέπει να είναι όσο το δυνατόν πιο λιτό, σύντομο και καθαρό.

 

Εσύ έχεις μια αναδρομική συνάρτηση rec, με καμιά 10αριά ορίσματα (με τουλάχιστον αμφιλεγόμενα ονόματα) και ... άπειρο κώδικα στο κυρίως σώμα της. Χάνει η μάνα το παιδί και το παιδί τη μάνα, που να βγάλουμε άκρη εκεί μέσα!

Δημοσ.

Για να είμαι απολύτως ειλικρινής, η έλλειψη περιγραφής του τι προσπαθείς να κάνεις, το στυλ γραφής που χρησιμοποιείς, τα ακαταλαβίστικα ονόματα μεταβλητών, αλλά κυρίως η πολύπλοκη λογική που ακολουθείς στην υλοποίηση με περίπου... μυριάδες ορίσματα στις συναρτήσεις και ξεχωριστά αρχεία για κάθε συνάρτηση, προσωπικά με αποτρέπουν από το να κάτσω να ασχοληθώ με τον κώδικά σου (όχι μόνο με αποτρέπουν, αλλά και μόνο που τον βλέπω με πιάνει... ζαλάδα).

 

Τα σχόλια που έχεις συμπεριλάβει προφανώς βοηθάνε, αλλά βοηθάνε εσένα που λίγο-πολύ ξέρεις τι έχεις κάνει, όχι όμως κι εμάς.

 

Οπότε αρκούμαι μονάχα σε γενικού είδους παρατηρήσεις/επισημάνσεις (όπως έκανα με τον gdb χτες). Εφόσον έγραψες χτες πως έχεις θέμα με τις επαναλήψεις της αναδρομής, το 1ο που πρέπει να κοιτάξεις είναι το base case της (εκείνη δηλαδή τη συνθήκη που καθορίζει πότε θα τερματίσει η συνάρτηση).

 

ΥΓ. Οι συναρτήσεις πρέπει να είναι όσο το δυνατόν πιο λιτές και με όσο το δυνατόν λιγότερα ορίσματα. Ότι μπορεί να χρησιμοποιηθεί ως τοπική μεταβλητή μέσα στη συνάρτηση πρέπει να ορίζεται και να χρησιμοποιείται έτσι, τοπικά. Αυτό μπορεί να σημαίνει και ανασχεδιασμό της λογικής του προγράμματος.

 

Από την άλλη μεριά, στην αναδρομή είναι συχνά αναπόφευκτο να περαστούν πολλά ορίσματα (συνήθως μετρητές των επαναλήψεων) αλλά ακόμα και τότε το κυρίως σώμα της αναδρομικής συνάρτησης πρέπει να είναι όσο το δυνατόν πιο λιτό, σύντομο και καθαρό.

 

Εσύ έχεις μια αναδρομική συνάρτηση rec, με καμιά 10αριά ορίσματα (με τουλάχιστον αμφιλεγόμενα ονόματα) και ... άπειρο κώδικα στο κυρίως σώμα της. Χάνει η μάνα το παιδί και το παιδί τη μάνα, που να βγάλουμε άκρη εκεί μέσα!

Η περιγραφή iphw1112_3.pdf (ξεκινάει απο την 4η σελίδα)

Οταν λες τρόπο γράφης τι εννοείς?Επίσης πιστεύω οτι είναι καλά τα ονόματα των μεταβλητών και έχω και σχόλια που εξηγούν κάθε μεταβλητή τι σημαίνει.Κατα την γνώμη σου τι ονόματα έπρεπε να ειχα βάλει?

Τώρα ως προς τις συναρτήσεις δεν ξέρω πως γίνεται να βάλω λιγότερα ορίσματα αφου χρειάζομαι τα στοιχεία ολων των πινάκων.

Ένας τρόπος που σκεύτηκα για να τις κάνω πιο λιτές είναι να προσθέσω περισσότερες συναρτήσεις.

 

το 1ο που πρέπει να κοιτάξεις είναι το base case της (εκείνη δηλαδή τη συνθήκη που καθορίζει πότε θα τερματίσει η συνάρτηση)

Έτσι όπως το έχω φτιάξει το πρόγραμμα η συναρτήση καλει τον εαυτό της συνέχεια (υπο μια προυπόθεση φυσικά) οταν ολοκληρωθουν αυτα που πρέπει να γίνουν σταματαω την συναρτηση που κλήθηκε στο τελος και μετά σταματανε μια μια οι προηγούμενες αναδρομικές καλούμενες συναρτήσεις.Μήπως αυτο είναι λάθος?

 

Υ.Σ θα προσπαθήσω να το κάνω πιο ευανάγνωστο και θα ανεβάσω εκ νέου τον κώδικα

Υ.Σ είναι το 3ο πρόγραμμα που έχω ασχοληθεί στην ζωή μου οπότε δεν έχω πείρα συγνώμη.

Ευχαριστώ για τον χρόνο σου

Δημοσ.

Καλέ μου φίλε καταρχήν δεν χρειάζεται να απολογείσαι. Σου εξήγησα τους λόγους που αποτρέπουν εμένα προσωπικά να κοιτάξω τον κώδικά σου. Ως εποικοδομητικές επισημάνσεις τις έγραψα και όχι για να σου κάνω τον έξυπνο ή να σε κάνω να αισθανθείς άσχημα ή να σε... μαλώσω!

 

Άλλοι έχουν μεγαλύτερη ευχέρεια από μένα να διαβάζουν συμπυκνωμένο κώδικα τρίτων, εγώ δεν.

 

Διάβασα την εκφώνηση της άσκησής σου. Καταρχήν είναι πολύ καλή και δείχνει αν μη τι άλλο σοβαρή σχολή! Δυστυχώς όμως προϋποθέτει διάβασμα και πειραματισμούς ακόμα κι από ήδη έμπειρους, που όμως δεν πολυ-γουστάρουν την αναδρομή κι έχουν καταφέρει να την αποφύγουν στη πλειονότητα των περιπτώσεων.

 

Ανήκω σε αυτούς, ποτέ μου δεν την συμπάθησα την αναδρομή. Κατά περιπτώσεις είναι πραγματικά χρήσιμη αλλά δεν είμαι σε θέση να σε βοηθήσω στην παρούσα φάση διότι δεν έχω πρόσφατη την εξοικείωση που απαιτείται για την αναδρομική λύση της άσκησης αυτής (και δεν έχω καμία διάθεση να κάθομαι να παιδεύομαι με πειραματισμούς ;) ).

Δημοσ.

ο καθηγητής είπε ότι αν θέλουμε μπορούμε να μην την κάνουμε με αναδρομή όμως πρέπει να γίνεται με αλλον τρόπο οπισθοδρόμηση.

Εγς σκέφτηκα να αλλάξω το

>             else /* try to marry man i-1 */
               rec(prm,prw,col,prefw,prefm,i-1,1+col[i-1],n);
                }
         i++;   

σε

>             else /* try to marry man i-1 */
                  {
                     s=1+col[i-1];
                     i-=2;
                   }
                }
         i++;   

 

Απλά όταν είχα κάνει πιο παλιά έβγαζε προβλήματα.

Τι μου προτείνεις να κάνω?

Δημοσ.

Το backtracking είναι από τις περιπτώσεις εκείνες που η αναδρομή πραγματικά εξυπηρετεί και στη συντριπτική πλειοψηφία των περιπτώσεων συνδυάζεται με δέντρα (ζήσε Μάη μου να φας τριφύλλι, δηλαδή... πρέπει να αλλάξεις ολόκληρο το πρόγραμμα).

 

Οι παραθέσεις που μου κάνεις από τον κώδικά σου δεν με βοηθάνε, διότι πολύ απλά δεν τον έχω κοιτάξει τον κώδικά :P Ελπίζω όμως να βρεθεί κάποιος να σε βοηθήσει!

Δημοσ.

δεν έχουμε πει στο μάθημα ακομα για δέντρα.

Τι αλλαγές πρέπει να κάνω για να το κάνω πιο ευανάγνωστο?

Το πιο ανώδυνο κι αποτελεσματικό για υπάρχων κώδικα είναι να χρησιμοποιήσεις κάποιον source beautifier (prettifier) για C... όπως είναι π.χ. τα AStyle, Uncrustify, κλπ (google them).

 

Για νέο κώδικα, προσωπικά ακολουθώ σε ποσοστό που ξεπερνάει το 90% τα ANSI C Coding Standards

Δημοσ.

θα το κάνω πιο ευανάγνωστο οταν γυρίσω και θα σου στείλω το νέο και αν φυσίκα έχεις χρόνο και δεν είσαι κουρασμένος ριχτου μια ματιά

Δημοσ.
>#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* prototupa sunarthsewn */

void rec (int *,int *,int *,int**,int **,int,int);
int stmar  (int *,int *,int**,int **,int,int,int);
void solution (int *,int *,int *,int);


int main (void) {

   int i,j,k ;                                             /* metrhtes */
   int n ;                                                 /* plh8os antrwn kai gunekwn */
   int *wife,*husband,*series ;
   int **prefw,**prefm ;
   long curtime ;
   
   curtime=time(NULL);
   srand((unsigned int) curtime);
   printf("Please give the number of men and women\n");
   scanf("%d",&n);
   
   if (n==0)                                                /* an den uparxoun kanenas antras kai kamia guneka */
   {                                         
      printf("There aren't any men and women to marry\n");
      system("pause");
   }
   
   /* dhmiourgia twn dynamikwn pinakwn */
   
   prefm = malloc(n * sizeof(int *));                       /* oi protimiseis tou ka8e antra */
   prefw = malloc(n * sizeof(int *));                       /* oi protimiseis ths ka8e gunekas */
   wife = malloc(n * sizeof(int ));                         /* h guneka tou ka8e antra */
   husband = malloc(n * sizeof(int ));                      /* o antras ths ka8e gunekas */
   series = malloc((n+1)* sizeof(int ));                    /* deixnei thn seira pou exei h guneka tou ka8e antra stis protimiseis tou */
                                                            /* series[n]: deixnei ton ari8mo twn dunatwn tropwn kataskebhs stable gamwn */
   
   if (prefm==NULL || prefw==NULL || wife==NULL || husband==NULL || series==NULL)  
   {
      printf("Sorry, cannot allocate memory\n");
      return -1;
   }
         
   series[n]=0;                                             /* 0 luseis */
       
   for (i=0;i<n;i++)     
   {               
       *(prefm+i)=malloc(n * sizeof(int));
       *(prefw+i)=malloc(n * sizeof(int));
        if (*(prefm+i)==NULL || *(prefw+i)==NULL) 
        {
           printf("Sorry, cannot allocate memory\n");
           return -1;
        }
   }
   
   for (i=0;i<n;i++)    
   {        
       wife[i]=0; /* elef8erwnoume tis gunekes */
       printf("Please give the order of preferences for  m%3.3d\n",i+1);
       
       for (j=0;j<n;j++) 
       {
           scanf ("%d",&prefm[i][j]);
       }
   } 
    
   for (i=0;i<n;i++)    
   {
       husband[i]=0;  /* elef8erwnoume kai tous antres */
       printf("Please give the order of preferences for  w%3.3d\n",i+1);
       for (j=0;j<n;j++)   
       {
           scanf ("%d",&prefw[i][j]);
       }
   }
   
   for (i=0;i<n;i++)     
       series[i]=0;                                         /* oloi oi antres einai anypantroi */      
     
   //printf("\n\n");   
   //printf("\n\n\nFinding the male optimal solution with the Gale-Shapley algorithm\n\n\n");
   //gshap(wife,husband,prefw,prefm,n);
   //for (i=0;i<n;i++)
   //wife[i]=husband[i]=0;
   printf("\n\n\nFinding all solutions with backtracking via recursion\n\n\n\n");
   rec(wife,husband,series,prefw,prefm,0,n);
   system("pause");
}


/* kataskevh olwn twn dunatwn gamwn */

void rec(int *wife,int *husband,int *series,int **prefw,int **prefm,int i ,int n)  
{   
    int k,j;                                                      /* metrhtes */
    int flag=0;
    
    while(i<n && series[n]!=-10)         
    {                                                
         for (j=series[i];j<n;j++)            
         {
             printf("Trying to marry man %d to woman %d \n",i+1,prefm[i][j]);
             if (husband[prefm[i][j]-1]==0)  
             {
                if (stmar(wife,husband,prefw,prefm,i,j,n)==0) 
                {           
                   printf("Succeeded!!\n");                                         
                   series[i]=j+1;
                   wife[i]=prefm[i][j] ;
                   husband[prefm[i][j]-1]=i+1 ;
                   
                   if (i==n-1)                                   /* an pantrepsoume kai ton telefteo antra */               
                      solution (wife,husband,&series[n],n) ;
                   else
                   {
                      series[i+1]=0;
	       break;
                   }
                }                   
                else
                   printf("Sorry, marriage is unstable\n");                
             }
             else                  
                printf("Sorry, woman%d is already married\n",prefm[i][j]);                                                  
         }
         if (wife[i]==0)                                 /* an o antras i+1 einai elef8eros */             
         {    
            husband[wife[i-1]-1]=0;   /* free woman whom man i-1 has married */
            wife[i-1]=0;    /* free man i-1 */
            series[i]=0;
            for (k=0;k<i;k++)
                if (series[k]!=n) 
                   flag=1;  
                        
            if (flag==0){        /* if each man before i has married the last woman of his preferences */
               printf("\n\nFound %d Solution(s)\n",series[n]);
               series[n]=-10; /* -10: random value which doesn't affect the program */
            }
            if (series[n]!=-10) /* try to marry man i-1 */
               rec(wife,husband,series,prefw,prefm,i-1,n);
                }
         i++;           
    }

}


/*Elenxos an o gamos tou antra i me thn gunaika pou tis ekane protash gamou einai sta8eros
Oi gamoi einai sta8eroi otan den exoume 2 gamous A-G a'-G' (o antras A exei na pantreftei thn guneka G kai o antras A' thn guneka G')
tetoioi wste o a A na protima thn G' perissotero apo thn G kai h G' na protima ton A perissotero apo ton A'*/

int stmar (int *wife,int *husband,int **prefw,int **prefm,int i,int j,int n)
{   
   int k,p ;                                             /* metrhtes */
   int unstable1=0 ;
   int unstable2=0 ;
   
   for (k=1;k<=i;k++)  
   {
       unstable1=unstable2=0;
       for(p=0;p<n;p++)
       {
           if (prefw[wife[k-1]-1][p]== i+1)              /* if woman of man k prefers man i+1 more than her man */
              unstable1++;
           else if (prefw[wife[k-1]-1][p]== k)           /* if woman of man k prefers her man more than man i */         
              break;
       }
       
       for(p=0;p<n;p++) 
       {
           if (prefm[i][p]==wife[k-1])                   /*if man i prefers woman of man k more than his woman */  
              unstable1++;
           else if (prefm[i][p] == prefm[i][j])	     /*if man i prefers his woman more than woman of man k */
              break;
       }
       
       if (unstable1==2 )                               /* if there is a unstable marriage */
          break;

       for(p=0;p<n;p++) 
       {
           if (prefm[k-1][p]==prefm[i][j])              /*if man k prefers woman of man i more than his woman */
            unstable2++;
           else if (prefm[k-1][p]==wife[k-1])            /*if man k prefers his woman more than woman of man k */
               break;
       }
       
       for(p=0;p<n;p++) 
       {
           if (prefw[prefm[i][j]-1][p]==k)              /*if woman of man i prefers man k more than her man */
            unstable2++;			
           else if (prefw[prefm[i][j]-1][p]==i+1)       /*if woman of man i prefers her man more than man k */
            break;
       }
       if (unstable2==2)                                /* if there is a unstable marriage */
           break;
           
   }
   if (unstable1==2 || unstable2==2)
      return 1;
   else
      return 0;
}


/* ektupwsh twn lusewn */

void solution(int *wife,int *husband,int *solution,int n) 
{
    int k;
         
    solution[0]++;      /* increase by 1 the number of solutions */
    printf("\nSolution %d: \n\n",solution[0]);
    
    for (k=0;k<n;k++) 
    {     
         printf("m%3.3d - w%3.3d   ",k+1,wife[k]);
         if ((k+1)%4==0 && k!=0)
            printf("\n");
    }
    
    printf("\n\n");
         if (n!=1) 
         {
            husband[wife[n-1]-1]=0;
            wife[n-1]=0;
         }
}

Δημοσ.

με valgrind βρήκα οτι τελικά το segmentation fault οφείλετε στο stack overflow.

το stack overflow που οφείλετε?

η εκτέλεση του προγράματος με valgrind:

Please give the number of men and women

==20348== Memcheck, a memory error detector.

==20348== Copyright © 2002-2007, and GNU GPL'd, by Julian Seward et al.

==20348== Using LibVEX rev 1804, a library for dynamic binary translation.

==20348== Copyright © 2004-2007, and GNU GPL'd, by OpenWorks LLP.

==20348== Using valgrind-3.3.0-Debian, a dynamic binary instrumentation framework.

==20348== Copyright © 2000-2007, and GNU GPL'd, by Julian Seward et al.

==20348==

--20348-- Command line

--20348-- randsm_linux

--20348-- 20

--20348-- 2011

--20348-- Startup, with flags:

--20348-- -v

--20348-- --leak-check=yes

--20348-- Contents of /proc/version:

--20348-- Linux version 2.6.24-28-generic (buildd@palmer) (gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu3)) #1 SMP Wed Nov 24 09:30:14 UTC 2010

--20348-- Arch and hwcaps: X86, x86-sse1-sse2

--20348-- Page sizes: currently 4096, max supported 4096

--20348-- Valgrind library directory: /usr/lib/valgrind

--20348-- Reading syms from /lib/ld-2.7.so (0x4000000)

--20348-- Reading debug info from /lib/ld-2.7.so...

--20348-- ... CRC mismatch (computed 8d230822 wanted 7471daa4)

--20348-- object doesn't have a symbol table

--20348-- Reading syms from /home/users1/sdi1100130/ask3r3/randsm_linux (0x8048000)

--20348-- Reading syms from /usr/lib/valgrind/x86-linux/memcheck (0x38000000)

--20348-- object doesn't have a dynamic symbol table

--20348-- Reading suppressions file: /usr/lib/valgrind/default.supp

--20348-- Reading syms from /usr/lib/valgrind/x86-linux/vgpreload_core.so (0x401E000)

--20348-- Reading syms from /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so (0x4020000)

--20348-- Reading syms from /lib/tls/i686/cmov/libc-2.7.so (0x403B000)

--20348-- Reading debug info from /lib/tls/i686/cmov/libc-2.7.so...

--20348-- ... CRC mismatch (computed 819d92e3 wanted 4edc7d98)

--20348-- object doesn't have a symbol table

--20348-- REDIR: 0x40ad6c0 (rindex) redirected to 0x4023710 (rindex)

--20348-- REDIR: 0x40a8cc0 (malloc) redirected to 0x4022a50 (malloc)

--20348-- REDIR: 0x40af440 (strchrnul) redirected to 0x4023df0 (strchrnul)

Please give the order of preferences for m001

Please give the order of preferences for m002

Please give the order of preferences for m003

Please give the order of preferences for m004

Please give the order of preferences for m005

Please give the order of preferences for m006

Please give the order of preferences for m007

Please give the order of preferences for m008

Please give the order of preferences for m009

Please give the order of preferences for m010

Please give the order of preferences for m011

Please give the order of preferences for m012

Please give the order of preferences for m013

Please give the order of preferences for m014

Please give the order of preferences for m015

Please give the order of preferences for m016

Please give the order of preferences for m017

Please give the order of preferences for m018

Please give the order of preferences for m019

Please give the order of preferences for m020

Please give the order of preferences for w001

Please give the order of preferences for w002

Please give the order of preferences for w003

Please give the order of preferences for w004

Please give the order of preferences for w005

Please give the order of preferences for w006

Please give the order of preferences for w007

Please give the order of preferences for w008

Please give the order of preferences for w009

Please give the order of preferences for w010

Please give the order of preferences for w011

Please give the order of preferences for w012

Please give the order of preferences for w013

Please give the order of preferences for w014

Please give the order of preferences for w015

Please give the order of preferences for w016

Please give the order of preferences for w017

Please give the order of preferences for w018

--20348-- REDIR: 0x40aa500 (free) redirected to 0x40225f0 (free)

Please give the order of preferences for w019

Please give the order of preferences for w020

 

 

 

Finding all solutions with backtracking via recursion

 

 

 

--20348-- REDIR: 0x40ae550 (memset) redirected to 0x4023d50 (memset)

==20348==

==20348== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1)

--20348--

--20348-- supp: 11 dl-hack3-1

==20348== malloc/free: in use at exit: 0 bytes in 0 blocks.

==20348== malloc/free: 1 allocs, 1 frees, 80 bytes allocated.

==20348==

==20348== All heap blocks were freed -- no leaks are possible.

--20348-- memcheck: sanity checks: 1 cheap, 2 expensive

--20348-- memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use

--20348-- memcheck: auxmaps_L1: 0 searches, 0 cmps, ratio 0:10

--20348-- memcheck: auxmaps_L2: 0 searches, 0 nodes

--20348-- memcheck: SMs: n_issued = 7 (112k, 0M)

--20348-- memcheck: SMs: n_deissued = 0 (0k, 0M)

--20348-- memcheck: SMs: max_noaccess = 65535 (1048560k, 1023M)

--20348-- memcheck: SMs: max_undefined = 0 (0k, 0M)

--20348-- memcheck: SMs: max_defined = 21 (336k, 0M)

--20348-- memcheck: SMs: max_non_DSM = 7 (112k, 0M)

--20348-- memcheck: max sec V bit nodes: 0 (0k, 0M)

--20348-- memcheck: set_sec_vbits8 calls: 0 (new: 0, updates: 0)

--20348-- memcheck: max shadow mem size: 416k, 0M

--20348-- translate: fast SP updates identified: 1,898 ( 90.1%)

--20348-- translate: generic_known SP updates identified: 110 ( 5.2%)

--20348-- translate: generic_unknown SP updates identified: 98 ( 4.6%)

--20348-- tt/tc: 3,928 tt lookups requiring 3,957 probes

--20348-- tt/tc: 3,928 fast-cache updates, 2 flushes

--20348-- transtab: new 1,956 (41,681 -> 602,803; ratio 144:10) [0 scs]

--20348-- transtab: dumped 0 (0 -> ??)

--20348-- transtab: discarded 0 (0 -> ??)

--20348-- scheduler: 137,927 jumps (bb entries).

--20348-- scheduler: 1/2,051 major/minor sched events.

--20348-- sanity: 2 cheap, 2 expensive checks.

--20348-- exectx: 769 lists, 8 contexts (avg 0 per list)

--20348-- exectx: 13 searches, 5 full compares (384 per 1000)

--20348-- exectx: 0 cmp2, 26 cmp4, 0 cmpAll

--20348-- errormgr: 6 supplist searches, 75 comparisons during search

--20348-- errormgr: 11 errlist searches, 26 comparisons during search

Segmentation fault

Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε

Πρέπει να είστε μέλος για να αφήσετε σχόλιο

Δημιουργία λογαριασμού

Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!

Δημιουργία νέου λογαριασμού

Σύνδεση

Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.

Συνδεθείτε τώρα

  • Δημιουργία νέου...