title: Organizing Simulation Results
body: <p>I wrote a queue program based on a checkout simulation taking data from a text file (sim.txt). I got everything to work in my code except for one thing. I want to organize each sim result by numbers. </p>
  
  <p><strong>sim.txt</strong></p>
  
  <pre><code>2000
  0.10
  5
  15
  5
  20
  10000
  0.05
  5
  15
  5
  18
  2000
  0.20
  5
  20
  10
  10
  2000
  0.20
  1
  5
  10
  25
  20000
  0.50
  1
  2
  50
  10
  </code></pre>
  
  <p><strong>Here is an example output</strong></p>
  
  <pre><code>Simulation Results #1
  
         // result #1 output is here...
  
  Simulation Results #2
  
         // result #2 output is here...
  
  Simulation Results #3
  
         // result #3 output is here...
  
  Simulation Results #4
  
         // result #4 output is here...
  
  Simulation Results #5
  
         // result #5 output is here...
  
  </code></pre>
  
  <p>I tried implementing a for loop to achieve this, but it didn't work as expected. This problem occurs for each simulation result. </p>
  
  <pre><code>Simulation Results #1
  Simulation Results #2
  Simulation Results #3
  Simulation Results #4
  Simulation Results #5
  
           // result #1 outputs here...
  </code></pre>
  
  <p><strong>Here is my code to get a better look what I'm doing</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;queue&gt;
  #include &lt;fstream&gt;
  #include &lt;ctime&gt;
  
  using namespace std;
  
  // Input parameters
  #define SIMULATION_TIME     0
  #define ARRIVAL_RATE        1
  #define MIN_SERVICE_TIME    2
  #define MAX_SERVICE_TIME    3
  #define MAX_LINE_SIZE       4
  #define ANGRY_THRESHOLD     5
  #define PARAMS_MAX          6
  #define FILENAME            "sim.txt"
  
  // Or like this:
  // const int SIMULATION_TIME = 0;
  
  // Or make 6 variables:
  // double SIMULATION_TIME = 0;
  
  // Counters -- indexes into variable 'counters' below
  #define CUSTOMERS_SERVICED  0
  #define CUSTOMERS_LEAVING   1
  #define AVERAGE_WAIT        2
  #define AVERAGE_LINE        3
  #define ANGRY_CUSTOMERS     4
  #define COUNTERS_MAX        5
  
  // Holds the current simulation parameters
  double parameters[PARAMS_MAX];
  double counters[COUNTERS_MAX];
  
  // This is an example debug macro you can use to print
  // things each time through the loop
  #define DEBUGGING_ENABLED 1
  #ifdef DEBUGGING_ENABLED
  #define DEBUG(x) do { \
          cerr &lt;&lt; __FILE__ &lt;&lt; ": " &lt;&lt; __LINE__ &lt;&lt; ": " &lt;&lt; x &lt;&lt; endl; \
      } while (0)
  #else
  #define DEBUG(x)
  #endif // DEBUGGING_ENABLED
  
  // Return the service interval, between 'min'
  // and 'max'.
  int randomInt(int min, int max)
  {
      return (rand() % (max - min) + min);
  }
  // Returns TRUE if a customer arrived during this minute
  bool randomChance(double prob)
  {
      double rv = rand() / (double(RAND_MAX) + 1);
      return (rv &lt; prob);
  }
  
  // Read the next simulation from the file.  Return
  // TRUE if one could be read, FALSE otherwise (eof).
  bool readNextSim(fstream &amp;f, double parameters[])
  {
      for (int i = 0; i &lt; PARAMS_MAX; i++)
      {
          string tmp;
          getline(f, tmp);
          if (f.eof())
              return false;
          // Read in the next parameter
          parameters[i] = atof(tmp.c_str());
      }
  
      for (int i = 0; i &lt; 5; i++)
      cout &lt;&lt;"Simulation Results #" &lt;&lt; i + 1 &lt;&lt; endl;
      cout &lt;&lt; "---------------------" &lt;&lt; endl;
  
      cout &lt;&lt; "\t Overall simulation time: " &lt;&lt; "\t" &lt;&lt; parameters[SIMULATION_TIME] &lt;&lt; endl;
      cout &lt;&lt; "\t Arrival rate: " &lt;&lt; "\t\t\t" &lt;&lt; parameters[ARRIVAL_RATE] &lt;&lt; endl;
      cout &lt;&lt; "\t Minimum service time: " &lt;&lt; "\t\t" &lt;&lt; parameters[MIN_SERVICE_TIME] &lt;&lt; endl;
      cout &lt;&lt; "\t Maximum service time: " &lt;&lt; "\t\t" &lt;&lt; parameters[MAX_SERVICE_TIME] &lt;&lt; endl;
      cout &lt;&lt; "\t Maximum line size: " &lt;&lt; "\t\t" &lt;&lt; parameters[MAX_LINE_SIZE] &lt;&lt; endl;
      cout &lt;&lt; "\t Angry threshold: " &lt;&lt; "\t\t" &lt;&lt; parameters[ANGRY_THRESHOLD] &lt;&lt; endl;
  
      return true;
  }
  
  int main()
  {
      fstream f(FILENAME);
  
      // Seed the random number generator here
      srand(time(0));
  
      if (!f.good())
      {
          cout &lt;&lt; "Invalid file." &lt;&lt; endl;
          return -1;
      }
      while (readNextSim(f, parameters))
      {
          // Run the next simulation
          queue&lt;int&gt; line;
  
          for (int i = 0; i &lt; COUNTERS_MAX; i++)
              counters[i] = 0;
  
          // or:
          // memset(counters, 0, COUNTERS_MAX * sizeof(double));
  
          //int customersLeavingLineFull = 0;
          int simTime = 0;
          int currentCustomer = -1;
  
          // Each time through this loop represents 1 minute passing.
          // There needs to be code to handle everything that can happen
          // in 1 minute:
          //  - Customer arriving (yes/no?)
          //  - Is the current customer finished
          //  - Possibly process the next person in line
          //  - Calculate simulation statistics along the way
          while (simTime++ &lt; parameters[SIMULATION_TIME])
          {
              // One iteration of the loop represents one
              // minute of simulation time.
  
              // Check to see if a customer arrived
              // (if so, process, also see if the line is full)
              bool arrived = randomChance(parameters[ARRIVAL_RATE]);
              if (arrived)
              {
                  // A customer arrived in this minute
                  if (currentCustomer == -1)
                  {
                      // No customer is currently at the cashier
                      int serviceTime = randomInt(parameters[MIN_SERVICE_TIME],
                                                  parameters[MAX_SERVICE_TIME]);
  
                      currentCustomer = simTime + serviceTime;
                  }
                  else
                  {
                      if (line.size() == parameters[MAX_LINE_SIZE])
                      {
                          // Count this customer as leaving because the line is too
                          // full
                          counters[CUSTOMERS_LEAVING]++;
                      }
                      else
                      {
                          line.push(simTime);
                      }
                  }
              }
  
              counters[AVERAGE_LINE] += line.size();
  
              // Check to see if the current customer is done
              // at the cashier.  Also check if there is no customer
              // at the cashier (in which the next customer goes to the
              // cashier).
              if (simTime == currentCustomer)
              {
                  if (!line.empty())
                  {
                      int nextCustomerTimestamp = line.front();
                      int waitingTime = simTime - nextCustomerTimestamp;
  
                      // We need to include this in the average waiting times
                      if (waitingTime &gt;= parameters[ANGRY_THRESHOLD])
                          counters[ANGRY_CUSTOMERS]++;
  
                      counters[AVERAGE_WAIT] += waitingTime;
  
                      // Set currentCustomer to the time when that customer
                      // will be done.  Need to call randomInt().
                      int serviceTime = randomInt(parameters[MIN_SERVICE_TIME],
                                                  parameters[MAX_SERVICE_TIME]);
  
                      // This will give us a timestamp of when the current customer will
                      // be done.
                      currentCustomer = simTime + serviceTime;
  
                      line.pop();
                      counters[CUSTOMERS_SERVICED]++;
                  }
                  else
                  {
                      // The line is empty
                      counters[CUSTOMERS_SERVICED]++;
                      currentCustomer = -1;
                  }
              }
          }
          // Print a summary of the simulation:
          // counters
          counters[AVERAGE_WAIT] /= counters[CUSTOMERS_SERVICED];
          counters[AVERAGE_LINE] /= parameters[SIMULATION_TIME];
  
          cout &lt;&lt; endl;
          cout &lt;&lt; "\t Customers serviced: " &lt;&lt; "\t\t" &lt;&lt; counters[CUSTOMERS_SERVICED] &lt;&lt; endl;
          cout &lt;&lt; "\t Customers leaving: " &lt;&lt; "\t\t" &lt;&lt; counters[CUSTOMERS_LEAVING] &lt;&lt; endl;
          cout &lt;&lt; "\t Average time spent in line: " &lt;&lt; "\t" &lt;&lt; counters[AVERAGE_WAIT] &lt;&lt; endl;
          cout &lt;&lt; "\t Average line length: " &lt;&lt; "\t\t" &lt;&lt; counters[AVERAGE_LINE] &lt;&lt; endl;
          cout &lt;&lt; "\t Angry customers: " &lt;&lt; "\t\t" &lt;&lt; counters[ANGRY_CUSTOMERS] &lt;&lt; endl;
          cout &lt;&lt; endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Any advice on how to achieve the example output above would be appreciated!</p>