-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixSort.c
69 lines (62 loc) · 1.29 KB
/
matrixSort.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
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Get size.
int n, m, maxSum = 0, sum; // TODO:
scanf("%d%d", &n, &m);
// Create matrix.
int **mat = malloc(sizeof(int *) * n);
for (int i = 0; i < n; i++)
{
mat[i] = malloc(sizeof(int) * m);
}
// Get matrix.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%d", *(mat + i) + j);
}
}
// Calc rows.
for (int i = 0; i < n; i++)
{
for (int k = i + 1; k < n; k++)
{
sum = 0;
for (int j = 0; j < m; j++)
{
sum += (*(mat + i))[j] + (*(mat + k))[j];
}
if (sum > maxSum)
{
maxSum = sum;
}
}
}
// Calc columns.
for (int i = 0; i < m; i++)
{
for (int k = i + 1; k < m; k++)
{
sum = 0;
for (int j = 0; j < n; j++)
{
sum += (*(mat + j))[i] + (*(mat + j))[k];
}
if (sum > maxSum)
{
maxSum = sum;
}
}
}
// Free memory.
for (int i = 0; i < n; i++)
{
free(mat[i]);
}
free(mat);
printf("%d", maxSum);
return 0;
}