1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define ARRAYS 3 6 #define LARGE_OF_ARRAYS 4 7 8 typedef struct { 9 size_t n, m; 10 int **a; 11 } array_t; 12 13 int arr_find_max(array_t *array); 14 void arr_create(size_t n, size_t m, array_t *array); 15 void arr_initran(array_t *array); 16 void arr_print(array_t *array); 17 void arr_destroy(array_t *array); 18 19 int main (void) 20 { 21 array_t array; 22 23 arr_create(ARRAYS, LARGE_OF_ARRAYS, &array); 24 25 arr_initran(&array); 26 27 arr_print(&array); 28 29 printf("\nThe maximum value is: %d\n", arr_find_max(&array)); 30 31 arr_destroy(&array); 32 return 0; 33 } 34 35 36 void arr_create(size_t n, size_t m, array_t *array) 37 { 38 array->n = n; 39 array->m = m; 40 41 array->a = (int **)malloc(n * sizeof(int *)); 42 43 for(size_t i = 0; i < n; i++) 44 *(array->a + i) = (int *)malloc(m * sizeof(int)); 45 } 46 47 void arr_destroy(array_t *array) 48 { 49 for(size_t i = 0; i < array->n; i++) 50 free(*(array->a + i)); 51 52 free(array->a); 53 } 54 55 void arr_initran(array_t *array) 56 { 57 srand((unsigned int)time(NULL)); 58 59 for(size_t i = 0; i < array->n; i++) 60 for(size_t j = 0; j < array->m; j++) 61 array->a[i][j] = ((double)rand()/(double)(RAND_MAX)) * 20; 62 63 } 64 65 void arr_print(array_t *array) 66 { 67 for(size_t i = 0; i < array->n; i++) { 68 for(size_t j = 0; j < array->m; j++) 69 printf("%2d ", array->a[i][j]); 70 71 putchar('\n'); 72 } 73 } 74 75 int arr_find_max(array_t *array) 76 { 77 int max = 0; 78 for(size_t i = 0; i < array->n; i++) 79 for(size_t j = 0; j < array->m; j++) 80 if(array->a[i][j] > max) 81 max = array->a[i][j]; 82 83 return max; 84 }