Friday, July 8, 2022
HomeSoftware DevelopmentEasy methods to create a Listing (or Array) contained in the one...

Easy methods to create a Listing (or Array) contained in the one other Listing (or Array)?


A checklist is a set of comparable or several types of information parts.

Dynamic Language like python can retailer completely different information varieties in the identical checklist, however for statically typed language like C++, an inventory means a set of comparable information varieties.  Each checklist merchandise will be accessed by its indices, and for a lot of the languages (python, java, C++, and many others.) indices begin from 0.

An array is similar because the checklist for dynamic language, however for a language like C++ the place for an inventory dynamic reminiscence allocation is completed by the compiler, for array consumer has to do it manually.

Implementation of Listing inside Listing utilizing C++:

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

int principal()

{

    

    

    

    

    

    checklist<checklist<int> > ListofList;

  

    

    

    checklist<int> insertedList;

    for (int i = 1; i < 5; i++) {

        insertedList.push_back(i);

    }

  

    

    ListofList.push_back(insertedList);

    ListofList.push_back(insertedList);

  

    for (auto it = ListofList.start();

         it != ListofList.finish(); it++) {

        for (int j : *it)

            cout << j << " ";

        cout << endl;

    }

    return 0;

}

Implementation of Listing inside Listing utilizing Python:

For Dynamic Language like python Create 2 or extra lists and append them inside one of many lists, And it’ll create an inventory inside an inventory,

Python3

list_1 = [1, 2, 3, 4]

list_2 = [4, 7, 8, 9]

list_3 = [3, 1, 4, 7]

list_4 = [3, 5, 7, 11]

  

list_1.append(list_2)

list_1.append(list_3)

list_1.append(list_4)

  

print(list_1)

Output

[1, 2, 3, 4, [4, 7, 8, 9], [3, 1, 4, 7], [3, 5, 7, 11]]

Creating array contained in the array in C:

However for statically typed language like C one should predefine every part like the information sort of the array which shops one other array. 

For instance, to create a 1D integer array inside one other array, firstly, the principle array ought to retailer (int*) datatype then solely, one other 1D array will be saved/created inside that array.

Notice: Following the identical method an array will be created inside one other array in C++ additionally.

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

#outline dimension 5

  

int principal()

{

    

    

    

    int** out_array = (int**)malloc(dimension * sizeof(int*));

  

    

    

    

    int* temp1 = (int*)malloc(dimension * sizeof(int));

    int* temp2 = (int*)malloc(dimension * sizeof(int));

  

    for (int i = 0; i < dimension; i++) {

        temp1[i] = i;

        temp2[i] = i;

    }

  

    

    

    

    out_array[0] = temp1;

    out_array[1] = temp2;

  

    cout << "After Creating array inside array " << endl;

  

    

    

    for (int i = 0; i < 2; i++) {

        for (int j = 0; j < dimension; j++) {

            cout << out_array[i][j] << " ";

        }

        cout << endl;

    }

    return 0;

}

C

#embody <stdio.h>

#embody <stdlib.h>

#outline dimension 5

  

int principal()

{

    

    

    

    int** out_array = (int**)malloc(dimension * sizeof(int*));

  

    

    

    

    int* temp1 = (int*)malloc(dimension * sizeof(int));

    int* temp2 = (int*)malloc(dimension * sizeof(int));

  

    for (int i = 0; i < dimension; i++) {

        temp1[i] = i;

        temp2[i] = i;

    }

  

    

    

    

    out_array[0] = temp1;

    out_array[1] = temp2;

  

    printf("After Creating array inside arrayn");

  

    

    

    for (int i = 0; i < 2; i++) {

        for (int j = 0; j < dimension; j++) {

            printf("%d ", out_array[i][j]);

        }

        printf("n");

    }

    return 0;

}

Output

After Creating array inside array 
0 1 2 3 4 
0 1 2 3 4 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments