c - 数组会用在什么地方?

标签 c arrays loops simulation

Suppose that a fast-food restaurant sells salad and burger. There are two cashiers. With cashier 1, the number of seconds that it takes to complete an order of salad is uniformly distributed in {55,56,...,64,65}; and the number of seconds it takes to complete an order of burger is uniformly distributed in {111,112,...,,129,130}. With cashier 2, the number of seconds that it takes to complete an order of salad is uniformly distributed in {65,66,...,74,75}; and the number of seconds it takes to complete an order of burger is uniformly distributed in {121,122,...,,139,140}. Assume that the customers arrive at random times but has an average arrival rate of r customers per minute.

Consider two different scenarios.

• Customers wait in one line for service and, when either of two cashiers is available, the first customer in the line goes to the cashier and gets serviced. In this scenario, when a customer arrives at the restaurant, he either gets serviced if there is no line up, or waits at the end of the line.

• Customers wait in two lines, each for a cashier. The first customer in a line will get serviced if and only if the cashier for his line becomes available. In this scenario, when a customer arrives at the restaurant, he joins the shorter line. In addition, we impose the condition that if a customer joins a line, he will not move to the other line or to the other cashier when the other line becomes shorter or when the other cashier becomes free.

In both scenarios considered, a cashier will only start serving the next customer when the customer he is currently serving has received his ordered food. (That is the point we call “the customer’s order is completed”.)

... Simulation

For each of the two scenarios and for several choices of r (see later description), you are to simulate the customers arriving/waiting/getting service over a period of 3 hours, namely, from time 0 to time 180 minutes, where you assume that at time 0 there is no customer waiting and both cashiers are available; The entire period of 3 hours is to be divided into time slots each of 1 second duration. At each time slot, with r/60 probability, you make one new customer arrive, and with 1 − r/60 probability you make no new customer arrive. This should give rise to an average customer arrival rate of r customers/minute, and the arrival model will be reasonably close to what is described above. In each time slot, you will make your program handle whatever necessary.

... Objectives and Deliverables

You need to write a program to investigate the following. For each of the two scenarios and for each r, you are to divide the three-hour simulated period into 10-minute periods, and for every customer arriving during period i (i ∈ {1,2,...,18}), compute the overall waiting time of the customer (namely, from the time he arrives at the restaurant to the time when his order is completed. You need to print for each i the average waiting time for the customers arriving during period i. Note that if a customer arriving in period i has not been served within the three-hour simulated period, then his waiting time is not known. So the average waiting time for customers arriving in this period cannot be computed. In that case, simply print “not available” as the average waiting time for that period.

所以,这个程序处理小时、分钟和秒。

最好是这样制作一个三维数组:

time[3][60][60]

总共三个小时,其中60分钟,其中60秒。

或者,我想我应该用这个结构创建一个“for-loop”:

for (time=0;t<10800;t++)

此循环的每次迭代都将代表三小时模拟中的一秒(3hx60mx60s=10800 秒)。

伙计们,我走在正确的道路上吗?哪种方法更靠谱。还有其他对该程序至关重要的数组吗?

一如既往地感谢您的帮助!

最佳答案

你的内部时间表示以秒为单位几乎总是最好的;使用 for 循环比使用三维数组要容易得多。一个很好的约定是将其写为

MAX_SECONDS = 3 * 60 * 60
for (t=0;t<MAX_SECONDS;t++)

这个项目要研究的数据结构是一个队列。这可以使用数组来实现,但需要一些额外的工作。

关于c - 数组会用在什么地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13315009/

相关文章:

c - Gcc 和 g++ 在写时不同()

c - 是否可以在字符串化宏之前处理数学计算?

c - 使用 Switch case 删除 C 中字符串中的标点符号

python - 如何用python中文件中的数据填充数组

python - 循环遍历 pandas 数据框,将公式应用于每个值

c - 在函数中传递矩阵 (C)

c - 如何将无符号的磁力计数据转换为度数

javascript - 如何使用 Angular 5 中的 ngrx 状态仅更新一个对象属性或数组中的多个对象属性?

java - 不在循环中多次生成相同的随机数

Python: '_io.TextIOWrapper' 类型的对象没有 len()