Prerequisite:
What’s a Jagged Array?
A jagged array is an array of arrays such that member arrays might be of various sizes, in 2D array phrases for every row we will have a variable variety of columns. All these arrays are often known as Jagged arrays.

Jagged Array in C++
Instance:
arr[3][] = 1 2 3 4 // arr[0][4] : 1st row have 4 columns 5 6 // arr[1][2] : 2nd row have 2 columns 7 8 9 // arr[2][3] : third row have 3 columns
Strategies to Create Jagged Array
Jagged Array might be applied in C++ in two methods:
- Utilizing a static array of pointers
- Utilizing dynamic 2D arrays
1. Utilizing a static array of pointers
- create ‘n’ numbers 1D-arrays (row1, row2, row3, ….. and so on.) the place n is not any of rows, the scale of every row array shall be No. of columns i.e, the variety of components in every row array will present no. of columns in that specific row.
- Create a 1D array of pointers, storing the bottom tackle of every row array.
- Create one other 1D array Sizes[] storing the scale of every row array (This helps whereas iterating every component).
To know concerning the array of pointers confer with an array of pointers article.
Under is the implementation of the above methodology:
C++
|
components in matrix kind as observe 1 2 3 4 5 6 7 8 9
2. Utilizing Dynamic 2D arrays
- Create row, col variables storing no. of rows and no. of columns.
- Create a Dynamic row array arr (array of pointers) that may retailer a “row” variety of addresses.
- Retailer dimension in one other 1D array Sizes[] (dimension of no. or Rows) to retailer no. of columns for every row component within the row array. (This helps whereas iterating every column for every row).
- Create a Dynamic Column array for every row component with dimension: sizes[i]
- Now every row component within the Row array has the Base tackle of every Column array.
Under is the implementation of the above methodology:
C++
|
components in matrix kind as observe 1 2 3 4 5 6 7 8 9