Question #157631

 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.


Expert's answer

int main()
{
    const int ARRAY_SIZE_1 = 100;
    const int ARRAY_SIZE_2 = 4;

    char* array1 = new char[ARRAY_SIZE_1];
    
    char** array2 = new char*[ARRAY_SIZE_2];
    for(int i=0; i<ARRAY_SIZE_2; ++i)
    {
        array2[i] = new char[i+1];
    }
	
    delete[] array1;

    for(int i=0; i<ARRAY_SIZE_2; ++i)
    {
        delete[] array2[i];
    }
    
    delete[] array2;
    
    return 0;    
}
LATEST TUTORIALS
APPROVED BY CLIENTS