1

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

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
4

2 回答 2

0

Try this:

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;
}
于 2013-05-30T21:14:19.240 回答
0

Seems like the obvious method would be something like:

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';
}
于 2013-05-30T21:14:30.060 回答