Answer to Question #157609 in C++ for Usama Saleem

Question #157609

Write a piece of code that dynamiccaly creates 1) a 1d char array of size 100 and 2) a 2d array having 4 rows where the first row has only one cell, second row has two cells, third row has three cell, fourth row has four cells. Now write the code to delete the arrays.


1
Expert's answer
2021-01-24T16:38:10-0500
int main()
{
    char *array1d = new char[100];

    char **array2d = new char*[4];
    for (int i = 0; i < 4; i++) {
        // for i=0, sets (0+1)=1 element to 0th row, and so on
        array2d[i] = new char[i + 1];
    }

    delete[] array1d;
    for (int i = 0; i < 4; i++)
        delete[] array2d[i];
    delete[] array2d;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog