Help required for C program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • majestic
    New Member
    • Feb 2008
    • 7

    Help required for C program

    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 abpve exercise.

    Your help would be very much appreciated.
    Thanks
  • sanctus
    New Member
    • Mar 2007
    • 84

    #2
    What about making a while-for combination? i being the bulb where you start the iteration (i=2 first round, then i=3 etc) and n the bulb increased according to n where you have to switch. I would imagine it looks something like this
    [code=c]
    code removed
    [/code]
    But it won't work yet completely this way I guess, but it should help you get started...

    Originally posted by majestic
    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 abpve exercise.

    Your help would be very much appreciated.
    Thanks
    Last edited by sicarie; Feb 28 '08, 02:21 PM. Reason: Posting Guidelines

    Comment

    • sanctus
      New Member
      • Mar 2007
      • 84

      #3
      Sorry for posting the code...

      Majestic if you post how you think to do it there will be surely people who help you see if your idea works...

      Comment

      • majestic
        New Member
        • Feb 2008
        • 7

        #4
        Hi, This is the code that i've written so far, I'm having some problems configuring the for loops..... can anyone help?? here is the code

        [CODE=c]#include <stdio.h>

        int main(){
        int number[501];
        int i,bulb,incr,j;
        bulb=1;


        bulb++;
        incr=bulb;
        for(i=bulb;i<=5 01;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;
        }[/CODE]
        Last edited by Ganon11; Mar 14 '08, 11:57 AM. Reason: Please use the [CODE] tags provided.

        Comment

        Working...