0

This is FCFS cpu scheduling algorithm.

void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[])
{

// Calculating turnaround time by adding bt[i] + wt[i] 
    for (int i = 0; i < n; i++)
        tat[i] = bt[i] + wt[i];
}

// Function to calculate average waiting and turn-around 
// times. 
void findavgTime(int processes[], int n, int bt[], int at[])
{
    int wt[n], tat[n];

// Function to find waiting time of all processes 
    findWaitingTime(processes, n, bt, wt, at);

// Function to find turn around time for all processes 
    findTurnAroundTime(processes, n, bt, wt, tat);

// Display processes along with all details 
    cout << "Processes " << " Burst Time " << " Arrival Time "
            << " Waiting Time " << " Turn-Around Time "
            << " Completion Time \n";
    int total_wt = 0, total_tat = 0;
    for (int i = 0; i < n; i++)
    {
        total_wt = total_wt + wt[i];
        total_tat = total_tat + tat[i];
        int compl_time = tat[i] + at[i];
        cout << " " << i + 1 << "\t\t" << bt[i] << "\t\t" << at[i] << "\t\t"
                << wt[i] << "\t\t " << tat[i] << "\t\t " << compl_time << endl;
    }

    cout << "Average waiting time = " << (float) total_wt / (float) n;
    cout << "\nAverage turn around time = " << (float) total_tat / (float) n;
}

How are variables like wt and tat connected if they are decleared inside each function?(This is the main question)

full code is working.

user4581301
  • 33,082
  • 7
  • 33
  • 54
PROvishesh
  • 15
  • 4
  • 1
    Because `findavgTime` passes them to `findTurnAroundTime`? I'm not sure what the confusion is here. – NathanOliver Mar 28 '19 at 19:10
  • btw bad formatting and intendation makes it harder to read code and sometimes even prevent to graps basic things ;) – 463035818_is_not_an_ai Mar 28 '19 at 19:12
  • changes in findTurnAroundTime is reflecting in findavgTime. This is my confusion. They are decleared in there seperate functions – PROvishesh Mar 28 '19 at 19:13
  • Are you being puzzled by [Array Decay](https://stackoverflow.com/questions/1461432/what-is-array-decaying)? – user4581301 Mar 28 '19 at 19:16
  • `findTurnAroundTime` accepts the pointer to the array `tat` as an argument, If you look at the implementation it populates this array with values. So it is, in effect, space to store the results of the function. In `findavgTime`, these results are stored in the local variable `tat` (not to be confused with the argument `tat` to `findTurnAroundTime`). I can only presume that `findWaitingTime` does something similar to populate the `wt` local variable. – Wyck Mar 28 '19 at 19:18
  • Parameters that look like arrays (`int tat[[`) are actually pointers (`int * tat` is equivalent). – molbdnilo Mar 28 '19 at 19:21

1 Answers1

2

How are variables like wt and tat connected if they are decleared inside each function?

wt and tat are defined in findavgTime. (They are defined using a non-standard extension but that's a separate issue).

When findavgTime calls findWaitingTime and findTurnAroundTime, it passes those variables to the functions. The functions don't define them in their function body -- they are defined in the functions by way of function arguments. Since wt and tat are arrays, they decay to a pointer to the first elements of the respective arrays when findWaitingTime and findTurnAroundTime are called. Because of that, any changes made to the variables inside those functions are visible in findavgTime too.

You don't have to use the same variable names in the function arguments. You could use

void findTurnAroundTime(int processes[], int n, int bt[], int wt_here[], int tat_here[])
{    
    for (int i = 0; i < n; i++)
        tat_here[i] = bt[i] + wt_here[i];
}

That won't change the behavior of the program.

R Sahu
  • 204,454
  • 14
  • 159
  • 270