-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path045.2_varun_practice_sheet_question_2.c
167 lines (134 loc) · 4.39 KB
/
045.2_varun_practice_sheet_question_2.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// 2) a) Given an (n x n) matrix, find the sum of the squares of the diagonal elements.
// Value of n: 3
// 1 2 3
// 3 4 5
// 5 6 7
// 66
/*
#include <stdio.h>
#include <math.h>
int main() {
int num_of_r_c;
printf("Enter the number of rows and columns in an nxn matrix: ");
scanf("%i", &num_of_r_c);
printf("\n");
int matrix[num_of_r_c][num_of_r_c];
int sum = 0;
for (int row_count = 0; row_count < num_of_r_c; row_count++) {
for (int column_count = 0; column_count < num_of_r_c; column_count++) {
printf("Enter the value of %dx%d: ", row_count + 1, column_count + 1);
scanf("%i", &matrix[row_count][column_count]);
sum += (row_count == column_count) ? pow(matrix[row_count][column_count], 2) : 0;
}
}
printf("\n");
printf("Your matrix is: \n");
for (int row_count = 0; row_count < num_of_r_c; row_count++) {
for (int column_count = 0; column_count < num_of_r_c; column_count++) {
printf("%3i ", matrix[row_count][column_count]);
}
printf("\n");
}
printf("\n");
printf("Sum of diagonal elements is: %i \n", sum);
}
*/
// 2) b) Given an (n x m) matrix, find the matrix obtained by multiplying it with its transpose.
// Value of n: 2
// Value of m: 3
// 1 2 3
// 4 5 6
// 14 32
// 32 77
/*
#include <stdio.h>
int main() {
int num_of_r;
int num_of_c;
printf("Enter the number of rows in an mxn matrix: ");
scanf("%i", &num_of_r);
printf("Enter the number of columns in an mxn matrix: ");
scanf("%i", &num_of_c);
printf("\n");
int matrix[num_of_r][num_of_c];
int matrix_transpose[num_of_c][num_of_r];
int matrix_product[num_of_r][num_of_r];
for (int row_count = 0; row_count < num_of_r; row_count++) {
for (int column_count = 0; column_count < num_of_c; column_count++) {
printf("Enter the value of %dx%d: ", row_count + 1, column_count + 1);
scanf("%i", &matrix[row_count][column_count]);
matrix_transpose[column_count][row_count] = matrix[row_count][column_count];
}
}
printf("\n");
printf("Your %dx%d matrix is: \n", num_of_r, num_of_c);
for (int row_count = 0; row_count < num_of_r; row_count++) {
for (int column_count = 0; column_count < num_of_c; column_count++) {
printf("%3i ", matrix[row_count][column_count]);
}
printf("\n");
}
printf("\n");
printf("Your %dx%d matrix's %dx%d transpose is: \n", num_of_r, num_of_c, num_of_c, num_of_r);
for (int row_count = 0; row_count < num_of_c; row_count++) {
for (int column_count = 0; column_count < num_of_r; column_count++) {
printf("%3i ", matrix_transpose[row_count][column_count]);
}
printf("\n");
}
// multiplying matrix and matrix_transpose
for (int row_count = 0; row_count < num_of_r; row_count++) {
for (int column_count = 0; column_count < num_of_r; column_count++) {
matrix_product[row_count][column_count] = 0;
for (int index = 0; index < num_of_c; index++) {
matrix_product[row_count][column_count] += matrix[row_count][index] * matrix_transpose[index][column_count];
}
}
}
printf("\n");
printf("Your multiplied %dx%d matrix is: \n", num_of_r, num_of_r);
for (int row_count = 0; row_count < num_of_r; row_count++) {
for (int column_count = 0; column_count < num_of_r; column_count++) {
printf("%3i ", matrix_product[row_count][column_count]);
}
printf("\n");
}
}
*/
// 2) c) Given a (n x m) matrix, find a sub-matrix with a given required sum.
// Value of n: 3
// Value of m: 4
// Required sum: 30
// 1 2 3 4
// 3 4 5 6
// 5 6 7 8
// 3 4 5
// 5 6 7
#include <stdio.h>
int main() {
int num_of_r;
int num_of_c;
int sum;
printf("Enter the number of rows in an mxn matrix: ");
scanf("%i", &num_of_r);
printf("Enter the number of columns in an mxn matrix: ");
scanf("%i", &num_of_c);
printf("Enter the required sum: ");
scanf("%i", &sum);
printf("\n");
int matrix[num_of_r][num_of_c];
for (int row_count = 0; row_count < num_of_r; row_count++) {
for (int column_count = 0; column_count < num_of_c; column_count++) {
printf("Enter the value of %dx%d: ", row_count + 1, column_count + 1);
scanf("%i", &matrix[row_count][column_count]);
}
}
printf("\n");
printf("Your matrix is: \n");
for (int row_count = 0; row_count < num_of_r_c; row_count++) {
for (int column_count = 0; column_count < num_of_r_c; column_count++) {
printf("%3i ", matrix[row_count][column_count]);
}
printf("\n");
}
}