matrices.c
2.33 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <stdlib.h>
int main (){
int x, y, size, byhand, diagonal1 = 1, diagonal2 = 1;
printf("input the size of the matrices\n");
scanf("%d", &x);
y = x;
size = x;
int matrix1[x][y],
matrix2[x][y],
resultingMatrix[x][y];
printf("type 1 to intput the elements of the matrices by hand. other inputs will generate random elements\n");
scanf("%d", &byhand);
if (byhand != 1){
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
matrix1[x][y] = rand() % 500;
matrix2[x][y] = rand() % 500;
}
}
}else{
printf("enter elements of first matrix\n");
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
scanf("%d", &matrix1[x][y]);
}
}
printf("enter elements of the second matrix\n");
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
scanf("%d", &matrix2[x][y]);
}
}
}
printf("first matrix:\n");
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
printf("%d, ", matrix1[x][y]);
}
printf("\n");
}
printf("second matrix:\n");
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
printf("%d, ", matrix2[x][y]);
}
printf("\n");
}
printf("sum of the matrices is as follows:\n");
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
resultingMatrix[x][y]=matrix1[x][y]+matrix2[x][y];
printf("%d, ", resultingMatrix[x][y]);
}
printf("\n");
}
printf("substraction of the matrices is as follows:\n");
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
resultingMatrix[x][y]=matrix1[x][y]-matrix2[x][y];
printf("%d, ", resultingMatrix[x][y]);
}
printf("\n");
}
printf("the sum of the multiplication of the diagonals of the first matrix:\n");
//using byhand because i dont want to declare another variable. it acts as the sum of the multiplication of the diagonals.
for(x = 0; x < size; x++){
y = x;
diagonal1 = matrix1[x][y] * diagonal1;
}
for(x=0; x < size; x++){
y = size - 1 - x;
diagonal2 = matrix1[x][y] * diagonal2;
}
byhand = diagonal1 + diagonal2;
printf("%d\n", byhand);
diagonal1=1;
diagonal2=1;
printf("the sum of the multiplication of the diagonals of the second matrix:\n");
for(x = 0; x < size; x++){
y = x;
diagonal1 = matrix2[x][y] * diagonal1;
}
for(x=0; x < size; x++){
y = size - 1 - x;
diagonal2 = matrix2[x][y] * diagonal2;
}
byhand = diagonal1 + diagonal2;
printf("%d\n", byhand);
return 0;
}