简体   繁体   English

为什么变量分配的位置如此重要? [C]

[英]Why the place of the assignment of a variable is so important? [C]

While trying to solve an exercise of Kochan - Programming in C Book (ex 4 loops chapter) the question was to write the first 10 factorial numbers. 在尝试解决Kochan的练习-C书中的编程(第4循环章节)时,问题是要写出前10个阶乘数。 I tried to solve it with nested loops, but the results were a complete rubbish. 我试图用嵌套循环来解决它,但是结果是一个完全的垃圾。 I looked up, and I found one solution that was very similar to mine but I noticed that the place of the variable is different. 我抬起头,发现一种解决方案与我的非常相似,但是我注意到变量的位置不同。

Previous solution: 先前的解决方案:

/*This program calculates the first 10 factorials*/

#include <stdio.h>

int main(void){
    int n,i;
    unsigned long int fact=1;
    printf(" n\t\t  n!\n");
    printf("---\t\t--------\n");
    for(n=1; n<=10; n++){
        for(i = 1; i<=n; i++){
            fact*=i;
        }       
        printf("%2d\t\t%7ld\n",n,fact);
    }

    return 0;
}

Output: 输出:

./factorial 
 n        n!
---     --------
 1            1
 2            2
 3           12
 4          288
 5        34560
 6      24883200
 7      857276416
 8      -511705088
 9      1073741824
10            0

Fixed solution: 固定解决方案:

/*This program calculates the first 10 factorials*/

#include <stdio.h>

int main(void){
    int n,i;
    printf(" n\t\t  n!\n");
    printf("---\t\t--------\n");
    for(n=1; n<=10; n++){
        unsigned long int fact=1;       
        for(i = 1; i<=n; i++){
            fact*=i;
        }       
        printf("%2d\t\t%7ld\n",n,fact);
    }

    return 0;
}

Output: 输出:

./factorial 
 n        n!
---     --------
 1            1
 2            2
 3            6
 4           24
 5          120
 6          720
 7         5040
 8        40320
 9       362880
10      3628800

Notice that the only difference is that that variable fact is local in the second solution while it was global in the previous one. 注意,唯一的区别是该变量事实在第二个解决方案中是局部的,而在前一个解决方案中是全局的。

So I guess the question is clarified now, why changing just the place seems to solve the issue? 所以我想这个问题现在已经澄清了,为什么只改变位置似乎可以解决问题? Why there is this issue of placing variables in the first place? 为什么存在将变量放在首位的问题? Thanks in advance ! 提前致谢 !

因为在n上的每次迭代期间,变量都被设置为1。如果在迭代n的循环开始时将其设置为1,则可以像在第一个代码中一样声明它。 。

The issue is not so much where the variable is declared , as where it is initialized . 问题不仅仅在于变量的声明位置和初始化位置 In the correct code, fact is set to 1 for each new n , in order to "start fresh" and calculate the factorial. 在正确的代码中, fact对于每个新n都设置为1 ,以便“重新开始”并计算阶乘。 In your code, you don't reset fact , so it continues to hold the leftover value from the previous n , so you're computing n! 在您的代码中,您无需重置fact ,因此它将继续保留前n剩余的值,因此您正在计算n! times the previous result. 乘以先前的结果。

Another solution is to remove the inner loop, and take advantage of the fact that n ! 另一种解决方案是删除内部循环,并利用n = n × ( n -1)!. = n ×( n -1)!。 (That will also perform better.) (这也会表现更好。)

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

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