Magic of Arrays Using C#

2. Multi-Dimensional Arrays versus Jagged Arrays with Traveling Problems
3. The Problem
4. The Solution
6. Summary
• Print

Multi-Dimensional Arrays versus Jagged Arrays with Traveling Problems

We discussed the syntax of multi-dimensional arrays and jagged arrays, and the confusion that started with the syntax because the syntax provided to the jagged arrays is actually the syntax used for multi-dimensional arrays in C or C++. But we have to remember that this is the syntax of jagged arrays, not for multi-dimensional arrays in C#.

For example, suppose I want to store trips to other cities made by our marketing executives in the first week of October, and there are three executives in my company's marketing department.

I'll declare a two-dimensional array, in which I'll have three rows and several columns. Because not every marketing executive has the same number of trips, I'll have to take the maximum possible number of trips as number of columns.

So, for one week, the maximum number of days is 7. This means that any executive can make a maximum of 7 trips when he is allowed to make maximum one trip per day.

I use a byte array here because the maximum value I want to store is 7 and that can be accommodated in a byte variable (without wasting memory with 'int' variables).

```byte [ , ] Trips ; // Declaring a two dimensional byte array
// Allocating memory space for 3 rows and 7 columns
Trips[ , ] = new byte [3,7] ;```

Now, suppose that marketing executive 1 makes only three trips in the week (on the 1st, 3rd, and 7th day of the week); executive 2 makes five trips (on the 1st, 2nd, 5th, 6th, and 7th day of the week); and executive 3 makes a daily trip to the nearby city.

As shown in Figure 1, we are wasting a total memory of six elements for executives 1 and 2. Although this may not look a major waste of memory chunk, it can be for the companies that bankroll these trips. Somehow, we have to save the precise memory space or use the concept of virtual memory, while in fact we are wasting the actual memory that we could utilize.

 1 3 7 1 2 5 6 7 1 2 3 4 5 6 7

Figure 1 Memory map using a multi-dimensional array.

The solution to our problem is the jagged array (array of arrays). With a jagged array, we can choose the number of columns or another array of any length for every defined row.

```byte[][] Trips ; // Declaring a two dimensional jagged array
Trips = new byte[3][] ; // Allocating memory space for 3 rows

// Allocating memory for 3 trips to the first executive
Trips[0] = new byte[3] ;
// Allocating memory for 5 trips to the second executive
Trips[1] = new byte[5] ;
// Allocating memory for 7 trips to the third executive
Trips[2] = new byte[7] ;```

As shown in Figure 2, we are using the exact memory space that we needed; we are not wasting a single byte in our storing process. Every executive's number of trips is different and uses the exact memory space he needs. (Here, we leave the overheads of memory incurred by jagged arrays because this is very small).

 1 3 7
 1 2 5 6 7
 1 2 3 4 5 6 7

Figure 2 Memory map using a jagged array.