title: How to get rid of nested loops
body: <p>I have a set of vessels (data is given in external .txt file: vessel number, arriving time, service time, due time, penalty factor, waiting factor) which come to the berth and served one by one. I need to find the order of vessels with the least cost. I am using very basic approach - I generate all possible sequences and than I compare a cost for each. A vessel can be served only once, which means there can be no repetitions in the sequence.
  The cost of each newcoming vessel consists of waiting cost (the time it spent waiting in line) and penalty cost (the time it spent in berth after the due time).
  Currently, I have a program with 10 nested loops for the set of 10 vessels.
  The code</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;conio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;time.h&gt;
  
  
  
  void main()
  {
  
  FILE *f1;
  int n = 10;/*The number of vessels*/
  int arr[10];
  int count = 0, s = 1;
  int a, b, c, d, e, f, g, h, i, j;
  int t[10];
  int min;
  int time[10];
  
  
  struct vessel{
      int name;/*the name of the vessel*/
      int ar; /*arrival time*/
      int ser; /*service time*/
      int exp; /*due time*/
      int df; /*delay factor*/
      int wf; /*waiting factor*/
  };
  struct vessel x[10]; /* where number means the total number of vessels arriving*/
  
  for (i = 0; i &lt; n; i++)
  {
      s = s*(n-i) ;
  }
  printf("S is %d\n", s);/*The number of possible sequences. (n-1)! */
  
  
  f1 = fopen("sample1.txt", "r");/*Read data from file*/
  if (f1 == NULL){
      printf("Can not open the file\n");
      getch();
      return 0;
  }
  for (i = 0; i &lt; n; i++)
  {
      fscanf(f1, "%d, %d, %d, %d, %d, %d\n", &amp;x[i].name, &amp;x[i].ar, &amp;x[i].ser, &amp;x[i].exp, &amp;x[i].df, &amp;x[i].wf);
      arr[i] = x[i].name;
  }
  
  
  /*f2 = fopen("results.txt", "w");*/
  
  min = 9999999999;/*Set it to be big.*/
  
  for (a = 0; a &lt; n; a++)/*First step. Set time and cost.*/
  {
      t[a] = 0;/*As first vessel comes and goes, there are no penalt or waiting cost.*/
      time[a] = x[a].ar + x[a].ser;/*The time vessel arrives + the service time.*/
      for (b = 0; b &lt; n; b++)/*LOOP START.*/
      {
          if (b != a)/*One vessel can be served only once.*/
          {
              time[b] = 0;
              if (time[a] &gt; x[b].ar)/*If vessel arrived during service time of the previous one*/
              {
                  if ((time[a] + x[b].ser - x[b].exp) &gt; 0)/*If the service of current vessel ends after the due time.*/
                  {
                      t[b] = t[a] + (time[a] - x[b].ar)*x[b].wf + (time[a] + x[b].ser - x[b].exp)*x[b].df;
                  }
                  else/*If the service ends before the due time.*/
                  {
                      t[b] = t[a] + (time[a] - x[b].ar)*x[b].wf;
                  }
                  time[b] = time[a] + x[b].ser;/*Update time. The time when the service of current vessel ends.*/
              }
              else/*If vessel arrives after the service time of the previous one.*/
              {
                  t[b] = t[a] + 0;/*No penalty or waiting cost.*/
                  time[b] = x[b].ar + x[b].ser;/*Update time. Current vessel arrival time + its service time.*/
              }
              if (t[b] &lt; min)/*Check if current cost is smaller than found minimum cost. If yes, continue. If not, go to the start of the loop. LOOP END.*/
              {
                  for (c = 0; c &lt; n; c++)
                  {
                      if (c != b &amp;&amp; c != a)
                      {
                          time[c] = 0;
                          if (time[b] &gt; x[c].ar)
                          {
                              if ((time[b] + x[c].ser - x[c].exp) &gt; 0)
                              {
                                  t[c] = t[b] + (time[b] - x[c].ar)*x[c].wf + (time[b] + x[c].ser - x[c].exp)*x[c].df;
                              }
                              else
                              {
                                  t[c] = t[b] + (time[b] - x[c].ar)*x[c].wf;
                              }
                              time[c] = time[b] + x[c].ser;
                          }
                          else
                          {
                              t[c] = t[b] + 0;
                              time[c] = x[c].ar + x[c].ser;
                          }
                          if (t[c] &lt; min)
                          {
                              for (d = 0; d &lt; n; d++)
                              {
                                  if (d != c &amp;&amp; d != b &amp;&amp; d != a)
                                  {
                                      time[d] = 0;
                                      if (time[c] &gt; x[d].ar)
                                      {
                                          if ((time[c] + x[d].ser - x[d].exp) &gt; 0)
                                          {
                                              t[d] = t[c] + (time[c] - x[d].ar)*x[d].wf + (time[c] + x[d].ser - x[d].exp)*x[d].df;
                                          }
                                          else
                                          {
                                              t[d] = t[c] + (time[c] - x[d].ar)*x[d].wf;
                                          }
                                          time[d] = time[c] + x[d].ser;
                                      }
                                      else
                                      {
                                          t[d] = t[c] + 0;
                                          time[d] = x[d].ar + x[d].ser;
                                      }
                                      if (t[d] &lt; min)
                                      {
                                          for (e = 0; e &lt; n; e++)
                                          {
                                              if (e != d &amp;&amp; e != c &amp;&amp; e != b &amp;&amp; e != a)
                                              {
                                                  time[e] = 0;
                                                  if (time[d] &gt; x[e].ar)
                                                  {
                                                      if ((time[d] + x[e].ser - x[e].exp) &gt; 0)
                                                      {
                                                          t[e] = t[d] + (time[d] - x[e].ar)*x[e].wf + (time[d] + x[e].ser - x[e].exp)*x[e].df;
                                                      }
                                                      else
                                                      {
                                                          t[e] = t[d] + (time[d] - x[e].ar)*x[e].wf;
                                                      }
                                                      time[e] = time[d] + x[e].ser;
                                                  }
                                                  else
                                                  {
                                                      t[e] = t[d] + 0;
                                                      time[e] = x[e].ar + x[e].ser;
                                                  }
                                                  if (t[e] &lt; min)
                                                  {
                                                      for (f = 0; f &lt; n; f++)
                                                      {
                                                          if (f != e &amp;&amp; f != d &amp;&amp; f != c &amp;&amp; f != b &amp;&amp; f != a)
                                                          {
                                                              time[f] = 0;
                                                              if (time[e] &gt; x[f].ar)
                                                              {
                                                                  if ((time[e] + x[f].ser - x[f].exp) &gt; 0)
                                                                  {
                                                                      t[f] = t[e] + (time[e] - x[f].ar)*x[f].wf + (time[e] + x[f].ser - x[f].exp)*x[f].df;
                                                                  }
                                                                  else
                                                                  {
                                                                      t[f] = t[e] + (time[e] - x[f].ar)*x[f].wf;
                                                                  }
                                                                  time[f] = time[e] + x[f].ser;
                                                              }
                                                              else
                                                              {
                                                                  t[f] = t[e] + 0;
                                                                  time[f] = x[f].ar + x[f].ser;
                                                              }
                                                              if (t[f] &lt; min)
                                                              {
                                                                  for (g = 0; g &lt; n; g++)
                                                                  {
                                                                      if (g != a &amp;&amp; g != b &amp;&amp; g != c &amp;&amp; g != d &amp;&amp; g != e &amp;&amp; g != f)
                                                                      {
  
                                                                          time[g] = 0;
                                                                          if (time[f] &gt; x[g].ar)
                                                                          {
                                                                              if ((time[f] + x[g].ser - x[g].exp) &gt; 0)
                                                                              {
                                                                                  t[g] = t[f] + (time[f] - x[g].ar)*x[g].wf + (time[f] + x[g].ser - x[g].exp)*x[g].df;
                                                                              }
                                                                              else
                                                                              {
                                                                                  t[g] = t[f] + (time[f] - x[g].ar)*x[g].wf;
                                                                              }
                                                                              time[g] = time[f] + x[g].ser;
                                                                          }
                                                                          else
                                                                          {
                                                                              t[g] = t[f] + 0;
                                                                              time[g] = x[g].ar + x[g].ser;
                                                                          }
                                                                          if (t[g] &lt; min)
                                                                          {
                                                                              for (h = 0; h &lt; n; h++)
                                                                              {
                                                                                  if (h != a &amp;&amp; h != b &amp;&amp; h != c &amp;&amp; h != d &amp;&amp; h != e &amp;&amp; h != f &amp;&amp; h != g)
                                                                                  {
                                                                                      time[h] = 0;
                                                                                      if (time[g] &gt; x[h].ar)
                                                                                      {
                                                                                          if ((time[g] + x[h].ser - x[h].exp) &gt; 0)
                                                                                          {
                                                                                              t[h] = t[g] + (time[g] - x[h].ar)*x[h].wf + (time[g] + x[h].ser - x[h].exp)*x[h].df;
                                                                                          }
                                                                                          else
                                                                                          {
                                                                                              t[h] = t[g] + (time[g] - x[h].ar)*x[h].wf;
                                                                                          }
                                                                                          time[h] = time[g] + x[h].ser;
                                                                                      }
                                                                                      else
                                                                                      {
                                                                                          t[h] = t[g] + 0;
                                                                                          time[h] = x[h].ar + x[h].ser;
                                                                                      }
                                                                                      if (t[h] &lt; min)
                                                                                      {
                                                                                          for (i = 0; i &lt; n; i++)
                                                                                          {
                                                                                              if (i != a &amp;&amp; i != b &amp;&amp; i != c &amp;&amp; i != d &amp;&amp; i != e &amp;&amp; i != f &amp;&amp; i != g &amp;&amp; i != h)
                                                                                              {
                                                                                                  time[i] = 0;
                                                                                                  if (time[h] &gt; x[i].ar)
                                                                                                  {
                                                                                                      if ((time[h] + x[i].ser - x[i].exp) &gt; 0)
                                                                                                      {
                                                                                                          t[i] = t[h] + (time[h] - x[i].ar)*x[i].wf + (time[h] + x[i].ser - x[i].exp)*x[i].df;
                                                                                                      }
                                                                                                      else
                                                                                                      {
                                                                                                          t[i] = t[h] + (time[h] - x[i].ar)*x[i].wf;
                                                                                                      }
                                                                                                      time[i] = time[h] + x[i].ser;
                                                                                                  }
                                                                                                  else
                                                                                                  {
                                                                                                      t[i] = t[h] + 0;
                                                                                                      time[i] = x[i].ar + x[i].ser;
                                                                                                  }
                                                                                                  if (t[i] &lt; min)
                                                                                                  {
                                                                                                      for (j = 0; j &lt; n; j++)
                                                                                                      {
                                                                                                          if (j != a &amp;&amp; j != b &amp;&amp; j != c &amp;&amp; j != d &amp;&amp; j != e &amp;&amp; j != f &amp;&amp; j != g &amp;&amp; j != h &amp;&amp; j != i)
                                                                                                          {
                                                                                                              time[j] = 0;
                                                                                                              if (time[i] &gt; x[j].ar)
                                                                                                              {
                                                                                                                  if ((time[i] + x[j].ser - x[j].exp) &gt; 0)
                                                                                                                  {
                                                                                                                      t[j] = t[i] + (time[i] - x[j].ar)*x[j].wf + (time[i] + x[j].ser - x[j].exp)*x[j].df;
                                                                                                                  }
                                                                                                                  else
                                                                                                                  {
                                                                                                                      t[j] = t[i] + (time[i] - x[j].ar)*x[j].wf;
                                                                                                                  }
                                                                                                                  time[j] = time[i] + x[j].ser;
                                                                                                              }
                                                                                                              else
                                                                                                              {
                                                                                                                  t[j] = t[i] + 0;
                                                                                                                  time[j] = x[j].ar + x[j].ser;
                                                                                                              }
  
  
                                                                                                              if (t[j] &lt; min)
                                                                                                              {
                                                                                                                  min = t[j];
                                                                                                                  printf("%d\n", min);
                                                                                                                  /*printf("%d %d %d %d %d %d %d %d %d %d\n", t[a], t[b], t[c], t[d], t[e], t[f], t[g], t[h], t[i], t[j]);*/
                                                                                                                  arr[0] = a + 1; arr[1] = b + 1; arr[2] = c + 1; arr[3] = d + 1; arr[4] = e + 1; arr[5] = f + 1; arr[6] = g + 1; arr[7] = h + 1; arr[8] = i + 1; arr[9] = j + 1;
                                                                                                              }
  
                                                                                                              count++;/*How many sequences of n vessels were able to reach the end of loop.*/
  
                                                                                                          }
                                                                                                      }
                                                                                                  }
                                                                                              }
                                                                                          }
                                                                                      }
                                                                                  }
                                                                              }
                                                                          }
                                                                      }
                                                                  }
                                                              }
                                                          }
                                                      }
                                                  }
                                              }
                                          }
                                      }
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
  }
  
  printf("Final sequence is: ");
  for (i = 0; i &lt; n; i++)
  {
      printf("%d, ", arr[i]);
  }
  printf("\nFinal count is %d\n", count);
  printf("Final cost is %d\n", min);
  
  
  getch();
  return 0;
  }
  </code></pre>
  
  <p>This code works. BUT.
  It is long and nasty. I have no idea how to change it.
  The only part that matter for me are loops.
  The sample data (just in case)</p>
  
  <pre><code>1, 756, 207, 1148, 1160, 303
  2, 660, 243, 1105, 847, 344
  3, 1444, 225, 1857, 1006, 310
  4, 1554, 199, 1941, 1004, 326
  5, 1376, 186, 1737, 1112, 321
  6, 1396, 170, 1680, 1053, 247
  7, 1577, 158, 1917, 1005, 275
  8, 629, 218, 1026, 976, 289
  9, 807, 181, 1151, 1078, 299
  10, 779, 157, 1088, 804, 254
  </code></pre>
  
  <p>Thanks a lot for your time.</p>