gpt4 book ai didi

c++ - 代码具有特定于功能的变量,这些变量在范围之外更改值

转载 作者:行者123 更新时间:2023-11-30 02:16:06 25 4
gpt4 key购买 nike

这是FCFS cpu调度算法。

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 之类的变量如果在每个函数中都被清除,它们是如何连接的?(这是主要问题)

完整代码正在运行。

最佳答案

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

wttatfindavgTime 中定义. (它们是使用非标准扩展定义的,但这是一个单独的问题)。

findavgTime电话 findWaitingTimefindTurnAroundTime ,它将这些变量传递给函数。这些函数不在函数体中定义它们——它们是通过函数参数在函数中定义的。自 wttat是数组,当 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];
}

这不会改变程序的行为。

关于c++ - 代码具有特定于功能的变量,这些变量在范围之外更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55405192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com