c++ - 在 c 中的二维数组的末尾添加一个额外的数据列

标签 c++ arrays pointers 2d

我如何从一个数组中添加数据并将其作为列放置在预先存在的数组中。

example
double array[3][2];

打印时:

3   2
5   5
7   8

我还有另一个包含其他信息的数组

double arrayb[3]={1,1,1};

我想运行 for 循环并能够打印

for (int i=0; i<3; i++){
 for (int j=0; j<3; j++){
  cout << array[i][j];}}

这就是我想看到的:

    3  2  1
    5  5  1
    7  8  1

最佳答案

试试这个:

for (int i=0; i<3; i++) {
    // You have only two elements in array[i], so the limit should be 2
    for (int j=0; j<2; j++) {
        // Leave some whitespace before the next item
        cout << array[i][j] << " ";
    }
    // Now print the element from arrayb
    cout << arrayb[i] << endl;
}

关于c++ - 在 c 中的二维数组的末尾添加一个额外的数据列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16845929/

相关文章:

c++ - 适用于嵌入式 Windows 的 Silverlight

c++ - 将 mat vector 转换为 Mat 文件以便使用 opencv svm

c++ - 请用指针解释数组

objective-c - 提供回调函数作为特定的指针类型

c - 将字符串数组从 C 返回到 Fortran

c++ - 如何在C++中输入整数而不使用 ">>"

c++ - Qt中的数据库错误?

java - 这是返回指向值的指针还是复制值?

C++ ifstream 不读取\n?

java - 如何打印/引用第 7 号人物?