title: Matrix Enumeration Lab
body: <p>Your job is to write a program called matrix_enum, which takes three command line arguments: W, E and either an 'x' or an 'h'. Your program will enumerate all of the matrices for W and E, in any order you want, and print them out in one of the following two formats:</p>
  <ul>
  <li>If the last argument is 'x', then you'll print the matrices out in the format below. You should print each matrix as W lines of W characters that are '.', 'X' or 'E'. After each matrix, you print a blank line.</li>
  <li>If the last argument is 'h', then you'll convert each line of each matrix into an integer, and print that integer in hexadecimal, with no leading 0's and no leading &quot;0x&quot;. If element i in a row is 'X' or 'E', then you'll set the i-th bit of the number to 1. Otherwise, the i-th bit is zero. You'll print each integer on its own line, and print a blank line at the end of each matrix.</li>
  </ul>
  <hr />
  <p>UNIX&gt; matrix_enum 2 0 x</p>
  <p>X.</p>
  <p>.X</p>
  <p>.X</p>
  <p>X.</p>
  <hr />
  <p>UNIX&gt; matrix_enum 2 0 h</p>
  <p>1</p>
  <p>2</p>
  <p>2</p>
  <p>1</p>
  <hr />
  <p>Here's my code so far but I don't know where to go from here</p>
  <pre><code>  5 class Matrix {
    6     public:
    7         int W;
    8         int E;
    9         int P;                      /* This is 'x' or 'h' */
   10         vector &lt;int&gt; Perm;          /* Permutation of 0 .. (W-1), for the 'X' elements. */
   11         vector &lt;int&gt; Non_X;         /* This is the row/col id of each of the non-X elements. */
   12         vector &lt;int&gt; E_ID;          /* This is the row/col id of the E elements */
   13         void Print();               /* Print the matrix defined by W, Perm and E_ID */
   14         void Permute(int index);    /* This is the recursive permuting method. */
   15         void Choose(int index);     /* This is the recursive n-choose-k method. */
   16 };
   17 
   18 void Matrix::Print() {
   19 
   20     for(int i = 0; i &lt; Perm.size(); i++) {
   21 
   22 
   23     }
   24 }
   25
   26  void Matrix::Permute(int index) {
   27     //recursion to permute matrix
   28     string tmp;
   29 
   30     if(index == Perm.size()) {
   31         //Base case
   32         for (int i = 0; i &lt; W; i++) {
   33             for(int j = 0; j &lt; W; j++) {
   34                 //check if there is an 'x'
   35                 if(Perm.at(i) == j) {
   36                     continue;
   37                 }
   38 
   39                 Non_X.push_back(Perm.at(i));
   40                 Choose(0);
   41                 return;
   42             }
   43         }
   44     }
   45     //swap Perm[index] with Perm[i] and swap back
   46     for(int i = index; i &lt; Perm.size(); i++) {
   47         //swap
   48         tmp = Perm[i];
   49         Perm[i] = Perm[index];
   50         Perm[index] = tmp;
   51 
   52         Permute(index+1);
   53 
   54         //backtrack
   55         tmp = Perm[i];
   56         Perm[i] = Perm[index];
   57         Perm[index] = tmp;
   58     }
   59 }
   60 
   61 void Matrix::Choose(int index) {
   62     //n choose k permutation
   63     //people = non x
   64     //eid = teams
   65     if (E - E_ID.size() == 0) {
   66         cout &lt;&lt; E_ID[0];
   67         for (i = 1; i &lt; E_ID.size(); i++) cout &lt;&lt; &quot; &quot; &lt;&lt; E_ID[i];
   68         cout &lt;&lt; endl;
   69         return;
   70     }
   71 
   72     if ((E - E_ID.size()) &gt; Non_X.size() - index) return;
   73 
   74     E_ID.push_back(Non_X[index]);
   75     Choose(index+1, (E - E_ID.size())-1);
   76     E_ID.pop_back();
   77 
   78     Choose(index+1, (E - E_ID.size()));
   79 
   80     // call Print();
   81 }
  </code></pre>