简体   繁体   English

无法在 C 中分配值并增加 int

[英]Unable to assign value and increment an int in C

I have this program to generate armstrong numbers upto a given range.我有这个程序可以生成给定范围内的阿姆斯壮数字。 But the problem is that the range variable (n in this case) is somehow acting like a const.但问题是范围变量(在本例中为 n)在某种程度上就像一个 const。 I cannot assign a value nor increment it... There are errors during compilation with gcc.我不能分配一个值也不能增加它......在使用 gcc 编译期间出现错误。 The power function works fine (Same issue with pow() defind in math.h).电源 function 工作正常(与 pow() 在 math.h 中定义的问题相同)。 I would like to know why this is happening to this code and the possible fix(es).我想知道为什么此代码会发生这种情况以及可能的修复。 Thank you谢谢

#include <stdio.h>
//#include <math.h>

int power(int a, int b) {
    int c = 1;
    for(int i = 0; i < b; i++) {
        c *= a;
    }
    return c;
}

void main(void) {
    int sum, y, temp;
    printf("temp, y, sum, n\n");
    for(int n = 1; n < 100; n++) {
        temp = n;
        printf("%d ", temp);
        y = 0; // y to hold the number of digits of n
        while (n > 0) {
            n = n / 10;
            y++;
        }
        printf("%d ", y);

        n = temp;
        sum = 0;

        while(n > 0) {
            sum += power((n % 10), y);
            n = n / 10;
        }

        if (temp == sum) {
            printf("%d ", sum);
        }

        printf("%d\n", n);
    }
}

Output: Output:

temp, y, sum, n
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
.
.
.

Are you not constantly dividing n by 10?你不是经常将 n 除以 10 吗?

As n is an integer that starts as 1, and not a float, it would constantly set to 0.由于 n 是一个 integer,它以 1 开头,而不是浮点数,它会一直设置为 0。

Your second while(n > 0) loop effectively sets n=0 within the outer for loop.您的第二个while(n > 0)循环在外部for循环中有效地设置了n=0 Did you want to use a second n = temp ?你想使用第二个n = temp吗?

before the last printf statement add: n = temp;在最后一个 printf 语句之前添加: n = temp;
try you code like this:试试你这样的代码:

#include <stdio.h>
//#include <math.h>

int power(int a, int b) {
    int c = 1;
    for(int i = 0; i < b; i++) {
        c *= a;
    }
    return c;
}

void main(void) {
    int sum, y, temp;
    printf("temp, y, sum, n\n");
    for(int n = 1; n < 100; n++) {
        temp = n;
        printf("%d ", temp);
        y = 0; // y to hold the number of digits of n
        while (n > 0) {
            n = n / 10;
            y++;
        }
        printf("%d ", y);

        n = temp;
        sum = 0;

        while(n > 0) {
            sum += power((n % 10), y);
            n = n / 10;
        }

        if (temp == sum) {
            printf("%d ", sum);
        }
        n = temp;
        printf("%d\n", n);
    }
}

It is because the value of n is set to 0 outside the loop.这是因为 n 的值在循环外设置为 0。 Try the below code.试试下面的代码。

   #include <stdio.h>
//#include <math.h>

int power(int a, int b) {
    int c = 1;
    for(int i = 0; i < b; i++) {
        c *= a;
    }
    return c;
}

void main(void) {
    int sum, y, temp;
    printf("temp, y, sum, n\n");
    for(int n = 1; n < 100; n++) {
        temp = n;
        printf("%d ", temp);
        y = 0; // y to hold the number of digits of n
        while (n > 0) {
            n = n / 10;
            y++;
        }
        printf("%d ", y);

        n = temp;
        sum = 0;

        while(n > 0) {
            sum += power((n % 10), y);
            n = n / 10;
        }

        if (temp == sum) {
            printf("%d ", sum);
        }
        n = temp;
        printf("%d\n", n);
    }
}

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

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