Monday, May 24, 2010

How do I write a C program for 100 doors and 100 students using an array??

There are 100 doors (1-100) and 100 students (1-100). All the doors are closed. As a student numbered x goes to all the doors that are multiples of x and changes the status of the door (opens if closed and closes if opened), After all the students have completed their turns what are the door numbers that will be left open?

How do I write a C program for 100 doors and 100 students using an array??
You can make an array for the doors. Initial values are false.





//for loop that stops at 100 for students





//while (student times x multiple %26lt; 100)


// !element of the array


// x++;





//set x = 1 for the next loop iteration





// exit for loop and output array elements that are true
Reply:Hi there are lots of ways of doing this and Ive chosen an array of characters that is set to C=Closed or O=Open, as each Student tries to open all the doors.





A visual representation of the status of the doors can be printed and the O and C characters can be added up to tally the totals.





Good luck.





#define DOORS 100





// Define a char array of 100 DoorStatus values


// C = Door Closed


// 0 = Door Open


char DoorStatus[DOORS] = {0};





void main()


{


int DoorNum=0;


int StudentNum=0;


int DoorOpen=0;


int DoorClosed=0;





// Initialise the Door to Closed


for (DoorNum=0; DoorNum%26lt;DOORS; DoorNum++)


{


DoorStatus[DoorNum] = 'C';


}





// in the last element add a null char so we may print


// the char array as a string


DoorStatus[DOORS+1] = '\0';





// Outer loop: each student [1-100] gets a go at closing


// the doors they are allowed to


for (StudentNum=1; StudentNum%26lt;=100; StudentNum++)


{


// Inner loop: Each student tries all the doors and


// only the ones that are his multiples get changed


for (DoorNum=1; DoorNum%26lt;=DOORS; DoorNum++)


{


// Use mod to see if the Students number


// is a multiple of the Door number


if ((DoorNum % StudentNum) == 0)


{


// Students number is a multiple of the Door number


// change the Doors status from Closed to Open


// or Open to Closed


if (DoorStatus[DoorNum-1] == 'C')


{


DoorStatus[DoorNum-1] = 'O';


}


else


{


DoorStatus[DoorNum-1] = 'C';


}


}


}


// Compile this in if you want to see the status of the Doors


// after Student get a go on each Door


//printf("%s\n", DoorStatus);


}





// You now have an array that can be printed to see the


// status of all the doors


printf("\nVISUAL DOOR STATUS\n%s\n\n", DoorStatus);





// Loop again on the DoorStaus array and count the C


// and O characters resectively


for (DoorNum=0; DoorNum%26lt;DOORS; DoorNum++)


{


if (DoorStatus[DoorNum] == 'C')


{


DoorClosed++;


}


else if (DoorStatus[DoorNum] == 'O')


{


DoorOpen++;


}


}





printf("Doors: Open=%d, Closed=%d\n\n",


DoorOpen, DoorClosed);


}











Output should be as follows:





VISUAL DOOR STATUS


OCCOCCCCOCCCCCCOCCCCCCCCOCCCCCCCCCCOCC...





Doors: Open=10, Closed=90
Reply:This will print out an array of ones. You tell me why.


#include %26lt;stdio.h%26gt;





void change(int door[100], int place);





int main()


{


int students, i, doors[100];





students =1;


for (i=0;i%26lt;100;i+=1)


doors[i]=0;





for (i=0;i%26lt;100;i+=1)


{


if (students%(i+1)==0)


change(doors, i);


students+=1;


}


for (i=0;i%26lt;100;i+=1) printf("%d ", doors[i]);


return 0;


}





void change(int door[100], int place)


{


if (door[place]==1) door[place]=0;


else door[place]=1;


}

floral

No comments:

Post a Comment