#include #include #include void arrays(); void matrixes(); void multidimensional_arrays(); int main() { srand( time(NULL) ); arrays(); matrixes(); multidimensional_arrays(); return 0; } void print_array(long double array[], size_t size) { printf("\t{"); for ( size_t index = 0; index < size; ++index ) printf("%s %Lg", index ? "," : "", array[index] ); printf(" }\n"); } void arrays() { // 1. Literal array (continuous) long double literal_array[] = {-1.0L, -0.5L, 0.0L, 0.5L, 1.0L}; size_t literal_size = sizeof(literal_array) / sizeof(literal_array[0]); printf("literal_array:\n"); print_array(literal_array, literal_size); printf("\tcount: %zu elements\n", literal_size); printf("\tsizeof: %zu bytes\n", sizeof(literal_array)); // 2. Automatic array (continuous) size_t automatic_size = 3 + rand() % 8; long double automatic_array[automatic_size]; for ( size_t index = 0; index < automatic_size; ++index ) automatic_array[index] = index; printf("\nautomatic_array:\n"); print_array(automatic_array, automatic_size); printf("\tcount: %zu elements\n", automatic_size); printf("\tsizeof: %zu bytes\n", sizeof(automatic_array)); // 3. Dynamic allocated array (not Dynamic array) size_t dynamic_alloc_size = 3 + rand() % 8; long double* dynamic_alloc_array = (long double*)malloc(dynamic_alloc_size * sizeof(long double)); for ( size_t index = 0; index < dynamic_alloc_size; ++index ) dynamic_alloc_array[index] = index; printf("\ndynamic_alloc_array:\n"); print_array(dynamic_alloc_array, dynamic_alloc_size); printf("\tcount: %zu elements\n", dynamic_alloc_size); printf("\tsizeof: %zu bytes (long double*)\n", sizeof(dynamic_alloc_array)); free(dynamic_alloc_array); } void print_literal_matrix(size_t rows, size_t cols, long double matrix[rows][cols]) { for ( size_t row_index = 0; row_index < rows; ++row_index ) { putchar('\t'); for ( size_t col_index = 0; col_index < cols; ++col_index ) printf("%5Lg ", matrix[row_index][col_index] ); putchar('\n'); } } void print_continous_matrix(long double* matrix, size_t rows, size_t cols) { for ( size_t row_index = 0; row_index < rows; ++row_index ) { putchar('\t'); for ( size_t col_index = 0; col_index < cols; ++col_index ) printf("%5Lg ", matrix[row_index * cols + col_index] ); putchar('\n'); } } void print_uncontinous_matrix(long double** matrix, size_t rows, size_t cols) { for ( size_t row_index = 0; row_index < rows; ++row_index ) { printf("\t{"); for ( size_t col_index = 0; col_index < cols; ++col_index ) printf("%5Lg ", matrix[row_index][col_index] ); printf(" }\n"); } } void literal_matrix() { long double matrix[][3] = { {1.1L, 1.2L, 1.3L}, {2.1L, 2.2L, 2.3L}, }; size_t cols = 3; size_t rows = sizeof(matrix) / cols / sizeof(matrix[0][0]); printf("\nliteral_matrix:\n"); print_literal_matrix(rows, cols, matrix); printf("\tcount: %zu rows x %zu cols\n", rows, cols); printf("\tsizeof: %zu bytes\n", sizeof(matrix)); } void automatic_matrix() { // 2. Automatic matrix (continuous) size_t rows = 3 + rand() % 8; size_t cols = 3 + rand() % 8; long double matrix[rows][cols]; for ( size_t row_index = 0; row_index < rows; ++row_index ) for ( size_t col_index = 0; col_index < cols; ++col_index ) matrix[row_index][col_index] = (row_index + 1) + (col_index + 1) / 10.0; printf("\nautomatic_matrix:\n"); print_literal_matrix(rows, cols, matrix); printf("\tcount: %zu rows x %zu cols\n", rows, cols); printf("\tsizeof: %zu bytes\n", sizeof(matrix)); } void continous_dynamic_allocated_matrix() { // 3. Dynamic allocated matrix (continous) (not Dynamic matrix) size_t rows = 3 + rand() % 8; size_t cols = 3 + rand() % 8; long double* matrix = (long double*)malloc(rows * cols * sizeof(long double)); for ( size_t row_index = 0; row_index < rows; ++row_index ) for ( size_t col_index = 0; col_index < cols; ++col_index ) matrix[row_index * cols + col_index] = (row_index + 1) + (col_index + 1) / 10.0; printf("\ndynamic_alloc_matrix:\n"); print_continous_matrix(matrix, rows, cols); printf("\tcount: %zu rows x %zu cols\n", rows, cols); printf("\tsizeof: %zu bytes (long double*)\n", sizeof(matrix)); free(matrix); } void uncontinous_dynamic_allocated_matrix() { // 4. Dynamic allocated matrix (uncontinous) (not Dynamic matrix) size_t rows = 3 + rand() % 8; size_t cols = 3 + rand() % 8; // Allocate a vector of vectors long double** matrix = (long double**)malloc(rows * sizeof(long double*)); for ( size_t row_index = 0; row_index < rows; ++row_index ) matrix[row_index] = (long double*)malloc(cols * sizeof(long double)); // Initialize values for ( size_t row_index = 0; row_index < rows; ++row_index ) for ( size_t col_index = 0; col_index < cols; ++col_index ) matrix[row_index][col_index] = (row_index + 1) + (col_index + 1) / 10.0; printf("\nuncontinous_dynamic_alloc_matrix:\n"); print_uncontinous_matrix(matrix, rows, cols); printf("\tcount: %zu rows x %zu cols\n", rows, cols); printf("\tsizeof: %zu bytes (long double**)\n", sizeof(matrix)); // Free each row first, then the vector of vectors for ( size_t row_index = 0; row_index < rows; ++row_index ) free(matrix[row_index]); free(matrix); } void matrixes() { literal_matrix(); automatic_matrix(); continous_dynamic_allocated_matrix(); uncontinous_dynamic_allocated_matrix(); } void multidimensional_arrays() { // 3. multidimensional array long double multidimensional_array[2][3][4] = { { {11.1L, 11.2L, 11.3L, 11.4L}, {12.1L, 12.2L, 12.3L, 12.4L}, {13.1L, 13.2L, 13.3L, 13.4L}, }, { {21.1L, 21.2L, 21.3L, 21.4L}, {22.1L, 22.2L, 22.3L, 22.4L}, {23.1L, 23.2L, 23.3L, 23.4L}, }, }; printf("\nmultidimensional_array:\n"); printf("\tsizeof: %zu\n", sizeof(multidimensional_array)); // To be done... }