【问题标题】:Loop to create random numbers, read from existing array, then create new array -- Stuck in While-Loop循环创建随机数,从现有数组中读取,然后创建新数组——卡在 While-Loop 中
【发布时间】:2014-09-24 02:24:22
【问题描述】:

我有一个难题。从概念上讲,我想我知道我需要做什么。代码方面,我不太确定。

我想通过AVAIL_NURSE_W1[] 数组(其中包含对应于在 Week_1 有空的护士的数字),生成一个随机数来决定该数组中的一个插槽(如果插槽包含零值,然后生成另一个随机数并重试),取那个数字(这是一个护士),并将其放入MONDAY[] 数组中。

相关代码:

int main() {

int randomNurse();
srand(time_t(NULL));

/*0 = Denise, 1 = Inja, 2 = Jane, 3 = Karen, 4 = Maggie, 5 = Margaret, 6 = MJ, 7 = Queen, 8 = Sherri*/


/*-----WEEKLY_ASSIGNMENT-----*/

int AVAIL_NURSE_W1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //holds the numerical values of the nurses that CAN work each week

/*scans in first week*/
for (int column = 0; column < 4; column++) {
    for (int num = 0; num < 9; num++) {
        if (AVAIL_NURSE_W1[num] == select[0][column])
            AVAIL_NURSE_W1[num] = 0;
    }
}


/*-----MONDAY-----*/
int MONDAY[5]; //days of the week, number of jobs on a given day
for (int e = 0; e < 5; e++) { //loop for positions on monday
    while (MONDAY[e] == 0) {
        int temp_assign = randomNurse();
        if (AVAIL_NURSE_W1[temp_assign] != 0) { //if the nurse IS available...
            MONDAY[e] = AVAIL_NURSE_W1[temp_assign];
            AVAIL_NURSE_W1[temp_assign] = 0; //we don't want to repeat nurses
        } else {
            continue;
        }
    }
} 

return 0;
}

/*function to generate random nurse*/
int randomNurse() {

return rand() % 9; //random number 0-8, to pick nurse
}

我的问题:
我如何处理从AVAIL_NURSE_W1[] 数组中获取可用护士,生成一个随机数来决定从哪个插槽获取值,获取该值,将其存储在新数组MONDAY[] 中;如果AVAIL_NURSE_W1[]数组中的值是零,则重复上述过程,直到选择一个非零值;在我选择了一个值后,我会将选择的值更改为零,然后再次循环。

想要的结果
MONDAY[] 数组应包含五个非零整数。没有重复。

到目前为止,while 循环条件似乎从未改变。

如果还有什么需要说的,请告诉我。我希望我已经提供了足够的信息。

【问题讨论】:

  • 评论scans in first week的部分发生了什么? select 在哪里声明,在哪里填充数据?
  • @dreamlax 我忽略了那个代码,因为上次我发布了一个关于这个的问题,它被“搁置了,因为它有太多的信息是离题的。”

标签: c for-loop random while-loop


【解决方案1】:

给你:

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

const char names[9][10] = {"Denise", "Inja", "Jane", "Karen", "Maggie", "Margaret",       "MJ", "Queen", "Sherri"};
const char days[5][10] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
int randomNurse();

int main() {


srand(time(NULL));
int day, e, pos, candidate;
int i,j;

/*0 = Denise, 1 = Inja, 2 = Jane, 3 = Karen, 4 = Maggie, 5 = Margaret, 6 = MJ, 7 = Queen, 8 = Sherri*/


/*-----WEEKLY_ASSIGNMENT-----*/

int AVAIL_NURSE_W1[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1}; //holds the status of each nurse, 1:available 0:unavailable

int pos_per_day[5] = {2, 5, 7, 4, 3}; // number of needed nurses per day, Monday:2 nurses, tuesday: 5 nurses ...

int select[5][9]; // the selected nurses per day 
for(i=0; i<5; i++)
for(j=0; j<9;j++) select[i][j] = -1; // initialize to -1 which means no nurse is selected


// fill all the days of week 

for (day = 0; day<5; day++)   // for every day
{
    for(pos = 0; pos<pos_per_day[day]; pos++ ) // for every position needed that day
    {
        do
        {
            candidate = randomNurse();
        }while(!AVAIL_NURSE_W1[candidate]); // look for available nurse

        AVAIL_NURSE_W1[candidate] = 0;  // change her status to not available
        select[day][pos] = candidate;   // fill the output array with appropriate nurse
    }
    for(i=0; i< 9; i++)
    {
        AVAIL_NURSE_W1[i] = 1; // initialize the nurses status for next day use
    }
}

for(i=0; i<5; i++) // display 
{
    printf("%-10s: ", days[i]);
    for(j=0; j<9;j++) 
    {
        if(select[i][j] != -1) printf("%-10s ", names[select[i][j]]);
    }
    printf("\n");
}


return 0;
}

/*function to generate random nurse*/
int randomNurse() {

return rand() % 9; //random number 0-8, to pick nurse
}

【讨论】:

  • 我正在尝试使用它,以便用户可以输入护士的值以忽略调度目的。您能否在这里查看代码:Pastebin - Nurse Schedule 并让我知道需要进行哪些进一步的更改?我想不通。
  • 乍一看,我会说avail_nurses[i] = 1; //initialize the nurses status for next day use这行会初始化为0个slackers,考虑一下。我还想向您推荐一种更有效的使用标志的方法,它应该比数组更好/更容易处理
猜你喜欢
  • 1970-01-01
  • 2015-08-22
  • 2017-11-10
  • 2014-10-28
  • 1970-01-01
  • 1970-01-01
  • 2010-10-13
  • 2013-04-28
  • 2011-03-05
相关资源
最近更新 更多