The Wayback Machine - https://web.archive.org/web/20240819104112/https://www.geeksforgeeks.org/jagged-array-or-array-of-arrays-in-c-with-examples/
Open In App

Jagged Array or Array of Arrays in C with Examples

Last Updated : 14 Feb, 2020
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Prerequisite: Arrays in C

Jagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These type of arrays are also known as Jagged arrays.

Example:

arr[][] = { {0, 1, 2},
            {6, 4},
            {1, 7, 6, 8, 9},
            {5} 
          };

Below are the methods to implement the jagged array in C:

  1. Using array and a pointer (Static Jagged Array)
    • First declare 1-D arrays with the number of rows you will need,
    • The size of each array (array for the elements in the row) will be the number of columns (or elements) in the row,
    • Then declare a 1-D array of pointers that will hold the addresses of the rows,
    • The size of the 1-D array is the number of rows you want in the jagged array.

    Below is the implementation of the above approach:
    Example:




    // C program to show the
    // implementation of Jagged Arrays
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
      
        int row0[4] = { 1, 2, 3, 4 };
        int row1[2] = { 5, 6 };
      
        int* jagged[2] = { row0, row1 };
      
        // Array to hold the size of each row
        int Size[2] = { 4, 2 }, k = 0;
      
        // To display elements of Jagged array
        for (int i = 0; i < 2; i++) {
      
            // pointer to hold the address of the row
            int* ptr = jagged[i];
      
            for (int j = 0; j < Size[k]; j++) {
                printf("%d ", *ptr);
      
                // move the pointer to the
                // next element in the row
                ptr++;
            }
      
            printf("\n");
            k++;
      
            // move the pointer to the next row
            jagged[i]++;
        }
      
        return 0;
    }

    
    

    Output:

    1 2 3 4 
    5 6
    
  2. Using an array of pointer (Dynamic Jagged Array)
    • Declare an array of pointers (jagged array),
    • The size of this array will be the number of rows required in the Jagged array
    • Then for each pointer in the array allocate memory for the number of elements you want in this row.

    Below is the implementation of the above approach:
    Example:




    // C program to show the
    // implementation of Jagged Arrays
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
        // 2 Rows
        int* jagged[2];
      
        // Allocate memory for elements in row 0
        jagged[0] = malloc(sizeof(int) * 1);
      
        // Allocate memory for elements in row 1
        jagged[1] = malloc(sizeof(int) * 3);
      
        // Array to hold the size of each row
        int Size[2] = { 1, 3 }, k = 0, number = 100;
      
        // User enters the numbers
        for (int i = 0; i < 2; i++) {
      
            int* p = jagged[i];
      
            for (int j = 0; j < Size[k]; j++) {
                *p = number++;
      
                // move the pointer
                p++;
            }
            k++;
        }
      
        k = 0;
      
        // Display elements in Jagged array
        for (int i = 0; i < 2; i++) {
      
            int* p = jagged[i];
            for (int j = 0; j < Size[k]; j++) {
      
                printf("%d ", *p);
                // move the pointer to the next element
                p++;
            }
            printf("\n");
            k++;
            // move the pointer to the next row
            jagged[i]++;
        }
      
        return 0;
    }

    
    

    Output:

    100 
    101 102 103
    


Previous Article
Next Article

Similar Reads

