1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| #include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#define getElem(M, I, J) { printf("Input " #M "[%u][%u]: ", (I), (J)); scanf("%d", &(M)[(I)][(J)]); }
void errExit(const char* fmt, ...);
int main()
{
size_t rows, cols, i, j;
int min;
int** matrix;
printf("Input the number of rows:\n? ");
scanf("%u", &rows);
printf("Input the number of columns:\n? ");
scanf("%u", &cols);
if((matrix = malloc(sizeof(char*) * rows)) == NULL)
errExit("Can't allocate memory for matrix %ux%u\n", rows, cols);
for(i = 0; i < rows; ++i)
if((matrix[i] = malloc(sizeof(char*) * cols)) == NULL)
errExit("Can't allocate memory for the %u row of the matrix", i);
for(i = 0; i < rows; ++i)
for(j = 0; j < cols; ++j)
getElem(matrix, i, j);
for(i = 0; i < rows; ++i)
{
min = matrix[i][0];
for(j = 1; j < cols; ++j)
if(min > matrix[i][j])
min = matrix[i][j];
printf("Min elem in the %u row is %d\n", i, min);
}
for(i = 0; i < rows; ++i)
free(matrix[i]);
free(matrix);
exit(0);
}
void errExit(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
} |