title: What fraction of the population is married?
body: <p>The problem below is one I came across on this <a href="http://wiki.quantsoftware.org/index.php?title=Compinvesti-prog-quiz" rel="nofollow noreferrer">wiki page</a>.</p>
  
  <blockquote>
    <p>Write a program to discover the answer to this puzzle:"Let's say men
    and women are paid equally (from the same uniform distribution). If
    women date randomly and marry the first man with a higher salary, what
    fraction of the population will get married?"</p>
  </blockquote>
  
  <p>My algorithm:</p>
  
  <ol>
  <li><p>Populate two arrays (female and male) with random salary values.</p></li>
  <li><p>Pair a female with a random male and compare salaries. If female
  salary is less than male, increase marriage counter. Set both female
  and male <code>isMarried</code> value to true.</p></li>
  <li><p>Continue dating process until maximum salary of unmarried males is
  less than the minimum salary of unmarried females.</p></li>
  </ol>
  
  <p>This is my implementation:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
  
  
      srand(time(NULL));
  
  
      int min = 1;
      int max = 1000000;
      Male male[100];
      Female female[100];
      double count = 0;
      bool done = false;
  
  
  
  
  
      //Fill array of Females and Males with random salaries ranging from 1 to 10
      for(int i=0; i&lt;100; i++){
          int output = min + (rand() % (int)(max - min + 1));
          male[i].salary = output;
      }
      for(int i=0; i&lt;100; i++){
          int output = min + (rand() % (int)(max - min + 1));
          female[i].salary = output;
      }
  
  
      //Start dating
      //Keep dating until the maximum salary of males is lower than minimum salary of females
  
      do{
          random_shuffle(begin(male), end(male));               //Shuffle array of males
          random_shuffle(begin(female), end(female));           //Shuffle array of females
  
  
          for(int i=0; i&lt;100; i++){                              //Compare a female and male from both arrays
              if(female[i].salary &lt; male[i].salary)
                  if(!female[i].isMarried &amp;&amp; !male[i].isMarried){
                      count++;
                      female[i].isMarried = true;
                      male[i].isMarried = true;
                      cout &lt;&lt; "Female salary: " &lt;&lt; female[i].salary &lt;&lt; endl;
                      cout &lt;&lt; "Male salary: " &lt;&lt; male[i].salary &lt;&lt; endl;
                  }
          }
  
          int maxMen = 0;
          for(int i=0; i&lt;100; i++){
              if(male[i].salary &gt; maxMen &amp;&amp; !male[i].isMarried)
                  maxMen = male[i].salary;
          }
  
          int minWomen = 1000000;
          for(int i=0; i&lt;100; i++){
              if(female[i].salary &lt; minWomen &amp;&amp; !female[i].isMarried)
                  minWomen = female[i].salary;
          }
  
          if(maxMen &lt;= minWomen)
              done = true;
  
  
      }while(!done);
  
  
      cout &lt;&lt; "Percentage: " &lt;&lt; count/100;
      cout &lt;&lt; endl;
  
      int unmarried = 0;
      cout &lt;&lt; "Number of unmarried females: ";
      for(int i=0; i&lt;100; i++)
          if(!female[i].isMarried)
              unmarried++;
      cout &lt;&lt; unmarried &lt;&lt; endl;
  
      unmarried = 0;
      cout &lt;&lt; "Number of unmarried males: ";
      for(int i=0; i&lt;100; i++)
          if(!male[i].isMarried)
              unmarried++;
      cout &lt;&lt; unmarried &lt;&lt; endl;
  
      cout &lt;&lt; endl;
  
  
  
  
      return a.exec();
  }
  </code></pre>
  
  <p>I asked this question on Programmers.SE and apparently, <a href="https://softwareengineering.stackexchange.com/a/254315/109561">I should be getting 68%</a>. 
  I'm getting percentage values that range from 35% to 40%. What am I doing wrong?</p>