Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,988 questions

51,933 answers

573 users

How to use jagged array in C++

2 Answers

0 votes
#include <iostream>

// Function to print the jagged array
void printJaggedArray(int* jaggedArray[], int rowCount) {
    for (int i = 0; i < rowCount; i++) {
        int colCount = (i == 0 ? 2 : (i == 1 ? 4 : 3));
        for (int j = 0; j < colCount; j++) {
            std::cout << jaggedArray[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

int main() {
    // Step 1: Declare a jagged array with 3 rows
    int* jaggedArray[3];

    // Step 2: Initialize the rows with different lengths
    jaggedArray[0] = new int[2];
    jaggedArray[1] = new int[4];
    jaggedArray[2] = new int[3];

    // Step 3: Assign values to the elements
    jaggedArray[0][0] = 1;
    jaggedArray[0][1] = 2;

    jaggedArray[1][0] = 3;
    jaggedArray[1][1] = 4;
    jaggedArray[1][2] = 5;
    jaggedArray[1][3] = 6;

    jaggedArray[2][0] = 7;
    jaggedArray[2][1] = 8;
    jaggedArray[2][2] = 9;

    // Step 4: Print jaggedArray 
    printJaggedArray(jaggedArray, 3);

    // Step 5: Deallocate memory
    for (int i = 0; i < 3; ++i) {
        delete[] jaggedArray[i];
    }
}


 
/*
run:
 
1 2 
3 4 5 6 
7 8 9 
 
*/

 



answered Mar 4, 2025 by avibootz
edited Mar 4, 2025 by avibootz
0 votes
#include <iostream>

// Function to create the jagged array
int** createJaggedArray() {
    // Step 1: Declare a jagged array with 3 rows
    int** jaggedArray = new int*[3];

    // Step 2: Initialize the rows with different lengths
    jaggedArray[0] = new int[2];
    jaggedArray[1] = new int[4];
    jaggedArray[2] = new int[3];

    // Step 3: Assign values to the elements
    jaggedArray[0][0] = 1;
    jaggedArray[0][1] = 2;

    jaggedArray[1][0] = 3;
    jaggedArray[1][1] = 4;
    jaggedArray[1][2] = 5;
    jaggedArray[1][3] = 6;

    jaggedArray[2][0] = 7;
    jaggedArray[2][1] = 8;
    jaggedArray[2][2] = 9;

    return jaggedArray;
}

// Function to print the jagged array
void printJaggedArray(int* jaggedArray[], int rowCount) {
    for (int i = 0; i < rowCount; i++) {
        int colCount = (i == 0 ? 2 : (i == 1 ? 4 : 3));
        for (int j = 0; j < colCount; j++) {
            std::cout << jaggedArray[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

int main() {
    // Create jagged array using the function
    int** jaggedArray = createJaggedArray();

    // Print jagged array
    printJaggedArray(jaggedArray, 3);

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


 
/*
run:
 
1 2 
3 4 5 6 
7 8 9 
 
*/

 



answered Mar 4, 2025 by avibootz

Related questions

2 answers 163 views
1 answer 55 views
55 views asked Mar 5, 2025 by avibootz
1 answer 53 views
53 views asked Mar 5, 2025 by avibootz
1 answer 58 views
58 views asked Mar 5, 2025 by avibootz
2 answers 65 views
65 views asked Mar 5, 2025 by avibootz
...