Jagged Arrays in C++ Last Updated : 19 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Prerequisite: Arrays in C++ An array of pointers in C++Dynamic 2D arrays in C++What is a Jagged Array? A jagged array is an array of arrays such that member arrays can be of different sizes, in 2D array terms for each row we can have a variable number of columns. These types of arrays are also known as Jagged arrays. Jagged Array in C++ Example: 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] : 3rd row have 3 columnsMethods to Create Jagged Array Jagged Array can be implemented in C++ in two ways: Using a static array of pointersUsing dynamic 2D arrays1. Using a static array of pointerscreate 'n' numbers 1D-arrays (row1, row2, row3, ..... etc.) where n is no of rows, the size of each row array will be No. of columns i.e, the number of elements in each row array will show no. of columns in that particular row.Create a 1D array of pointers, storing the base address of each row array.Create another 1D array Sizes[] storing the size of each row array (This helps while iterating each element). To know about the array of pointers refer to an array of pointers article. Below is the implementation of the above method: C++ // C++ Program to implement Jagged Array // 1st way: static arrays #include <iostream> using namespace std; int main() { // create 3 row arrays having different sizes // ( no ofcolumns) int row1[] = { 1, 2, 3, 4 }; int row2[] = { 5, 6 }; int row3[] = { 7, 8, 9 }; // storing base address of each row array int* jagged[] = { row1, row2, row3 }; int sizes[] = { 4, 2, 3 }; cout << "elements in matrix form as follow" << endl; for (int i = 0; i < 3; i++) { // getting current(ith) row int* ptr = jagged[i]; for (int j = 0; j < sizes[i]; j++) { // for ith row having sizes[i] no. of // columns cout << *(ptr + j) << " "; // *ptr have base address // adding j means access jth // element for particular(ith) row } cout << endl; } return 0; } Outputelements in matrix form as follow 1 2 3 4 5 6 7 8 9 2. Using Dynamic 2D arraysCreate row, col variables storing no. of rows and no. of columns.Create a Dynamic row array arr (array of pointers) that can store a "row" number of addresses.Store size in another 1D array Sizes[] (size of no. or Rows) to store no. of columns for each row element in the row array. (This helps while iterating each column for each row).Create a Dynamic Column array for each row element with size: sizes[i] Now each row element in the Row array has the Base address of each Column array. Below is the implementation of the above method: C++ // C++ Program to implement Jagged array // 2nd way: Dynamic 2D array #include <iostream> using namespace std; int main() { // system("cls"); int row, col; row = 3; // Create Row Array- dynamic array of pointers int** arr = new int*[row]; int sizes[] = { 4, 2, 3 }; // int *sizes= new int[row]; // if taking row as input // no of columns for each row as input from user for (int i = 0; i < row; i++) { /* cin>>col; //if col is taken as input / sizes[i]=col; // store each col number in size ( if row and col // are taken as input) */ *(arr + i) = new int[sizes[i]]; // creating column of sizes[i] for each row } // input from user int num = 1; for (int i = 0; i < row; i++) { for (int j = 0; j < sizes[i]; j++) { // cin>>arr[i][j]; //if user want to input arr[i][j] = num++; } } cout << "elements in matrix form as follow" << endl; for (int i = 0; i < row; i++) { for (int j = 0; j < sizes[i]; j++) { cout << arr[i][j] << " "; } cout << endl; } return 0; } Outputelements in matrix form as follow 1 2 3 4 5 6 7 8 9 Comment More infoAdvertise with us Next Article Jagged Arrays in C++ sujal_06 Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads Array of Vectors in C++ STL Prerequisite: Arrays in C++, Vector in C++ STL An array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element. Vectors are known as dynam 3 min read Data Races in C++ In C++, data race is a commonly occurring problem in concurrent programming. It occurs when two or more threads concurrently access the same memory location, and at least one of the accesses is a write. Data races lead to undefined behaviour, which means the program can exhibit unpredictable behavio 5 min read Draw a line in C++ graphics graphics.h library is used to include and facilitate graphical operations in program. graphics.h functions can be used to draw different shapes, display text in different fonts, change colors and many more. Using functions of graphics.h you can make graphics programs, animations, projects and games. 2 min read Bitmask in C++ In the world of programming, where precision and efficiency are of great importance, bitmasking is a powerful technique that utilizes the manipulation of bits. C++ also offers a platform where programmers can efficiently represent data and unlock unparalleled computational capabilities through bit m 8 min read Balanced Binary Tree in C++ A Balanced Binary Tree, also known as a height-balanced binary tree, is a binary tree in which the height of every node's left and right subtrees differs by at most one. This balance condition ensures that the tree maintains a logarithmic height, which in turn guarantees O(log n) time complexity for 8 min read isless() in C/C++ In C++, isless() is a predefined function used for mathematical calculations. math.h is the header file required for various mathematical functions.isless() function used to check whether the 1st argument given to the function is less than the 2nd argument given to the function or not. Means if a is 2 min read std::quoted in C++ 14 std::quoted is an I/O manipulator function that was introduced in C++ 14 as the part of <iomanip> library. Its primary purpose is to handle the quoted string in the input and output operations. In this article, we will learn about the std::quoted manipulator, how it works and how to use it in 5 min read Array of list in C++ with Examples What is an array? An array in any programming language is a data structure that is used to store elements or data items of similar data types at contiguous memory locations and elements can be accessed randomly using indices of an array. Arrays are efficient when we want to store a large number of e 5 min read std::make_unique in C++ 14 std::make_unique is a utility function in C++ that was introduced in C++14. It is used to create a unique_ptr object, which is a smart pointer that manages the lifetime of dynamically allocated objects. It is defined inside <memory> header file. Syntaxstd::make_unique <object_type> (argu 2 min read getline() Function and Character Array in C++ The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered 2 min read Like