#include #include #define USE_SUBSCRIPT_NOTATION 1 #define USE_POINTER_NOTATION 0 int test_automatic_matrix() { // Read dimensions. They must be larger than 0 size_t rows = 0, cols = 0; if ( scanf("%zu %zu\n", &rows, &cols) != 2 ) return 1; if ( rows * cols == 0 ) return 2; // Creates the matrix in stack segment: dangerous long double values[rows][cols]; // Read values for ( size_t row_index = 0; row_index < rows; ++row_index ) { for ( size_t col_index = 0; col_index < cols; ++col_index ) { #if USE_SUBSCRIPT_NOTATION if ( scanf("%Lg", &values[row_index][col_index]) != 1 ) return 3; #endif #if USE_POINTER_NOTATION if ( scanf("%Lg", *(values + row_index) + col_index) != 1 ) return 3; #endif } } // Print values for ( size_t row_index = 0; row_index < rows; ++row_index ) { for ( size_t col_index = 0; col_index < cols; ++col_index ) { #if USE_SUBSCRIPT_NOTATION printf("%Lg ", values[row_index][col_index]); #endif #if USE_POINTER_NOTATION printf("%Lg ", *(*(values + row_index) + col_index)); #endif } putchar('\n'); // fputc(stdout, '\n'); } // Matrix is automatically deallocated when function call ends return 0; } int dynamic_allocated_matrix() { // Read dimensions. They must be larger than 0 size_t rows = 0, cols = 0; if ( scanf("%zu %zu\n", &rows, &cols) != 2 ) return 4; if ( rows * cols == 0 ) return 5; // Create array in heap segment (dynamic memory): safer #if USE_SUBSCRIPT_NOTATION // Request for un-continous memory, i.e an array of arrays long double** values = (long double**)malloc(rows * sizeof(long double*)); if ( values == NULL ) return 6; // We now have a pointer array only // We have to create cells to store long double values for ( size_t row_index = 0; row_index < rows; ++row_index ) if ( (values[row_index] = (long double*)malloc(cols * sizeof(long double))) == NULL ) return 7; #endif #if USE_POINTER_NOTATION // Request for continous memory, i.e a matrix stored as an array long double* values = malloc(rows * cols * sizeof(long double)); if ( values == NULL ) return 8; #endif // Read values for ( size_t row_index = 0; row_index < rows; ++row_index ) { for ( size_t col_index = 0; col_index < cols; ++col_index ) { #if USE_SUBSCRIPT_NOTATION scanf("%Lg", &values[row_index][col_index]); #endif #if USE_POINTER_NOTATION scanf("%Lg", values + row_index * cols + col_index); #endif } } for ( size_t row_index = 0; row_index < rows; ++row_index ) { for ( size_t col_index = 0; col_index < cols; ++col_index ) { #if USE_SUBSCRIPT_NOTATION printf("%Lg ", values[row_index][col_index]); #endif #if USE_POINTER_NOTATION printf("%Lg ", *(values + row_index * cols + col_index)); #endif } putchar('\n'); // fputc(stdout, '\n'); } // Dynamic allocated memory MUST be released by programmer #if USE_SUBSCRIPT_NOTATION for ( size_t row_index = 0; row_index < rows; ++row_index ) free(values[row_index]); free(values); #endif #if USE_POINTER_NOTATION free(values); #endif return 0; } int main() { int result = 0; if ( ( result = test_automatic_matrix() ) != 0 ) return result; putchar('\n'); return dynamic_allocated_matrix(); }