-2

So I have started learning about coding in C++ and I'm just starting on arrays, but I just don't understand the concept of multidimensional arrays, does it save memory and make the program faster or something else? And also under what circumstance should I use them?

Thanks

MdinG
  • 13
  • 2

3 Answers3

2

Answer not specifically C or C++.

Imagine a chess board, it has some squares. Each square can have a playing piece on it. You could store the state of each square in an array.

If you number the squares 0, 1, 2, 3 … 63 then you can put them in a one dimensional array. To go left add 1, to go right subtract 1, to go down add width (8), to go up subtract width (8) (With some bounds checking).

Alternatively you can use a 2 dimensional array 0…7, 0…7 now you do not add 8 you just add one to y dimension. Note the computer converts it all to one dimensional in the end, but C and C++ arrays have no bounds checking, and you have to keep telling it the width. There are some good classes in the library to make things easier.

Also you can have 3, 4, 5 or any other number of dimensions.

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52
0

you'll get it just in practice! there are lot's of situationts where you use bidimensional arrays, just because it'll be not enough to use a simple array, for example simple backtracking problems such as labyrinths, sudoku, the lee's algorithm. Or another example: you have a table of data of students from your class, the columns is data of every student, but the raws can be: the first raw the average grade, the second raw his height, the third raw his number, and so on

Andrew
  • 106
  • 1
  • 1
  • 14
-3

In practice, don't use multidimensional arrays in C or C++. Just use a single dimension array, perhaps by making a class containing it with some getter and setter methods.

However, there are many occasions where you would use a container of containers, e.g. types like std::set<std::vector<long>> or occasionally std::vector<std::array<double,3>> etc...

The practical exception for using raw 2D arrays might be for matrixes whose dimensions are a compile-time constant. So a 3D linear transform (from R3 to itself) might be represented by a double m[3][3]; ; then a good optimizing compiler might even be able to vectorize the code.

But for a matrix whose both dimensions are only known at runtime, making it a bidimensional array is generally a mistake or an overkill (and this is why the STL does not provide you with a std::matrix template). You could use std::vector<std::vector<double>> but that requires allocating each row of your matrix.

Of course, you'll find a lot of existing linear algebra libraries like LAPACK, BLAS, etc... Use them for 2D or 3D computations. Some of them might even use hardware acceleration (e.g. using OpenCL to run on a GPGPU).

More generally, very often you should use and combine existing C++ standard containers (or even define your own templates providing your containers, perhaps by combining existing ones).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547