C++ 中的数组赋值错误,我的代码有什么问题?

发布于 2025-01-18 04:33:55 字数 458 浏览 0 评论 0原文

我正在用CodeBlocks尝试此代码,但结果是一些随机数! 如果您检查代码,请让我知道我的错误是什么。它应该导致乘数为25:

#include <iostream>

using namespace std;

main(){

    const int array_size = 10;
    int numbers[array_size];
    int counter = 0;

    while(counter < array_size){
        numbers[counter] = 25 * counter;
        counter++;
        cout << "number[" << counter << "] = " << numbers[counter] << endl;
    }
}

事先感谢。

I am trying this code in CodeBlocks but the result is some random numbers!
I appreciate if you check the code and let me know what my mistake is. It should result in multipliers of 25:

#include <iostream>

using namespace std;

main(){

    const int array_size = 10;
    int numbers[array_size];
    int counter = 0;

    while(counter < array_size){
        numbers[counter] = 25 * counter;
        counter++;
        cout << "number[" << counter << "] = " << numbers[counter] << endl;
    }
}

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云裳 2025-01-25 04:33:55

在设置 numbers[counter] 的值后,您将递增 counter。在那之后。 numbers[counter](具有 counter 的新值)是一个新的未初始化元素。

您应该在打印之后移动增量:

#include <iostream>

using namespace std;

int main(){

    const int array_size = 10;
    int numbers[array_size];
    int counter = 0;

    while(counter < array_size){
        numbers[counter] = 25 * counter;
        cout << "number[" << counter << "] = " << numbers[counter] << endl;
        counter++;
    }
}

或者干脆避免使用数组(在本例中):

#include <iostream>

int main(){

    const int array_size = 10;
    int counter = 0;

    while(counter < array_size){
        std::cout << "number[" << counter << "] = " << 25 * counter << std::endl;
        counter++;
    }
}

另一种方法是使用两个循环——一个用于初始化,一个用于打印:

#include <iostream>

using namespace std;

int main(){

    const int array_size = 10;
    int numbers[array_size];
    int counter = 0;

    while(counter < array_size){
        numbers[counter] = 25 * counter;
        counter++;
    }

    counter = 0;
    while(counter < array_size){
        cout << "number[" << counter << "] = " << numbers[counter] << endl;
        counter++;
    }
}

You are incrementing counter after setting the value of numbers[counter]. After that. numbers[counter] (with the new value of counter) is a new uninitialized element.

You should move the increment after the printing:

#include <iostream>

using namespace std;

int main(){

    const int array_size = 10;
    int numbers[array_size];
    int counter = 0;

    while(counter < array_size){
        numbers[counter] = 25 * counter;
        cout << "number[" << counter << "] = " << numbers[counter] << endl;
        counter++;
    }
}

Or simply avoid using an array (in this case):

#include <iostream>

int main(){

    const int array_size = 10;
    int counter = 0;

    while(counter < array_size){
        std::cout << "number[" << counter << "] = " << 25 * counter << std::endl;
        counter++;
    }
}

Another way is using two loops -- one for initialization and one for printing:

#include <iostream>

using namespace std;

int main(){

    const int array_size = 10;
    int numbers[array_size];
    int counter = 0;

    while(counter < array_size){
        numbers[counter] = 25 * counter;
        counter++;
    }

    counter = 0;
    while(counter < array_size){
        cout << "number[" << counter << "] = " << numbers[counter] << endl;
        counter++;
    }
}
多情癖 2025-01-25 04:33:55

在增加变量Counter后,您将输出数组的非初始化元素。

    counter++;
    cout << "number[" << counter << "] = " << numbers[counter] << endl;

错误的原因是使用不适当的循环。

而不是随行循环,而是用于循环。

例如,

for ( int counter = 0; counter < array_size; counter++ ){
    numbers[counter] = 25 * counter;
    cout << "number[" << counter << "] = " << numbers[counter] << endl;
}

请注意没有参数的函数主体应像

int main()

这样声明您可能不会省略函数返回类型。

You are outputting an uninitialized element of the array after incrementing of the variable counter.

    counter++;
    cout << "number[" << counter << "] = " << numbers[counter] << endl;

The reason of the error is usage of an inappropriate loop.

Instead of the while loop it is better to use for loop.

For example

for ( int counter = 0; counter < array_size; counter++ ){
    numbers[counter] = 25 * counter;
    cout << "number[" << counter << "] = " << numbers[counter] << endl;
}

Pay attention to that the function main without parameters shall be declared like

int main()

That is you may not omit the function return type.

尹雨沫 2025-01-25 04:33:55

更改此代码

const int array_size = 10;
    int numbers[array_size];
    int counter = 0;

    while(counter < array_size){
        numbers[counter] = 25 * counter;
        cout << "number[" << counter << "] = " << numbers[counter] << endl;
        counter++;
    }

change for this code

const int array_size = 10;
    int numbers[array_size];
    int counter = 0;

    while(counter < array_size){
        numbers[counter] = 25 * counter;
        cout << "number[" << counter << "] = " << numbers[counter] << endl;
        counter++;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文