Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a doozy of a conundrum. Conceptually, I think I know what I need to do. Code-wise, I'm not so sure.

I want to go through the AVAIL_NURSE_W1[] array (which holds the number that corresponds to which nurse is available on Week_1), generate a random number to decide on a slot in that array (if the slot holds a zero value, then generate another random number and try again), take that number (which is a nurse), and put it into the MONDAY[] array.

Related-code:

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
}

My Question:
How do I take care of getting the available nurses from the AVAIL_NURSE_W1[] array, generate a random number which decides which slot to take a value from, takes that value, stores it in a new array MONDAY[]; if the value in the AVAIL_NURSE_W1[] array is a ZERO, then repeat the above process until it's selected a non-zero value; after I've selected a value, I will change the selected value to a ZERO and then go through the loop again.

Desired result
The MONDAY[] array should contain five non-zero integers. No repeats.

So far, it seems the while loop condition never changes.

Let me know if there is anything else that needs saying. I hope I've given enough information.

share|improve this question
    
What is happening in the segment commented scans in first week? Where is select declared and where is it filled with data? –  dreamlax 2 days ago
    
@dreamlax I left that code out because last time I posted a question regarding this, it was put "on-hold, because it was off-topic for having too much information." –  Nxt3 yesterday

1 Answer 1

up vote 1 down vote accepted

Here you go :

#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
}
share|improve this answer
    
I'm trying to have it so a user can input values of nurses to disregard for scheduling purposes. Could you please look at the code here: Pastebin - Nurse Schedule and let me know what further changes would be necessary? I can't figure it out. –  Nxt3 21 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.