2arrays & Structures

Expect the following to print values of the 2D array

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
  #include <iostream>

using namespace std;

struct matrix
{
    int matrice[0][0]; int row; int column;
    
    matrix::matrix(int r, int c)
    {
        row = r, column = c;
        matrice[r][c];
    }
    
    void matrix::printMatrix() const
    {
        for(int r = 0; r < row; r++)
        {
            for(int c = 0; c < column; c++)
            {
                cout<< matrice[r][c] <<endl;
            }
        }
    }
};

int main()
{
    matrix M(3,3);
    
    M.matrice[0][0] = 12;
    M.matrice[0][1] = 3;
    M.matrice[0][2] = 32;
    M.matrice[1][0] = -2;
    M.matrice[1][1] = 2;
    M.matrice[1][2] = 42;
    M.matrice[2][0] = 7;
    M.matrice[2][1] = 11;
    M.matrice[2][2] = 1;
    
    M.printMatrix();
    
    system("pause");
    return 0;
}
Firstly :
int matrice[0][0];

You can't declare a variable (member) with zero size like that. You may try :
int matrice[200][200];

I guarantee it will never hurt you :)
Or you may also try int ** matrice; but is highly NOT RECOMMENDED - it is simply more advanced and it involves a variety of dangers. Also it requires quite a bit of time to set up and get it running for good :)
Thanks, that really helped (matrice[200][200])
Good to hear :)
~~~ Stay tuned ~~~

Or you may also try this (best recommended approach) :

vector<vector<int>> matrice;

And :
1
2
3
4
5
6
matrix::matrix(int r, int c)
    {
        row = r, column = c;
        matrice.resize(r);
        for (vector<int> &v : matrice) v.resize (c);
    }


Thoughts?
Anyway, have a good day :)
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
#include <array>
#include <iomanip>
#include <iostream>

template <std::size_t ROWS, std::size_t COLS>
struct matrix {
    std::array<std::array<int, COLS>, ROWS> matrice = {};

    void print() const {
        for (auto& row : matrice)
        {
            for (auto& cell : row)
                std::cout << std::setw(8) << cell;
            std::cout << '\n';
        }
    }
};

int main() {
    matrix<3, 3> M;

    M.print();
    std::cout << '\n';

    M.matrice[0][0] = 12;
    M.matrice[0][1] = 3;
    M.matrice[0][2] = 32;
    M.matrice[1][0] = -2;
    M.matrice[1][1] = 2;
    M.matrice[1][2] = 42;
    M.matrice[2][0] = 7;
    M.matrice[2][1] = 11;
    M.matrice[2][2] = 1;

    M.print();
}
@cire
Your idea is a very good one but if the OP wants to dynamically resize a matrix it will be a problem. Dynamic versions are much more flexible and customizable than static versions - if performance is not absolutely critically important dynamic ones are much better.
Your idea is a very good one but if the OP wants to dynamically resize a matrix it will be a problem.

At the point in time which this becomes necessary for the OP, a more complete approach will be appropriate. Since I'm not looking to fulfill all of the OPs future needs and he/she would benefit from reimplementing things if the need arises, (and since I don't buy into your argument anyway) I'm perfectly fine with using std::array here.

Thanks for your subjective-opinion-presented-as-fact, though. Much appreciated.
The above approaches seems complicated for me, but thanks anyways
Topic archived. No new replies allowed.