Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * TODO: Generinc I/O
- */
- #include <stdlib.h>
- #include <stdio.h>
- #define SUCCESS 0
- #define NEWLINE puts("")
- /* Error checked malloc and realloc. */
- void *ec_malloc(size_t size);
- void *ec_realloc(void *ptr, size_t size);
- #define mm(size, type) (type *) ec_malloc(size * sizeof(type))
- /* Allocate a matrix of any type. */
- #define mem_matrix(matrix, rows, cols, type) ;\
- matrix = mm(rows, type*);\
- for (int i = 0; i < rows; ++i) {\
- matrix[i] = mm(cols, type);\
- }
- /* Deallocate a matrix of any type. */
- #define free_matrix(matrix, rows) ;\
- for (int i = 0; i < rows; ++i) {\
- free(matrix[i]);\
- }\
- free(matrix);
- /* Read and print a matrix. */
- void readmat(int **, unsigned int, unsigned int);
- void printmat(int **, unsigned int, unsigned int);
- void printmat(int **mat, unsigned int rows, unsigned int cols)
- {
- NEWLINE;
- for (
- unsigned int ln_index = 0;
- ln_index < rows;
- ++ln_index
- )
- {
- for (
- unsigned int col_index = 0;
- col_index < cols;
- ++col_index
- )
- {
- printf("%d ",mat[ln_index][col_index]);
- }
- NEWLINE;
- }
- NEWLINE;
- }
- void readmat(int **mat, unsigned int rows, unsigned int cols)
- {
- for (
- unsigned int ln_index = 0;
- ln_index < rows;
- ++ln_index
- )
- {
- for (
- unsigned int col_index = 0;
- col_index < cols;
- ++col_index
- )
- {
- scanf("%d", mat[ln_index] + col_index);
- }
- }
- }
- void *ec_realloc(void *ptr, size_t size)
- {
- void *new_ptr;
- new_ptr = realloc(ptr, size);
- if (!new_ptr) {
- perror("[Error] realloc() returned NULL\n");
- exit(-1);
- }
- return new_ptr;
- }
- void *ec_malloc(size_t size)
- {
- return ec_realloc(NULL, size);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement