# 代码具有特定于函数的变量，这些变量会在范围之外更改值

``````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;
}
``````

`wt`并且`tat`在中定义`findavgTime`（它们是使用非标准扩展名定义的，但这是一个单独的问题）。

`findavgTime`调用`findWaitingTime`和时`findTurnAroundTime`，它将这些变量传递给函数。函数没有在函数主体中定义它们-它们是通过函数参数在函数中定义的。由于`wt``tat`是数组，因此在调用`findWaitingTime`时，它们会衰减为指向各个数组的第一个元素的指针`findTurnAroundTime`因此，对这些函数内部变量的任何更改也都可见`findavgTime`

``````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];
}
``````

0 条评论