Tutorial 06

 -----------JaggedArray.cpp

#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;  

}  

2 comments:

  1. Sir, I just wanted to say thank you for all the great resources you’ve created. I’m using them to learn DSA and I’m really impressed with how well they’re helping me. I’m so glad I had the opportunity to learn PF Lab from you in university.

    ReplyDelete
    Replies
    1. Thanks don't worry you may learn all here with ease in future as well, stay in touch for more. Do share and support!

      Delete

Fell free to write your query in comment. Your Comments will be fully encouraged.