When should we use two dimensional array?

Thread Starter

anukalp

Joined Jul 28, 2018
144
When should we use two dimensional array?

Two dimensional array
Code:
int array[2][4]={1, 2, 3, 4, 5, 6, 7, 8};
It takes 32 bytes

One dimensional array
Code:
 int array[8]={1, 2, 3, 4, 5, 6, 7, 8};
It also takes 32 bytes

What is so special in two dimensional array so that we should use it in place of one dimensional array ?
 
If you have data which is in associated groups such as Temperature and time, It makes it much easier to keep track of and access the group of data with a single command if it is stored in a two dimensional array. If there is more data associated with it, for example, air pressure and humidity, it would make sense to store the data as four associated elements in the two dimensional array.
Regards,
Keith
 
What is so special in two dimensional array so that we should use it in place of one dimensional array ?
It really isn't "so special", it's just syntactic sugar IMO. Kind of a preference thing. Ordinarily, if I have a series of timestamped values I'd use an array of structs, in which case you can add other fields as necessary. But, weighing readability and performance one might pick either approach. And I might be apt to use 2D in cases where one or both of the dimensions are static and well defined. A tick tack toe game for example would be xoro[3][3] rather than xoro[9], but I've used the latter approach. Explicitly using 2D lets the compiler do the row x column math for you.
 
The advantage is not in storage, it's in addressing.

Consider a signal generator that outputs a continuous waveform. For flexibility, we store the waveform in an array—say, 512 values—and simply step through it from start to end, repeatedly, sending the values to a DAC. For instance, if the array is called "wave" and we address it by stepping through a variable "index" from 0 to 511: out = wave[index]. But say we want multiple waveforms, maybe sine, ramp, and square. We could use three arrays, but we'd need to either use steering logic (if/else) to access the right array, or manipulate a pointer variable to the right array—ugly.

Let's say our solution is to use a variable named "type", which is 0 for sine, 1 for ramp, and 3 for square. And we place all of the wave sequentially in a single array, 512 values for sine, 512 for ramp, 512 for square, for an array of size 1536. Then we can address the wave by incrementing index in this way: out = wave[type * 512 + index].

Or, we use a two dimensional array: out = wave[index][type].

At the low level, both work the same—memory is not two-dimensional, the compiler simply lumps it together sequentially. The only difference is that the compiler does the ugly calculations for you. Doesn't the second way make a lot more sense?
 

John P

Joined Oct 14, 2008
1,780
There's no advantage to the computer in using a 2-dimensional array. The time to use it is when it's more useful to you. Does it help you organize information? That's what it's for.
 

dl324

Joined Mar 30, 2015
9,323
What is so special in two dimensional array so that we should use it in place of one dimensional array ?
As others have noted, it's for programmer convenience; it doesn't impact storage requirements.

In C, a 2D array is actually a 1D array where each of the elements is an array.
 

djsfantasi

Joined Apr 11, 2010
5,835
A simplification of the previous posts. You use a 1, 2, 3, ... array when called for to simplify data access. For example, you might use a 2D array to map elevation. The two indices are the coordinates of a point. The array element contains the height at that point.

As many have pointed out, arrays always contain a contiguous memory block. And the system calculated the address of an entry, based on the array indices. You can do that yourself, but (and here’s where I disagree with some) it’s likely that the compiler can do it faster. As the number of dimensions increases, so will the complexity of the calculations.
 
Top