Jagged Array In C++ - C++ Programming Tutorial
C++ Course / Arrays / Jagged Array In C++

Jagged Array In C++

BLUF: Mastering Jagged Array In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Jagged Array In C++

C++ is renowned for its efficiency. Learn how Jagged Array In C++ enables low-level control and high-performance computing in the tutorial below.

Introduction:

An array in C++ consists of elements of the same data type stored consecutively in memory. In contrast, a jagged array is a type of array where the number of columns can differ for each row. Another term for jagged arrays is "arrays of arrays." This post will explore the definition, application, and illustrations of jagged arrays in C++.

Definition of Jagged Array

Each row within a jagged array can consist of a different count of columns, making it a versatile structure. Due to its composition of multiple arrays, each with its unique count of elements, the jagged array is also referred to as an "array of arrays". The elements within the jagged array are arrays in their own right.

For example, consider the following jagged array:

Example

int jaggedArray[3][];
jaggedArray[0] = new int[2] {1, 2};
jaggedArray[1] = new int[3] {3, 4, 5};
jaggedArray[2] = new int[4] {6, 7, 8, 9};

In the previous instance, we created a jagged array consisting of three distinct arrays. The initial array held two elements, the second had three, and the third contained four elements.

The use of a jagged array

A jagged array in C++ can be generated by employing a two-dimensional array of pointers. The elements within the array consist of pointers that point to arrays containing numerical values. By using the new keyword, we have the ability to dynamically allocate memory for each individual row of the jagged array.

The ensuing C++ code showcases the process of constructing a jagged array:

Example

int main()
{
    int **jaggedArray;
    int rows = 3;
jaggedArray = new int *[rows];
jaggedArray[0] = new int[2] {1, 2};
jaggedArray[1] = new int[3] {3, 4, 5};
jaggedArray[2] = new int[4] {6, 7, 8, 9};

    // Accessing jagged array elements
cout<< "jaggedArray[0][0] = " <<jaggedArray[0][0] <<endl;
cout<< "jaggedArray[1][1] = " <<jaggedArray[1][1] <<endl;
cout<< "jaggedArray[2][2] = " <<jaggedArray[2][2] <<endl;

    // Deallocating memory
delete[] jaggedArray[0];
delete[] jaggedArray[1];
delete[] jaggedArray[2];
delete[] jaggedArray;

    return 0;
}

In the instance mentioned earlier, we employed a 2D array of pointers to build a jagged array. By utilizing the "new" keyword, we reserved memory for each row within the jagged array. Additionally, the square bracket notation was utilized to retrieve the elements of the jagged array.

By employing the delete keyword, we have successfully freed up the memory reserved for the jagged array. It is imperative to release the memory allocated for the jagged array to avoid memory leaks.

Examples of Jagged Array

Let's explore a few instances of uneven arrays to enhance our comprehension of them.

Example 1:

Various arrays of different lengths are stored within a jagged array:

Example

#include <iostream>
using namespace std;

int main()
{
    int* jaggedArray[3]; // declare a jagged array containing 3 rows

    // allocate memory for the rows and assign their addresses to jaggedArray
jaggedArray[0] = new int[2] {1, 2};
jaggedArray[1] = new int[3] {3, 4, 5};
jaggedArray[2] = new int[4] {6, 7, 8, 9};

    // print the elements of the jagged array
for(int i = 0; i< 3; i++) {
for(int j = 0; j < i+2; j++) {
cout<<jaggedArray[i][j] << " ";
        }
cout<<endl;
    }

    // deallocate memory for the rows
for(int i = 0; i< 3; i++) {
delete[] jaggedArray[i];
    }

    return 0;
}

Output:

Output

1 2
3 4 5
6 7 8 9

This outcome reveals the successful creation and display of a jagged array in C++.

Explanation:

In this instance, a jagged array has been constructed with three arrays. The initial array contains two elements, the second array contains three elements, and the third array contains four elements. Memory allocation for each row of the jagged array has been performed using the new keyword.

Following that, the components of the jagged array were displayed by employing a nested for loop. The internal loop cycles through the columns within each row, whereas the external loop traverses the rows of the jagged array. To restrict the inner loop's output to the elements in each row, we employed i+2 as the termination criterion. To ensure proper memory management, we ultimately released the allocated RAM for each row using the delete operator.

Example 2:

Various data types are stored within a jagged array:

Example

#include <iostream>
using namespace std;

int main()
{
    void *jaggedArray[3];
    int intArray[] = {1, 2};
    char charArray[] = {'a', 'b', 'c'};
    float floatArray[] = {1.1, 2.2, 3.3, 4.4};

jaggedArray[0] = &intArray;
jaggedArray[1] = &charArray;
jaggedArray[2] = &floatArray;

    // Accessing jagged array elements
    int *intPtr = static_cast<int*>(jaggedArray[0]);
cout<< "intArray[0] = " <<intPtr[0] <<endl;
cout<< "intArray[1] = " <<intPtr[1] <<endl;

    char *charPtr = static_cast<char*>(jaggedArray[1]);
cout<< "charArray[0] = " <<charPtr[0] <<endl;
cout<< "charArray[1] = " <<charPtr[1] <<endl;

    float *floatPtr = static_cast<float*>(jaggedArray[2]);
cout<< "floatArray[0] = " <<floatPtr[0] <<endl;
cout<< "floatArray[1] = " <<floatPtr[1] <<endl;

    return 0;
}

Output:

Output

intArray[0] = 1
intArray[1] = 2
charArray[0] = a
charArray[1] = b
floatArray[0] = 1.1
floatArray[1] = 2.2

Explanation:

In the previous example, we have created a jagged array that stores arrays of different data types. The arrays are stored in the jagged array using a void* pointer. Memory has been allocated for each array, and the corresponding element in the jagged array has been assigned the address of each array.

By converting the void pointer to the appropriate data type, we are able to access the individual elements of the jagged array. For example, by employing square brackets and converting the void pointer to an int* pointer, we can retrieve the initial element of the intArray. To confirm the successful execution of the program, we ultimately returned 0.

Conclusion

In this article, we explored the meaning, applications, and illustrations of jagged arrays in C++. Within a jagged array, each row can have a different quantity of columns. To represent a jagged array, a two-dimensional array can be employed.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience