title: Cartesian Product of n-sets in C
body: <p>I am writing a C program that should print the Cartesian product of <em>n</em> sets, where the elements of each set are the line of text in one file, and the names of the <em>n</em> files are passed as command-line arguments. So far I managed to read every line into matrix of strings. However I cannot wrap my head around how to write the algorithm for printing the product.</p>
  <p>Here is what I have so far:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #define LSIZ 128
  #define RSIZ 10
  int main(int argc, char *argv[])
  {
      char input[LSIZ];
      int n = argc;
      char *sets[argc - 1][RSIZ];
      int i = 0;
      int j = 0;
      int y = 1;
      FILE *file = NULL;
      if (argc == 1)
      {
          printf(&quot;nofiles&quot;);
      }
      else
      {
          while (--argc &gt; 0)
          {
              if ((file = fopen(argv[y], &quot;r&quot;)) == NULL)
              {
                  printf(&quot;cat: failed to open %s\n&quot;, *argv);
                  return 1;
              }
              else
              {
  
                  for (j = 0; j &lt; RSIZ &amp;&amp; fgets(input, sizeof(input), file); ++j)
                  {
                      int lineLen = strlen(input) + 1;
                      sets[i][j] = strncpy(malloc(lineLen), input, lineLen);
                  }
  
                  fclose(file);
                  j = 0;
              }
              i++;
              y++;
          }
      }
      return 0;
   }
  </code></pre>