k-Rough Number or k-Jagged Number
A k-rough or k-jagged number is a number whose smallest prime factor is greater than or equal to the number 'k'. Given numbers 'n' and 'k' as input, we are required to find whether 'n; is a k-rough number or not.Examples : Input : n = 10, k = 2 Output : 10 is a 2-rough number Explanation: The prime factors of 10 are 2 and 5 Hence its smallest prime
14 min read
Maximum OR sum of sub-arrays of two different arrays
Given two arrays of positive integers. Select two sub-arrays of equal size from each array and calculate maximum possible OR sum of the two sub-arrays. Note: Let f(x, l, r) is the OR sum of all the elements in the range [l, r] in array x. Examples : Input : A[] = {1, 2, 4, 3, 2} B[] = {2, 3, 3, 12, 1} Output : 22 Explanation: Here, one way to get m
5 min read
Merge k sorted arrays | Set 2 (Different Sized Arrays)
Given k sorted arrays of possibly different sizes, merge them and print the sorted output.Examples: Input: k = 3 arr[][] = { {1, 3}, {2, 4, 6}, {0, 9, 10, 11}} ;Output: 0 1 2 3 4 6 9 10 11 Input: k = 2 arr[][] = { {1, 3, 20}, {2, 4, 6}} ;Output: 1 2 3 4 6 20 We have discussed a solution that works for all arrays of the same size in Merge k sorted a
7 min read
Minimize sum of product of same-indexed elements of two arrays by reversing a subarray of one of the two arrays
Given two equal-length arrays A[] and B[], consisting only of positive integers, the task is to reverse any subarray of the first array such that sum of the product of same-indexed elements of the two arrays, i.e. (A[i] * B[i]) is minimum. Examples: Input: N = 4, A[] = {2, 3, 1, 5}, B[] = {8, 2, 4, 3} Output: A[] = 1 3 2 5 B[] = 8 2 4 3 Minimum pro
12 min read
Find sub-arrays from given two arrays such that they have equal sum
Given two arrays A[] and B[] of equal sizes i.e. N containing integers from 1 to N. The task is to find sub-arrays from the given arrays such that they have equal sum. Print the indices of such sub-arrays. If no such sub-arrays are possible then print -1.Examples: Input: A[] = {1, 2, 3, 4, 5}, B[] = {6, 2, 1, 5, 4} Output: Indices in array 1 : 0, 1
15+ min read
Count of possible arrays from prefix-sum and suffix-sum arrays
Given 2*N integers which are elements of a prefix and suffix array(in shuffled order) of an array of size N, the task is to find the no of possible array's of the size N which can be made from these elements Examples: Input: arr[] = {5, 2, 3, 5} Output: 2 Explanation: 1st array can be : {2, 3} Its prefix array:{2, 5} Its suffix array:{5, 3} 2nd arr
15+ min read
Difference between Static Arrays and Dynamic Arrays
In the Data structure, we know that an array plays a vital role in having similar types of elements arranged in a contiguous manner with the same data type. According to the concept of array, an array can be defined in two ways as per the memory allocation concept. Types of Arrays:There are basically two types of arrays: Static Array: In this type
5 min read
Generate all possible sorted arrays from alternate elements of two given sorted arrays
Given two sorted arrays A and B, generate all possible arrays such that the first element is taken from A then from B then from A, and so on in increasing order till the arrays are exhausted. The generated arrays should end with an element from B. Example: A = {10, 15, 25} B = {1, 5, 20, 30} The resulting arrays are: 10 20 10 20 25 30 10 30 15 20 1
12 min read
Count of possible unique arrays after swapping elements at same index of given Arrays
Given two arrays arr1[] and arr2[] with distinct elements of size N.The task is to count the total number of possible combinations after swapping elements at the same index of both the arrays such that there are no duplicates in both the arrays after performing the operation. Examples: Input: arr1[] = {1, 2, 3, 4}, arr2[] = {2, 1, 4, 3}, N = 4Outpu
12 min read
Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
Give an integer array arr[] consisting of elements from the set {0, 1}. The task is to print the number of ways the array can be divided into sub-arrays such that each sub-array contains exactly one 1. Examples: Input: arr[] = {1, 0, 1, 0, 1} Output: 4 Below are the possible ways: {1, 0}, {1, 0}, {1}{1}, {0, 1, 0}, {1}{1, 0}, {1}, {0, 1}{1}, {0, 1}
6 min read
Maximum sub-array sum after dividing array into sub-arrays based on the given queries
Given an array arr[] and an integer k, we can cut this array at k different positions where k[] stores the positions of all the cuts required. The task is to print maximum sum among all the cuts after every cut made. Every cut is of the form of an integer x where x denotes a cut between arr[x] and arr[x + 1]. Examples: Input: arr[] = {4, 5, 6, 7, 8
9 min read
Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
Given an array of integers arr[], the task is to count the number of sub-arrays such that the average of elements present in the sub-array is greater than the average of elements that are not present in the sub-array.Examples: Input: arr[] = {6, 3, 5} Output: 3 The sub-arrays are {6}, {5} and {6, 3, 5} because their averages are greater than {3, 5}
8 min read
Maximize the size of array by deleting exactly k sub-arrays to make array prime
Given an array arr[] of N positive integers and a non-negative integer K. The task is to delete exactly K sub-arrays from the array such that all the remaining elements of the array are prime and the size of the remaining array is maximum possible. Examples: Input: arr[] = {2, 4, 2, 2, 4, 2, 4, 2}, k = 2 Output: 4 Delete the subarrays arr[1] and ar
10 min read
Minimize swaps between two arrays such that sum of the first array exceeds sum of the second array
Given two arrays arr1[] and arr2[] of size N and M respectively, the task is to count the minimum number of swaps required between the two arrays in order to make the sum of the array arr1[] greater than the arr2[]. Examples: Input: arr1[] = {1, 3, 2, 4}, arr2[] = {6, 7, 8}Output: 1Explanation:Swapping arr1[0] with arr2[2] makes the sum of arr1[] e
10 min read
Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
Given two arrays arr1[] and arr2[] of lengths N and M respectively, the task is to find the maximum number of pairs (i, j) such that 2 * arr1[i] â‰Ī arr2[j] (1 â‰Ī i â‰Ī N and 1 â‰Ī j â‰Ī M). Note: Any array element can be part of a single pair. Examples: Input: N = 3, arr1[] = {3, 2, 1}, M = 4, arr2[] = {3, 4, 2, 1}Output: 2Explanation:Only two pairs can be
7 min read
Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array
Given two arrays arr1[] and arr2[] consisting of N and M integers respectively, the task is to find the probability of randomly selecting the two numbers from arr1[] and arr2[] respectively, such that the first selected element is strictly less than the second selected element. Examples: Input: arr1[] = {3, 2, 1, 1}, arr2[] = {1, 2, 5, 2}Output: 0.
15+ min read
Bitwise XOR of same indexed array elements after rearranging an array to make XOR of same indexed elements of two arrays equal
Given two arrays A[] and B[] consisting of N positive integers, the task is to the Bitwise XOR of same indexed array elements after rearranging the array B[] such that the Bitwise XOR of the same indexed elements of the arrays A[] becomes equal. Examples: Input: A[] = {1, 2, 3}, B[] = {4, 6, 7}Output: 5Explanation:Below are the possible arrangement
14 min read
Find all possible original Arrays using given Difference Array and range of array elements
Given an array arr[] of size N that contains the differences between the adjacents elements of an array. The task is to generate all possible arrays such that all the elements of the array lie in the range [L, R]. Examples: Input: arr[] = {1, -3, 4}, L = 1, R = 6Output: {3, 4, 1, 5}, {4, 5, 2, 6}Explanation: These two are the only possible sequence
6 min read
Check if two sorted arrays can be merged to form a sorted array with no adjacent pair from the same array
Given two sorted arrays A[] and B[] of size N, the task is to check if it is possible to merge two given sorted arrays into a new sorted array such that no two consecutive elements are from the same array. Examples: Input: A[] = {3, 5, 8}, B[] = {2, 4, 6}Output: Yes Explanation: Merged array = {B[0], A[0], B[1], A[1], B[2], A[2]} Since the resultan
15+ min read
Merging elements of two different arrays alternatively in third array
Given two arrays arr1[] and arr2[], we need to combine two arrays in such a way that the combined array has alternate elements of both. If one array has extra element, then these elements are appended at the end of the combined array. Input : arr1[] = {1, 2, 3, 4, 5, 6} arr2[] = {11, 22, 33, 44}Output: {1, 11, 2, 22, 3, 33, 4, 44, 5, 6} Input : arr
7 min read
Maximum array from two given arrays keeping order same
Given two same-sized arrays A[] and B[] (both arrays contain distinct elements individually but may have some common elements), the task is to form a third (or result) array of the same size. The resulting array should have maximum n elements from both arrays. It should have chosen elements of A[] first, then chosen elements of B[] in the same orde
8 min read
Check if array can be divided into two sub-arrays such that their absolute difference is K
Given an array arr[] and an integer K, the task is to find whether the array can be divided into two sub-arrays such that the absolute difference of the sum of the elements of both the sub-arrays is K. Examples: Input: arr[] = {2, 4, 5, 1}, K = 0 Output: Yes {2, 4} and {5, 1} are the two possible sub-arrays. |(2 + 4) - (5 + 1)| = |6 - 6| = 0 Input:
6 min read
Find the sums for which an array can be divided into sub-arrays of equal sum
Given an array of integers arr[], the task is to find all the values for sum such that for a value sum[i] the array can be divided into sub-arrays of sum equal to sum[i]. If array cannot be divided into sub-arrays of equal sum then print -1. Examples: Input: arr[] = {2, 2, 2, 1, 1, 2, 2} Output: 2 4 6 The array can be divided into sub-arrays of sum
10 min read
Count arrays of length K whose product of elements is same as that of given array
Given an integer array arr[] of length N and an integer K, the task is to count the number of possible arrays of length K such that the product of all elements of that array is equal to the product of all elements of the given array arr[]. Since the answer can be very large, return the answer modulo 109 + 7. Examples: Input: arr[] = {2, 3}, K = 3 O
15+ min read
Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
Given three integers N, E and O. The task is to find an array of size N such that the number of sub-arrays of sum even and odd are E and O respectively.Examples: Input: N = 3, E = 2, O = 4 Output: 0 1 0 There are total 6 sub-arrays: {0}, {1}, {0}, {0, 1}, {1, 0}, {0, 1, 0}. Their sums are {0, 1, 0, 1, 1, 1} respectively. 2 of them are even and 4 of
10 min read
Number of ways to divide an array into K equal sum sub-arrays
Given an integer K and an array arr[] of N integers, the task is to find the number of ways to split the array into K equal sum sub-arrays of non-zero lengths. Examples: Input: arr[] = {0, 0, 0, 0}, K = 3 Output: 3 All possible ways are: {{0}, {0}, {0, 0}} {{0}, {0, 0}, {0}} {{0, 0}, {0}, {0}}Input: arr[] = {1, -1, 1, -1}, K = 2 Output: 1 Approach:
9 min read
Maximise array sum after taking non-overlapping sub-arrays of length K
Given an integer array arr[] of length N and an integer K, the task is to select some non-overlapping sub-arrays such that each sub-array is exactly of length K, no two sub-arrays are adjacent and sum of all the elements of the selected sub-arrays is maximum.Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 2 Output: 12 Sub-arrays that maximizes sum wi
8 min read
Check whether array has all identical elements using Arrays.asList() and HashSet in Java
Given an array arr[] of N elements, the task is to check whether the array have all same (identical) elements or not without using the loop. If all elements are same then print Yes otherwise print No. Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: Yes The given array has all identical elements i.e. 2. Input: arr[] = {2, 3, 3, 3, 3, 2, 2} Output:
2 min read
Divide the array into minimum number of sub-arrays having unique elements
Given an array arr. The task is to divide the array into the minimum number of subarrays containing unique elements and return the count of such subarrays. Note: An array element cannot be present in more than one subarray.Examples : Input : arr[] = {1, 2, 1, 1, 2, 3}Output : 3Explanation : The subarrays having unique elements are { 1, 2 }, { 1 },
8 min read
Choose two elements from the given array such that their sum is not present in any of the arrays
Given two arrays A[] and B[], the task is to choose two elements X and Y such that X belongs to A[] and Y belongs to B[] and (X + Y) must not be present in any of the array.Examples: Input: A[] = {3, 2, 2}, B[] = {1, 5, 7, 7, 9} Output: 3 9 3 + 9 = 12 and 12 is not present in any of the given arrays.Input: A[] = {1, 3, 5, 7}, B[] = {7, 5, 3, 1} Out
5 min read
three90RightbarBannerImg