Create a program in the following way:
main
function, declare a static two-dimensional
array B with \(N\)
rows and \(M\) columns. Use the
directive #define
of a preprocessor.main
function.main
function. Print the obtained values inside the
main
function.main
function. Use a header:Modify the program in order to use a dynamic allocation to store the arrays:
// allocate a block of memory for elements
double *row = (double *) malloc(M * sizeof(double));
// free memory
free(row);
// allocate a continuous block of memory for all elements
double *elems = (double *) malloc(N * M * sizeof(double));
// allocate memory for the array storing pointers to the rows
double **B = (double **) malloc (N * sizeof(double));
// assign proper addresses of the rows to the pointers
for (int i = 0; i < N; i++)
{
B[i] = &elems[i*M];
}
// write a code here which uses the array
// free memory
free(B); // free memory used by the pointer array
free(elems); // free memory used by the elements