Printing 3D Arrays

I am about 3 months into learning C++ and am messing around with printing multidimensional arrays. My problem is that I cannot for the life of me figure out why this particular 3 dimensional array cannot print.

The error is "subscript requires array or pointer type"
Located at line "cout << multi[i][j][k] << " ";"
I am using Microsoft Visual Studio 2015. I do not believe it is my inputs because those would automatically be filled with 0's anyway. I believe my issue is understanding the error message.
Anything as simple as hints would be greatly appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <array>
using namespace std;

int main()
{
	array<double, 3> multi = { {1,2,3} };
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			for (int k = 0; k < 3; k++)
			{
				cout << multi[i][j][k] << "   ";
			}
		}
		cout << endl;
	}
Last edited on
 
std::array<double, 3> multi;


This is NOT a 3D array, its a 1D array. It is the same as saying:
 
double multi[3] = { 1, 2, 3 };

Therefore a 3D array can come in these forms:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    double a[3][3][3] = { { { 1,2,3 }, { 4,5,6}, { 7,8,9 } },
        { { 10,11, 12 }, { 13, 14, 15 }, { 1, 5, 8 } },
        { { 16, 17, 18 }, { 19, 20, 21 }, { 22, 23, 24 } }
    };
    for( int i = 0; i!= 3; ++i ){
        for( int x = 0; x!= 3; ++x ){
            for( int y = 0; y!= 3; ++y ){
                std::cout << a[i][x][y] << " ";
            }
            std::endl( std::cout );
        }
        std::endl( std::cout );
    }
    // OR
    std::array<std::array<std::array<double, 3>, 3>, 3> multi;



1 2 3 
4 5 6 
7 8 9 

10 11 12 
13 14 15 
1 5 8 

16 17 18 
19 20 21 
22 23 24 


Last edited on
Its a one dimensional array not the three dimension array
Thank you so much!
I'll mess around with this more until I get it!
Topic archived. No new replies allowed.