ex12-1.c
/* code: ex12-1.c (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 3000000
int main () {
int array[ARRAY_SIZE];
int i;
for (i = 0; i < ARRAY_SIZE; i++) {
array[i] = 100;
}
for (i = 0; i < 10; i++) {
printf ("%d ", array[i]);
}
return 0;
}
ex12-2.c
/* code: ex12-2.c (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 3000000
int array[ARRAY_SIZE];
int main () {
int i;
for (i = 0; i < ARRAY_SIZE; i++) {
array[i] = 100;
}
for (i = 0; i < 10; i++) {
printf ("%d ", array[i]);
}
return 0;
}
ex12-3.c
/* code: ex12-3.c (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 3000000
int main () {
int *array;
int i;
array = malloc (sizeof (int) * ARRAY_SIZE);
if (NULL == array) {
fprintf (stderr, "Error: malloc() \n");
exit (-1);
}
else {
for (i = 0; i < ARRAY_SIZE; i++) {
array[i] = 100;
}
for (i = 0; i < 10; i++) {
printf ("%d ", array[i]);
}
free (array);
}
return 0;
}
ex12-4.c
/* code: ex12-4.c (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------- */
int main () {
int **array;
int i, j, rows, columns;
rows = 768;
columns = 1024;
array = malloc (rows * sizeof (int *));
for (i = 0; i < rows; i++) {
array[i] = malloc (columns * sizeof (int));
}
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
array[i][j] = rand () % 10;
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf ("%d ", array[i][j]);
}
printf ("\n");
}
for (i = 0; i < rows; i++) {
free (array[i]);
}
free (array);
return 0;
}
ex12-5.c
/* code: ex12-5.c (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------- */
int main () {
int **array;
int i, j, rows, columns;
rows = 768;
columns = 1024;
array = malloc (rows * sizeof (int *));
array[0] = malloc (rows * columns * sizeof (int));
for (i = 1; i < rows; i++) {
array[i] = array[0] + i * columns;
}
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
array[i][j] = rand () % 10;
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf ("%d ", array[i][j]);
}
printf ("\n");
}
free (array[0]);
free (array);
return 0;
}
q12-1.c
/* code: q12-1.c (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------- */
int main () {
int ***array;
int i, j, k;
int x, y, z;
x = 10;
y = 20;
z = 30;
array = malloc (x * sizeof (int **));
for (i = 0; i < x; i++) {
array[i] = malloc (y * sizeof (int *));
for (j = 0; j < y; j++) {
array[i][j] = malloc (z * sizeof (int));
}
}
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
for (k = 0; k < z; k++) {
array[i][j][k] = rand () % 10;
}
}
}
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
for (k = 0; k < z; k++) {
printf ("%d ", array[i][j][k]);
}
}
}
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
free (array[i][j]);
}
free (array[i]);
}
free (array);
return 0;
}
q12-2.c
/* code: q12-2.c (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------- */
int **m2d_allocate (int rows, int columns) {
int **array;
int i;
array = malloc (rows * sizeof (int *));
for (i = 0; i < rows; i++) {
array[i] = malloc (columns * sizeof (int));
}
return array;
}
/* ------------------------------------------- */
void m2d_deallocate (int **array, int rows) {
int i;
for (i = 0; i < rows; i++) {
free (array[i]);
}
free (array);
}
/* ------------------------------------------- */
int main () {
int **array;
int i, j, rows, columns;
rows = 768;
columns = 1024;
array = m2d_allocate (rows, columns);
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
array[i][j] = rand () % 10;
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf ("%d ", array[i][j]);
}
printf ("\n");
}
m2d_deallocate (array, rows);
return 0;
}
EOF