Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// Task 3 Allocation and deallocation of mono-dimensional and bi-dimensional arr

ID: 3818740 • Letter: #

Question

// Task 3

Allocation and deallocation of mono-dimensional and bi-dimensional arrays represented by pointers

a. Declare and implement a function CreateArray(...) that returns a pointer to an array of n integers.

b. Declare and implement a function DeleteArray(...) that takes and deletes an array of integers.

c. Declare and implement a function CreateMatrix(...) that returns a pointer to an array of arrays of n × m floats.

d. Declare and implement a function Deletematrix(...) that takes and deletes this kind of matrix.

e. Declare and implement a function DisplayMatrix(...) that displays the address of the matrix of floats in the memory and all its elements.

Explanation / Answer

/* method to create an array */

int* createArray()
{
int *d = ( int * )malloc( 2 * sizeof( int ) );
printf("createArray");
return d;
}

/* method to delete an array */

void deleteArray(int *a)
{
free(a);
a=NULL;
printf("delete array");
}

/* method to create a matrix */

float** createMatrix(int row, int column) {

float** pArray = (float**)malloc(row*sizeof(float*));

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

    pArray[i] = (float*)malloc(column * sizeof(float));
}

return pArray;
}

/* method to delete a matrix */
void deleteMatrix(float **p,int rows)
{
     for (int i=0; i<rows; i++)
{
    free(*(p+i));
}
free(p);

}

/* method to display address and element value &*/
void displayMatrix(float **arr,int row , int column)
{

   for(int i=0; i<=row; i++)
   {
       for(int j=0;j<=column;j++)
       {
                 printf("value of elment=%f ", *(*(arr + i) + j));
                 printf("address of element=%u ",*(arr+i));
        
       }
    }

}