填充数组

发布于 2024-12-10 07:58:06 字数 373 浏览 3 评论 0原文

我正在尝试用整数填充数组,但我不太确定如何实现它。 我正在编写一个程序,询问每个人拥有的煎饼数量,我想将每个数量存储到一个数组中。

#include <iostream>
using namespace std;

int main ()
{
   //An array of people

    for(int i = 0; i<10; i ++)
    {
        int amount;
        cout<<"How many pancakes did person eat? \n";
        cin >> amount ;

        people[i] = amount;

    }
}

I'm trying to populate an array with integers, but I'm not quite sure how to implement it.
I'm writing a program that ask for the amount of pancakes each person has and im wanting to store each amount into an array.

#include <iostream>
using namespace std;

int main ()
{
   //An array of people

    for(int i = 0; i<10; i ++)
    {
        int amount;
        cout<<"How many pancakes did person eat? \n";
        cin >> amount ;

        people[i] = amount;

    }
}

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

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

发布评论

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

评论(2

不疑不惑不回忆 2024-12-17 07:58:06

如果你用 C++ 编程,你应该使用 std::vector 而不是普通数组。代码可能如下所示:

std::vector<int> people;
...
// Add amount at the end of the array
people.push_back(amount);

If you program C++ you should use std::vector instead of normal arrays. The code might then look like this:

std::vector<int> people;
...
// Add amount at the end of the array
people.push_back(amount);
懒的傷心 2024-12-17 07:58:06

您可以动态创建整数数组,如下所示:

int * people = new int[10]; // 10 is the number of elements inside the array

或者,也可以静态定义:

int people[10];

比较它们之间的差异: http://www.cplusplus.com/forum/beginner/12755/

You can dynamically create an integer array as follows:

int * people = new int[10]; // 10 is the number of elements inside the array

Or, it can statically be defined:

int people[10];

To compare the difference between them: http://www.cplusplus.com/forum/beginner/12755/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文