C++ Creating a for loop to output a multiple multi-dimensional arrays.



  • Hi Everyone,

    I am trying to output multiple multi-dimension array in C++, and need a little help understanding on why and how for loops display output where they do. I would like my first multi-dimensional array to output and then to the right of that I would like to have my second mult-dimensional array output. I am able to get my second mult-dimensional array to output underneath the first, but not next to it without making everything look like a mess. Below is an example of how I would like my code to output.The - are supposed to be empty space, I had an issue trying to get it to display properly.

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

    Below is what I have to output my first mult-dimensional array. Thanks for your time.

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

    }



  • I ended up finding that if I add another for statement and nested in the first for loop and then cout some blank spaces I was able to achieve what I was looking to do. Below is what I had to do.

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