#Why are multi-dimensional arrays useful?

1 messages · Page 1 of 1 (latest)

soft lotus
#

Hello, can anyone explain why multi-dimensional arrays are useful please. For example what do they allow us to do, how should we think of them, like in my case, I only think of a 2D array for example as a table but as a programmer, how should one think, like "ok so we want x,y,z and so maybe a 2D array might do the job here", what should make us think like that?

For example consider the following code:

public static String pascalTriangle(int n) {
        String row = "";
        String finalRow = "";
        if (n == 1) {
            row = "1";
            System.out.println(row);
            return row;
        } else if (n == 2) {
            row = "1  1";
            System.out.println("1");
            System.out.println("1 1");
            return row;
        }
        else {
            row = pascalTriangle(n - 1);
            int forwardPointer = 1;
            int backwardPointer = 0;
            String[] parts = row.split("\\s+");

            String middleNumber = "";
            while(forwardPointer < parts.length) {
                middleNumber = middleNumber + Integer.toString(Integer.parseInt(parts[backwardPointer]) + Integer.parseInt(parts[forwardPointer])) + " ";
                backwardPointer++;
                forwardPointer++;
            }
            finalRow = "1 " + middleNumber + "1";
            System.out.println(finalRow);

        }
        return finalRow;
    }

I think we can use 2D array to do the pascal triangle but how should one think before coming to this conclusion?

tender shale
#

think of matrices and linear algebra concepts. a 2d array is basically a representation of a matrix

soft lotus
#

why is that used so much in linear algebra? What makes linear algebra so "unique" ?

solemn crow
#

The first thing that comes to mind is that if you want to do any sort of image manipulation, you're looking at multi dimensional arrays, 2D arrays at the very least. That can come in handy when doing computer vision or open gl work if you're into that.

Apart from that, for problems like these, you typically sort of get used to what data structures are good for certain problems. For example, 2D arrays lend themselves well for graph problems (in certain cases they might be lists/arraylists instead of arrays, but they are still multi dimensional). So if your problem can be modelled as a graph, you might be looking at that.

Another use case off the top of my head is that like in 1D arrays, you often "encode" information that has just 1 variable. For example, an array called carPrices might have the value 300 at index 7, representing that the price was $300 on the 8th day (7+1 cuz zero-indexing).

Similarly you might use 2D arrays to encode two different variables. So if Jack is your dealer, he might get you a deal of $300 on the 8th day, but if Jenny is your dealer, she might get you a deal of $250. If Jack is dealer number 1 and Jenny dealer number 2, then perhaps your 2D array could have values 300 at [0, 7] and 250 at [1, 7].

It's really upto you how you want to model data, though.