简体   繁体   English

C 程序的问题(可能通过使用数组来解决)

[英]Issue with C program (maybe solved with use of arrays)

I have written the following code.我已经编写了以下代码。 But it doesn't run until the final printf .但它直到最后的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);
}

I suggest that you go work a little more your C basis...我建议你在 C 基础上多做一些工作......

First advice: As reported by halfer you should take care of your indentation code, it allows you (and us) to read it more easily.第一个建议:正如halfer所报告的,您应该注意缩进代码,它可以让您(和我们)更轻松地阅读它。

  • 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 .第一个错误,也由 halfer 指出:您可能忘记使用{}声明代码块,提示您的研究知道何时将其放在这里
  • Second error: You should take a look at the for loop syntax : for (j=0; j<30; j++);第二个错误:你应该看看for循环语法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...您混淆了赋值和条件测试, if (l[z]=0)for (i=1; i=k; i=i+1)for (z=i; z=30; z=2*z)没有条件测试,但赋值(只是=而不是==<=等)所以它们总是正确的......

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.另外,你没有解释你想让你的代码做什么......看起来你想打开一些灯,但是带有错误for的双语句循环令人困惑。 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:我不知道您是要打开 2^N 灯泡还是只打开用户选择的灯泡...这是我对您的代码的更正:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM