Three-Dimensional Array Assignment

I'm wondering why I'm not allowed to do this simple assignment:

1
2
3
4
5
6
7
int n=9,m=9,i,j,g;
int v[n][m];
int d[n][m][9];

[...]

v[i][j]=d[i][j][g];


i,j and g have adequate values
Last edited on
What error message do you get?
main.cpp:50:30: error: invalid types 'int [9][int [n]]' for array subscript
You would get an error like that if you tried to use an array as index.

1
2
3
4
5
...
{
	int g[9];
	v[i][j]=d[i][j][g]; // error: invalid types ‘int [9][int [9]]’ for array subscript
}


Make sure that you have not reused the names i, j or g to define an array in the [...] section.
Last edited on
@ryochan

You have to specify which array location you're using in the g array.
1
2
3
4
5
...
{
	int g[9];
	v[i][j]=d[i][j][g[?]]; // ? = A number, 0 to 8
}
@ryochan, you should also ask yourself if you *truly* need 3D array. If you provide more info for the use case, maybe some people here could help you refactor that into a better design.
I would like to fill a matrix (v) with random numbers from 1 to 9, do some controls on which numbers have been put in and keep track of them. The 3D array (d) would serve this scope in my view, memorizing for each matrix element the numbers that has already been "ruled out".
If this is not clear, I'm going to post the code. Thank you.

You have to specify which array location you're using in the g array.

g is not an array.. must be?
Last edited on
Also in C++ array sizes must be compile time constants.

What line in your posted code corresponds to line 50?

i,j and g have adequate values

What do you consider "adequate"?

Line 50 corresponds to the assignement that returns the error:
d[i][j][g]=v[i][j];

Adequate means that values are in the range 0-8 (sizes n, m been equal to nine)
Last edited on
So you're storing something like Sudoku possibilities for each element in matrix? Something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <array>
#include <set>

using namespace std;

const string HORIZ =  "     + - - - + - - - + - - - +\n";
const string FOOTER = "       0 1 2   3 4 5   6 7 8\n\n";

struct Element
{
    Element() : 
        Value(' '), 
        Possibilities({'1', '2', '3', '4', '5', '6', '7', '8', '9'})
    {
    }
    
    set<char> Possibilities;
    char Value;
};

void Show(const array<array<Element,9>, 9>& matrix)
{
    for (int r=0; r<9; ++r)
    {
        if (r%3 == 0)
            cout << HORIZ;
        cout << r << "    ";
            
        for (int c=0; c<9; ++c)
        {
            if (c%3 == 0)
                cout << "| ";
            cout << matrix[r][c].Value << " ";
        }
        cout << "|" << endl;
    }
    cout << HORIZ;
    
    cout << endl << FOOTER;
}

void Show(const set<char>& possibilities)
{
    cout << "[";
    for (auto& n : possibilities)
        cout << n << " ";
    cout << "]" << endl;
}

int main() 
{
    array<array<Element, 9>, 9> matrix;
    matrix[0][0].Value = '1';
    matrix[0][1].Value = '4';
    matrix[0][2].Value = '6';
    matrix[2][2].Value = 'X';
    matrix[2][7].Value = '2';
    matrix[8][2].Value = '7';
    Show(matrix);
    
    matrix[2][2].Possibilities = {'3', '5', '8', '9'};
    
    cout << "possibilities for X at (2,2): ";
    Show(matrix[2][2].Possibilities);
    
    return 0;
}


     + - - - + - - - + - - - +
0    | 1 4 6 |       |       |
1    |       |       |       |
2    |     X |       |   2   |
     + - - - + - - - + - - - +
3    |       |       |       |
4    |       |       |       |
5    |       |       |       |
     + - - - + - - - + - - - +
6    |       |       |       |
7    |       |       |       |
8    |     7 |       |       |
     + - - - + - - - + - - - +

       0 1 2   3 4 5   6 7 8

possibilities for X at (2,2): [3 5 8 9 ]
Last edited on
Yes, but I want to do it automatically
Topic archived. No new replies allowed.