title: SJF Non-preemptive scheduling algorithm
body: <p>I'm fresh on these scheduling algorithms. I've become comfortable with SJF non-preemptive and I understand it from a pen and paper Gantt chart perspective but not quite so from a programming perspective. My code is below and though it runs successfully my math is incorrect.  How can I fix it?</p>
  
  <p>TEST CASE
  Process  Burst  Arrival
  P1       20     0
  P2       3      3
  P3       2      5 </p>
  
  <p>Expected Outputs 
  Avg wait of 11.3
  Avg turnaround time of 19.6
  Avg response time of 10.6 </p>
  
  <p>Actual outputs
  avg wait of 8.3
  avg turnaround time of 6.6
  avg response time of 14</p>
  
  <pre><code>    #include &lt;iostream&gt;
      using namespace std;
  
      void SJF_NP(int n, int burst[], int arrival[], int throughput)
      {
        cout &lt;&lt; "Output for SJF_Non_Preemptive scheduling algorithm" &lt;&lt; endl;
  
        int i, j, temp, temp2;
        double tot, avgwait, avgturnaround, avgresponse, tp;
  
        //array instantiations                                                                                          
        int start[n], end[n], wait[n];
  
        //calculations                                                                                                  
        for(i=1;i&lt;=n;i++)
          {
            for(j=i+1;j&lt;=n;j++)
              {
                if (i&gt;=2 &amp;&amp; burst[i-1]&gt;burst[j-1])
                  {
                    temp = burst[i-1];
                    temp2 = arrival[i-1];
                    burst[i-1]=burst[j-1];
                    arrival[i-1]=arrival[j-1];
                    burst[j-1]=temp;
                    arrival[j-1]=temp2;
                  }
              }
            if(i==1)
              {
                start[0]=0;
                end[0]=burst[0];
                wait[0]=0;
              }
            else
              {
                start[i-1]=end[i-2];
                end[i-1]=start[i-1]+burst[i-1];
                wait[i-1]=start[i-1]+arrival[i-1];
              }
            //throughput                                                                                                
            if (start[i+1] &lt;= throughput)
              tp = i+1;
          }
  
        //output                                                                                                        
        cout &lt;&lt; "\n\nPROCESS \t BURST TIME\tARRIVAL TIME\tWAIT TIME\tSTART TIME\tEND TIME\n";
        for (i=0;i&lt;n;i++){
          cout &lt;&lt; "\nP[" &lt;&lt; i + 1 &lt;&lt; "]" &lt;&lt; "\t\t" &lt;&lt; burst[i] &lt;&lt; "\t\t" &lt;&lt; arrival[i] &lt;&lt; "\t\t" &lt;&lt; wait[i] &lt;&lt; "\t\t" &lt;&lt; start[i] &lt;&lt; "\t\t" &lt;&lt; end[i];
        }
        //avg wait time                                                                                                 
        for(i=1,tot=0;i&lt;n;i++){
          tot+=wait[i-1];
          avgwait=tot/n;
        }
        //avg turnaround time                                                                                           
        for(i=1,tot=0;i&lt;n;i++){
          tot+=end[i-1];
          avgturnaround=tot/n;
        }
        //avg response time                                                                                             
        for(i=1,tot=0;i&lt;n;i++){
          tot+=start[i-1];
          avgresponse=tot/n;
        }
        cout &lt;&lt; "\n\nAverage Wait Time: " &lt;&lt; avgwait;
        cout &lt;&lt; "\nAverage Response Time: " &lt;&lt; avgturnaround;
        cout &lt;&lt; "\nAverage Turnaround Time: " &lt;&lt; avgresponse;
        cout &lt;&lt; "\nThroughput for (" &lt;&lt; throughput &lt;&lt; "): " &lt;&lt; tp &lt;&lt; endl;
      }
  </code></pre>