在c中的2d数组的末尾添加额外的数据列

Adding an extra column of data at the end of a 2d array in c

本文关键字:添加 数据 中的 2d 数组      更新时间:2023-10-16

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

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;
}

似乎最明显的方法是:

for (int i=0; i<3; i++) {
    for (int j=0; j<2; j++)
       std::cout << array[i][j] << 't';
    std::cout << arrayb[i] << 'n';
}