-8

I have written the following code. But it doesn't run until the final printf. Plus if the validation I have set fails to pass, it prints a result I can't explain.

#include <stdio.h>

int main(void)
{
    int k, j, z, i, n;
    int l[30];

    // Setting initial values 0 (0=off 1=on)
    for (n=0; n<30; n++)
    {
        l[n] = 0;
    }

    // Employee number
    printf("give employee number\n");
    scanf("%d", &k);

    // Validation of k
    if (k<0 || k>30)
    {
        printf("wrong input");
    }
    else
        // Lamp status change
        for (i=1; i=k; i=i+1)
        {
            for (z=i; z=30; z=2*z)
            {
                if (l[z] = 0)
                   l[z] = 1;
                else
                    l[z] = 0;
            }
        }

        for (j=0; j<30; j++);
        {
            printf("lamp[%d] is: %d\n", j+1, l[j]);
        }

    return(0);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
S.Michael
  • 21
  • 2
  • 1
    Welcome to Stack Overflow! Please show your research/debugging effort so far. Please read [Ask] page first. – Sourav Ghosh Apr 03 '17 at 12:14
  • 2
    We can't help if we don't know what help you need. Do you have an algorithm worked out? If not, ask about what you're stuck on. If so, provide it and show us how you're trying to implement it. – David Schwartz Apr 03 '17 at 12:16
  • You must show evidence of work before we can do anything to assist you. – Manav Dubey Apr 03 '17 at 12:19
  • start with `bool lamps[31] = {false};` (`bool` in `#include `) or `uint32_t lamps = 0;` and Bit manipulation. (`uint32_t` in `#include `) – BLUEPIXY Apr 03 '17 at 12:23
  • I edited my question since it was very general. I am sorry – S.Michael Apr 03 '17 at 19:29
  • 1
    Thanks for the edits @S.Michael, I have upvoted this to try to draw attention to it. At a guess, I would say that the `else` on L19 should have its own set of braces - presently that will only enclose the following (nested) `for` loop, but your indentation implies you want it to cover the last `for` as well. – halfer Apr 13 '17 at 19:22
  • It seems likely that the last 'for' loop is intended to be part of the `else`. – Peter Mortensen May 22 '22 at 13:58

1 Answers1

2

I suggest that you go work a little more your C basis...

First advice: As reported by halfer you should take care of your indentation code, it allows you (and us) to read it more easily.

  • First error, also pointed out by halfer: You probably forgot to declare a block of code with {}, tips for your research to know when to put it here.
  • Second error: You should take a look at the for loop syntax: for (j=0; j<30; j++);will basically do nothing due to ;at the end.
  • You confuse assignment and condition test, if (l[z]=0), for (i=1; i=k; i=i+1) and for (z=i; z=30; z=2*z) haven't condition test, but assignment (just = and not == or <=, etc.) so they are always true...

Also, you don't explain what you want your code to do... It seems like you want to turn on some lights, but the double statement loop with the wrong for is confusing. I don't know if you want to turn on 2^N bulbs or just the one selected by the user... Here is my correction of your code:

int main(void) {
    int k, j, z, i, n;
    int l[30];

    // Setting initial values 0 (0=off 1=on)
    for (n=0; n<30; n++) {
        l[n] = 0;
    }

    // Employee number
    printf("give employee number\n");
    scanf("%d", &k);

    // Validation of k
    if (k<0 || k>30) {
        printf("wrong input");
    } else { // New block

        // Lamp status change
        /*
        i = k is an assignment not a test, maybe i == k ?
        but still false, for do while the condition is true
        so use <= and why use i = i+1 here and not i++ like for n++ L6 ?
        Ok for this loop, but with the other you gonna reswitch again and
        again. If you want only switch the one selected, consider to use
        an if instead of the 2nd for loop.
        */
        for (i=1; i <= k; i=i+1) {

            /*
            Same test / assignment misunderstanding.
            personally except 15, I don't know a lot of intergers
            mutiplied by 2 that give 30. Example: if I set 1 in
            my keyboard, k = 1, then i = 1, z = 1, z = 2,z = 4,
            z = 8,z = 16, z = 32, z = 64, etc. to overflow.
            So  z = 30 (ouch z == 30) is never true.
            If you tried to switch only the lamp selected by the user
            I don't see the point of the second for loop.
            But if you wanted to switch 1 light each 2^N bulbs you
            should set z <= 30.
            */
            for (z=i; z<=30; z=2*z) {
                if (l[z] == 0) // = is an assignment, == instead?
                    l[z] = 1; // A block with {} is not needed here because there is only 1 instruction
                else
                    l[z] = 0; // Same as above, {} are not needed here too
            }
        }

        for (j=0; j<30; j++) { // No; here, see the 'for' loop syntax
            printf("lamp[%d] is: %d\n", j+1, l[j]);
        }
    } // End of the else block

    return(0);
}
Taknok
  • 717
  • 7
  • 16