-3

I'm trying to implement the FCFSFirst in, first out (FIFO), also known as first come, first served (FCFS) algorithm in C++.

#include<bits/stdc++.h>

using namespace std;

class Process{
public:
  static int count;
  static int cycle_count;
  int id;
  int at;
  int wt;
  int tat;
  int bt;

  Process(){
    id = count++;
  }

  void compute(){
    if (cycle_count < at){
      cycle_count = at;
    }
    cycle_count += bt;
    tat = cycle_count;
    wt = tat - bt;
  }

};

float average_wt(int n, vector<Process> v){
  float avg = 0;
  for (Process i: v){
    avg += i.wt;
  }
  avg /= n;
  return avg;
}

float average_tat(int n, vector<Process> v){
  float avg = 0;
  for (int i = 0; i < n; ++i){
    avg += v[i].tat;
  }
  avg /= n;
  return avg;
}

void print(int n, vector<Process> v){
  cout << "Process\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time" << endl;
  cout << "-------\t----------\t------------\t------------\t---------------" << endl;
  for(Process i: v){
    i.compute();
    cout << i.id << "\t\t\t" << i.bt << "\t\t\t" << i.at << "\t\t\t\t" << i.wt << "\t\t\t\t" << i.tat << endl;
  }
  cout << "Average Waiting Time: " << average_wt(n, v) << endl;
  cout << "Average Turnaround Time: " << average_tat(n, v) << endl;
  cout << endl;
}


bool sort_on_at(Process a, Process b){
  return a.at < b.at;
}

int Process::count = 0;
int Process::cycle_count = 0;

int main(int argc, char const *argv[]) {


  int n;
  cout << "Enter the number of processes: ";
  cin >> n;
  vector<Process> process(n);
  for(int i = 0; i < n; ++i){
    cout << "Process " << i << ":" << endl;
    cout << "\tArrival Time: ";
    cin >> process[i].at;
    cout << "\tBurst Time: ";
    cin >> process[i].bt;
  }

  sort(process.begin(), process.end(), sort_on_at);
  print(n, process);

  return 0;
}

The issues is that this code correctly print the waiting and turnaround time for the individual processes, but gives the average waiting and turnaround times as 0.

The expected output:

Enter the number of processes: 3
Process 0:
    Arrival Time: 0
    Burst Time: 24
Process 1:
    Arrival Time: 0
    Burst Time: 3
Process 2:
    Arrival Time: 0
    Burst Time: 3
Process Burst Time  Arrival Time    Waiting Time    Turnaround Time
------- ----------  ------------    ------------    ---------------
0           24          0               0               24
1           3           0               24              27
2           3           0               27              30
Average Waiting Time: 17
Average Turnaround Time: 27

Actual output:

Enter the number of processes: 3
Process 0:
    Arrival Time: 0
    Burst Time: 24
Process 1:
    Arrival Time: 0
    Burst Time: 3
Process 2:
    Arrival Time: 0
    Burst Time: 3
Process Burst Time  Arrival Time    Waiting Time    Turnaround Time
------- ----------  ------------    ------------    ---------------
0           24          0               0               24
1           3           0               24              27
2           3           0               27              30
Average Waiting Time: 0
Average Turnaround Time: 0

I did try some debugging, and found that the compute() function does change the values,(because it prints the correct values for the individual processes) but for some reason the wt and tat values are 0 for all the processes in the average_tat() and average_wt().

Please let me know if I can make anything more clear.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
nullptr
  • 3,701
  • 2
  • 16
  • 40
  • 1
    Unrelated to your problem -- https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – R Sahu Aug 24 '19 at 05:56
  • @RSahu I know I shouldn't use it, but I'm lazy, lol – nullptr Aug 24 '19 at 05:57
  • 1
    One of the nice things about a vector is that you can always get it's size using `v.size()`. So you don't need to have all the functions where you pass in a vector and it's size, you just need the vector. – john Aug 24 '19 at 05:59
  • @john I know about the `v.size()`, but this code was written using arrays I'm trying to refactor the code, and then I ran into this problem. – nullptr Aug 24 '19 at 06:03
  • 1
    @nullptr OK, refactoring is good, just make sure you complete it by removing the unnecessary parameters. – john Aug 24 '19 at 06:05
  • Revert the last step after which this misbehaviour started. You obviously broke something while refactoring. That said, your question is off-topic here, because you'd have to extract a [mcve]. In case you didn't, also take the [tour] and read [ask]. – Ulrich Eckhardt Aug 24 '19 at 06:10

1 Answers1

3

wt is calculated in the compute method, but this operates on a copy of Process:

for(Process i: v){  // a copy is made
  i.compute();

You need to use a value reference (&i in this case) to work on original Process stored in the vector, then wt will be saved.

for(Process& i: v){
  i.compute();
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
rafix07
  • 20,001
  • 3
  • 20
  • 33
  • It works. I had no idea looping like this made a copy. What if I use the traditional loop. `for(int i = 0; i < n; ++i)` and call `v[i].compute()`. Does that work on the same object? – nullptr Aug 24 '19 at 06:00
  • As an addition to the above, it's really a very bad idea to be doing computation in a function called `print`. Don't surprise the readers or users of your code by putting functionality in places they don't expect. – john Aug 24 '19 at 06:01
  • @nullptr Yes it does because `v[i]` returns a reference. – john Aug 24 '19 at 06:02
  • @nullptr Traditional loop will be working fine. You can see [reference](https://en.cppreference.com/w/cpp/language/range-for) about for-range loop to get more details how it works. – rafix07 Aug 24 '19 at 06:03