majestic0431

Hello, Is it possible that someone can help me with the following program?

There are 500 light bulbs (numbered 1 to 500) arranged in a row. Initially they are all OFF. Starting with bulb 2, all even numbered bulbs are turned ON. Next, starting with bulb 3, and visiting every third bulb, it is turned ON if it is OFF, and it is turned OFF if it is ON. This procedure is repeated for every fourth bulb, then every fifth bulb, and so on up to the 500th bulb. Write a C program to determine which bulbs are off at the end of the above exercise.

Your help would be very much appreciated.
Thanks
I have started writing some code for the problem but i'm having some problems.
here is what i have so far:

``````#include <stdio.h>
int main(){
int number[501];
int i,bulb,incr,j;
number[501]=0;
bulb=1;

bulb++;
incr=bulb;
for(i=bulb;i<=501;i=i+incr)
{
for(j=1;j<=501;j++){
if(number[j]==0){
number[j]=1;}
else
{number[j]=0;}
}
}
printf("\nThe positions of Bulbs  that will be off are:\n\n ");

for(;number[i]==0;number[i]++){
printf("%d\n",i+1);
}
system("PAUSE");
return 0;``````

VernonDozier 2,218

I think you have your loops inverted here. The outer loop control variable should be incremented by 1 each pass through the outer loop. The amount the inner loop increments should vary. You have neither varying. You increase i by 2 each time since incr never changes, and you increase j each time by 1.

``````for(i=bulb;i<=501;i=i+incr)  // should increment by 1 each trip through loop
{
for(j=1;j<=501;j++){  // amount incremented will vary according to i value
if(number[j]==0){
number[j]=1;}
else
{number[j]=0;}
}
}``````

The display code below doesn't look right to me. You are going beyond the array boundary with your value of i so you're going to get either a seg fault or jibberish. It looks to me like you are going through that loop 0 or 1 times instead of 500, which is what I think you want.

``````for(;number[i]==0;number[i]++){
printf("%d\n",i+1);
}``````

edit: You also have a brackets problem. I'm not sure where you intended your loops to end, so maybe I misinterpreted your logic in my comments above. I miscounted brackets in my comment. Those bracket problems are easier to spot if you use consistent formatting, indentation, and code tags.

Hello ,
I have seen your problem & itry to solv it out .Can you help me to get sample program on TSR (terminate & stay resident) & the usage of keep() function in turbo c,c++.
thanks

Be in contact at email id email snipped

jephthah 1,888

I have seen your problem & itry to solv it out .

i seriously doubt that.