Friday, July 8, 2022
HomeWordPress DevelopmentThe way to create a Checklist (or Array) contained in the one...

The way to create a Checklist (or Array) contained in the one other Checklist (or Array)?


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

Dynamic Language like python can retailer totally different information varieties in the identical listing, however for statically typed language like C++, a listing means a set of comparable information varieties.  Each listing merchandise might be accessed by its indices, and for a lot of the languages (python, java, C++, and so on.) indices begin from 0.

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

Implementation of Checklist inside Checklist utilizing C++:

C++

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int most important()

{

    

    

    

    

    

    listing<listing<int> > ListofList;

  

    

    

    listing<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 Checklist inside Checklist utilizing Python:

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

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 primary array ought to retailer (int*) datatype then solely, one other 1D array might be saved/created inside that array.

Observe: Following the identical manner an array might be created inside one other array in C++ additionally.

C++

  

#embrace <bits/stdc++.h>

utilizing namespace std;

#outline dimension 5

  

int most important()

{

    

    

    

    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

#embrace <stdio.h>

#embrace <stdlib.h>

#outline dimension 5

  

int most important()

{

    

    

    

    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

- Advertisment -
Google search engine

Most Popular

Recent Comments