PracticalworkLab4Task2.c
2.59 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
int main(){
int matrix_size;
int value;
int product = 1;
int product2 = 1;
int num;
printf("Please enter dimension of matrices: ");
scanf("%d", &matrix_size);
int array1[matrix_size][matrix_size];
int array2[matrix_size][matrix_size];
// Values for first Matrix
printf("Please enter values for the first matrix: \n");
for(int i = 0; i < matrix_size; i++){
for(int j = 0; j < matrix_size; j++){
printf("Please enter value: ");
scanf("%d", &array1[i][j]);
}
}
printf("\nThis is your first Matrix: \n");
for(int i = 0; i < matrix_size; i++){
for(int j = 0; j < matrix_size; j++){
printf("%d, ", array1[i][j]);
}
printf("\n");
}
printf("\n");
// Values for second Matrix
printf("Please enter values for the second matrix: \n");
for(int i = 0; i < matrix_size; i++){
for(int j = 0; j < matrix_size; j++){
printf("Please enter value: ");
scanf("%d", &array2[i][j]);
}
}
printf("\nThis is your second Matrix: \n");
for(int i = 0; i < matrix_size; i++){
for(int j = 0; j < matrix_size; j++){
printf("%d, ", array2[i][j]);
}
printf("\n");
}
printf("\n");
// Add
printf("The sum of the two matrices is: \n");
for(int i = 0; i < matrix_size; i++){
for(int j = 0; j < matrix_size; j++){
value = array1[i][j] + array2[i][j];
printf("%d, ", value);
}
printf("\n");
}
printf("\n");
// Difference
printf("The Difference of the two matrices is: \n");
for(int i = 0; i < matrix_size; i++){
for(int j = 0; j < matrix_size; j++){
value = array1[i][j] - array2[i][j];
printf("%d, ", value);
}
printf("\n");
}
printf("\n");
// The sum of the multiplication of elements on the diagonals of the two matrices
printf("The sum of the multiplication of elements on \n");
printf("the diagonals of the two matrices is: \n");
printf("First Matrix: ");
for(int i = 0; i < matrix_size; i++){
for(int j = 0; j < matrix_size; j++){
if(i == j){
product *= array1[i][j];
}
}
}
num = matrix_size - 1;
for(int i = 0; i < matrix_size; i++){
product2 *= array1[i][num];
num--;
}
product = product + product2;
printf("%d\n", product);
product = 1;
product2 = 1;
printf("Second Matrix: ");
for(int i = 0; i < matrix_size; i++){
for(int j = 0; j < matrix_size; j++){
if(i == j){
product *= array2[i][j];
}
}
}
num = matrix_size - 1;
for(int i = 0; i < matrix_size; i++){
product2 *= array2[i][num];
num--;
}
product = product + product2;
printf("%d", product);
return 0;
}