-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixReshape.c
70 lines (67 loc) · 1.34 KB
/
matrixReshape.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
int reshape(int *orig, int *new, int orow, int ocol, int row, int col)
{
if (orow * ocol == row * col)
{
for (int i = 0; i < row * col; ++i)
{
*(new + i) = *(orig + i);
}
return 0;
}
else
{
return 1;
}
}
int main(void)
{
int m, n, r, c;
scanf("%d%d", &m, &n);
int mat[m][n];
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
scanf("%d", &mat[i][j]);
}
}
scanf("%d%d", &r, &c);
int new_mat[r][c];
if (reshape((int *)mat, (int *)new_mat, m, n, r, c))
{
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
printf("%d", mat[i][j]);
if (j != n - 1)
{
printf(" ");
}
}
if (i != m - 1)
{
printf("\n");
}
}
}
else
{
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
printf("%d", new_mat[i][j]);
if (j != c - 1)
{
printf(" ");
}
}
if (i != r - 1)
{
printf("\n");
}
}
}
}