是否可以使用for循环显示相邻的两个多维数组

Is it possible to display two multi-dimensional arrays next to each other using a for loop

本文关键字:两个 数组 for 可以使 循环 显示 是否      更新时间:2023-10-16

我正在尝试用C++输出多个多维数组,需要一些帮助来理解循环为什么以及如何在它们所在的位置显示输出。我希望输出我的第一个多维阵列,然后在它的右边输出我的第二个多维阵列。我可以让我的第二个多维数组在第一个数组下面输出,但不能在它旁边输出,否则会让一切看起来一团糟。下面是我希望我的代码如何输出的示例。

first         second
12 34 23      34 56 67
32 77 56      34 54 56

下面是我必须输出我的第一个多维数组的代码。谢谢你抽出时间。

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;


int main()
{
int a[2] [3] = 
{
    { 16, 18, 23 },
    { 54, 91, 11 }
};
int b[2][3] =
{
        { 14, 52, 77 },
        { 16, 19, 59 }
};
for (int row = 0; row < 2; row++)
{
    for (int column = 0; column < 3; column++)
    {
        cout << a[row][column] << " ";
    }
    cout << endl;
}

_getch();
return 0;

}

您可以尝试使用3个for循环,一个用于行数(将包围另外两个用于循环),然后对第一个数组的列使用for循环,然后输出几个空格,然后对第二个数组的行使用第三个for循环。

例如:

for(i = 0; i < num_rows; i++)
{
    for(j = 0; j < num_cols_1; j++)
    {
        // print j'th element in i'th row of array 1
    }
    // print some spaces
    for(j = 0; j < num_cols_2; j++)
    {
        // print j'th element in i'th row of array 2
    }
}