-
Notifications
You must be signed in to change notification settings - Fork 16
/
13.c
67 lines (59 loc) · 1.38 KB
/
13.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
#include <stdio.h>
void input_matrix(int *matrix, int rows, int cols)
{
int i, j;
printf("Enter matrix elements: \n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
scanf("%d", &matrix[i * cols + j]);
}
}
void print_matrix(int *matrix, int rows, int cols)
{
int i, j;
printf("Matrix: \n");
for (i = 0; i < rows; i++)
{
printf("[");
for (j = 0; j < cols; j++)
printf(" %d ", matrix[i * cols + j]);
printf("]\n");
}
}
void print_row_averages(int *matrix, int rows, int cols)
{
int i, j;
printf("Row averages: \n");
for (i = 0; i < rows; i++)
{
int sum = 0;
for (j = 0; j < cols; j++)
sum += matrix[i * cols + j];
printf("%d\n", sum / cols);
}
}
void print_col_averages(int *matrix, int rows, int cols)
{
int i, j;
printf("Column averages: \n");
for (i = 0; i < cols; i++)
{
int sum = 0;
for (j = 0; j < rows; j++)
sum += matrix[j * cols + i];
printf("%d\n", sum / rows);
}
}
int main()
{
int m, n;
printf("Enter number of rows and columns: ");
scanf("%d %d", &m, &n);
int matrix[m][n];
input_matrix(&matrix[0][0], m, n);
print_matrix(&matrix[0][0], m, n);
print_row_averages(&matrix[0][0], m, n);
print_col_averages(&matrix[0][0], m, n);
return 0;
}