gpt4 book ai didi

C - For 循环仅针对我修改的数组运行一次

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

首先,对可能的简单问题和糟糕的代码表示歉意,就像大多数发布问题的人一样,我对相关语言非常陌生。

代码用途:

创建一个模拟进程调度的程序。这个特定实例是模仿先来先服务调度。所讨论的数据集都是有序的(例如 PID 的 0 到 5 都按顺序到达)和无序的(例如 3 可以在 1 之前到达等)。

为了实现这一目标,我尝试在到达时间数组中找到最小的条目,以确保条目按顺序处理。因此,我在数组运行 for 循环时修改数组,更改最小值,这样它就不会运行两次(我相信这可能是我的问题,但我不确定)

我遇到问题的特定循环:

for (i = 1; i < processes; i++) {
atperm[position] = at[position];
smallestentry();
wt[position] = btt - at[position]; //wait time = total burst time - arrival time
btt += bt[position]; //total burst time = total burst time + burst time
awt += wt[position]; //average wait time = average wait time + wait time
tat[position] = wt[position] + bt[position]; //turn around time = wait time + burst time
atat += tat[position]; //average turn around time = average turn around time + turn around time
changeline(position);
}

完整代码:

#include <stdio.h>
#include <stdlib.h>

/*
* WT - Wait Time (TAT -BT)
* BT - Bust Time
* AT - Arrival Time
* TAT - Turn Around Time (CT - AT)
* AWT - Average Wait Time
* ATAT - Average Turn Around Time
*/

int wt[10], bt[10], at[10], atperm[10], tat[10], processes, smallest, i, position;
float awt, atat;

void input() {
printf("Enter number of processes:\n");
scanf("%d", &processes);
int i;
for (i = 0; i < processes; i++) {
printf("Enter burst time of process %d:", i + 1); //increase i by 1 but dont save
scanf("%d", &bt[i]);
printf("Enter arrival time of process %d:", i + 1);
scanf("%d", &at[i]);
}
}

void changeline(int position) {
at[position] = 999;

printf("AT\tBT\tWT\tTAT\tAT\n");
for (i = 0; i < processes; i++) {
printf("%3d\t%3d\t%3d\t%3d\t%3d\n", at[i], bt[i], wt[i], tat[i], atperm[i]);
}
}

void smallestentry() {
smallest = at[0];
for (i = 0; i < processes; i++) {
if (smallest > at[i]) {
smallest = at[i];
position = i;
}
}
}

void calculate() {
smallestentry();
wt[position] = 0; //sets the first entry in wait time array to equal 0
atat = tat[position] = bt[position]; //sets the first entries in atat and tat array to equal first bt entry
int btt = bt[position]; //to store total burst time sum
atperm[position] = at[position];
changeline(position);

for (i = 1; i < processes; i++) {
atperm[position] = at[position];
smallestentry();
wt[position] = btt - at[position]; //wait time = total burst time - arrival time
btt += bt[position]; //total burst time = total burst time + burst time
awt += wt[position]; //average wait time = average wait time + wait time
tat[position] = wt[position] + bt[position]; //turn around time = wait time + burst time
atat += tat[position]; //average turn around time = average turn around time + turn around time
changeline(position);
}
atat /= processes; // atat = atat / processes
awt /= processes; // awt = awt / processses
}

int main() {
printf("FCFS CPU Scheduling Algorithm\n");
input();
calculate();
}

最佳答案

你唯一的问题是你使用 i 作为全局变量。

int wt[10], bt[10], at[10], atperm[10], tat[10], processes, smallest, i, position;

在循环中间你调用了函数smallestentry(),它也使用了i:

void smallestentry() {
smallest = at[0];
for (i = 0; i < processes; i++) {
if (smallest > at[i]) {
smallest = at[i];
position = i;
}
}
}

此函数结束后,i 的值发生更改,因此循环仅运行一次。

我通过将 printf("%d",i); 放入循环中发现了问题。

您可以通过将 i 更改为函数内的局部变量来解决此问题。

(抱歉有任何错误的措辞,这是我的第一个答案。)

关于C - For 循环仅针对我修改的数组运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55071190/

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