HomeSoftware DevelopmentJagged Arrays in C++ - GeeksforGeeks

Jagged Arrays in C++ – GeeksforGeeks


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

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++

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:

  1. Utilizing a static array of pointers
  2. 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++

#embrace <iostream>

utilizing namespace std;

  

int essential()

{

    

    

    int row1[] = { 1, 2, 3, 4 };

    int row2[] = { 5, 6 };

    int row3[] = { 7, 8, 9 };

  

    

    int* jagged[] = { row1, row2, row3 };

  

    int sizes[] = { 4, 2, 3 };

  

    cout << "components in matrix kind as observe" << endl;

    for (int i = 0; i < 3; i++) {

  

        

        int* ptr = jagged[i];

  

        for (int j = 0; j < sizes[i]; j++) {

            

            

  

            cout << *(ptr + j) << " ";

            

            

            

        }

        cout << endl;

    }

  

    return 0;

}

Output

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++

#embrace <iostream>

utilizing namespace std;

  

int essential()

{

    

  

    int row, col;

    row = 3;

  

    

    int** arr = new int*[row];

    int sizes[] = { 4, 2, 3 };

    

    

  

    

    for (int i = 0; i < row; i++) {

        

        

        

        

        

        

  

        *(arr + i) = new int[sizes[i]];

        

    }

  

    

    int num = 1;

    for (int i = 0; i < row; i++) {

        for (int j = 0; j < sizes[i]; j++) {

            

            arr[i][j] = num++;

        }

    }

  

    cout << "components in matrix kind as observe" << endl;

    for (int i = 0; i < row; i++) {

        for (int j = 0; j < sizes[i]; j++) {

            cout << arr[i][j] << " ";

        }

        cout << endl;

    }

  

    return 0;

}

Output

components in matrix kind as observe
1 2 3 4 
5 6 
7 8 9 

RELATED ARTICLES

Most Popular

Recent Comments