-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample01.c
More file actions
265 lines (230 loc) · 7.61 KB
/
example01.c
File metadata and controls
265 lines (230 loc) · 7.61 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Matrix functions
* Thiago Luis Baldissarelli - 2015
*
* Implementation of the matrixlib.c
* Example 01 - Basic program
*
*/
#include <stdio.h> /* stdInputOutput */
#include <stdlib.h> /* Memory Allocation */
#include "matrixlib.h" /* Matrix calculation lib */
typedef enum{false, true} bool; /* Define bool type */
bool run = true;
/* Basic part of program */
double **createNewMatrix(int rows, int columns){
/* Allocate an new matrix on memory */
int i, j;
float item;
double (**matrix) = seed(rows, columns);
for(i = 0; i < rows; i++){
for(j = 0; j < columns; j++){
printf("Insert the item %dx%d: ", i+1, j+1);
scanf("%f", &item);
matrix[i][j] = item;
}
}
return(matrix);
}
void print_matrix(int rows, int columns, double (**matrix)){
/* Print matrix on program interface */
int i, j;
for(i = 0; i < rows; i++){
for(j = 0; j < columns; j++)
printf("%lf ", matrix[i][j]);
printf("\n");
}
}
/* Interactive interface */
void determinant_interface(){
/* Interface to calculate determinant */
int size;
printf("Insert how many line/columns your matrix have: ");
scanf("%d", &size);
bool check_matrix = verify(2, size, size, 0, 0);
if(check_matrix = true){
double (**matrix) = createNewMatrix(size, size);
print_matrix(size, size, matrix);
double det = determinant(matrix, size);
printf("\nThe determinant is: %lf\n", det);
}else{
printf("There is an error with your input data.\nCheck them please.\n");
}
}
void identity_interface(){
/* Interface to create the identity matrix */
int size;
printf("Insert how many line/columns your matrix have: ");
scanf("%d", &size);
bool check_matrix = verify(2, size, size, 0, 0);
if(check_matrix == true){
double (**matrix) = identity_matrix(size);
printf("\nThe identity matrix is:\n");
print_matrix(size, size, matrix);
}else{
printf("There is an error with your input data.\nCheck them please.\n");
}
}
void multiplication_interface(){
/* Interface to calculate matrix multiplication */
int matrix1_rows, matrix1_columns, matrix2_rows, matrix2_columns;
printf("Matrix 1 number of rows have to be equal to matrix 2 number of columns.");
printf("Insert how many lines matrix 1 have: ");
scanf("%d", &matrix1_rows);
printf("Insert how many columns matrix 1 have: ");
scanf("%d", &matrix1_columns);
printf("\nInsert how many lines matrix 2 have: ");
scanf("%d", &matrix2_rows);
printf("Insert how many columns matrix 2 have: ");
scanf("%d", &matrix2_columns);
bool check_matrix = verify(1, matrix1_rows, matrix1_columns, matrix2_rows, matrix2_columns);
if(check_matrix == true){
int i, j;
float item;
printf("\nThe result will be %dx%d\n", matrix1_rows, matrix2_columns);
printf("MATRIX 1 (%dx%d)\n", matrix1_rows, matrix1_columns);
double (**matrix1) = createNewMatrix(matrix1_rows, matrix1_columns);
printf("\nMATRIX 2 (%dx%d)\n", matrix2_rows, matrix2_columns);
double (**matrix2) = createNewMatrix(matrix2_rows, matrix2_columns);
printf("The result matrix is:\n");
double (**result) = matricial_multiplication(matrix1, matrix2, matrix1_rows, matrix1_columns, matrix2_rows, matrix2_columns);
print_matrix(matrix1_rows, matrix2_columns, result);
}else{
printf("There is an error with your input data.\nCheck them please.\n");
}
}
void sum_interface(int type){
/*
* Interface to add or subtract matrices.
* - Type 0 --> Add
* - Type 1 --> Subtract
*
*/
int rows, columns;
printf("Insert how many lines your matrix have: ");
scanf("%d", &rows);
printf("Inert how many columns your matrix have: ");
scanf("%d", &columns);
bool check_matrix = verify(0, rows, columns, 0, 0);
if(check_matrix == true){
int i, j;
float item;
printf("\nInsert item %dx%d of matrix 1\n", rows, columns);
double (**matrix1) = createNewMatrix(rows, columns);
printf("\nInsert item %dx%d of matrix 2\n", rows, columns);
double (**matrix2) = createNewMatrix(rows, columns);
printf("The result matrix is:\n");
double (**result) = sum_matrices(matrix1, matrix2, rows, columns, type);
print_matrix(rows, columns, result);
}else{
printf("There is an error with your input data.\nCheck them please.\n");
}
}
void inverse_interface(){
int size;
printf("Insert how many line/columns your matrix have: ");
scanf("%d", &size);
bool check_matrix = verify(0, size, size, 0, 0);
if(check_matrix == true){
double (**matrix) = createNewMatrix(size, size);
if(determinant(matrix, size) != 0.0){
double (**result) = inverse_matrix(matrix, size);
printf("The inverse matrix is:\n");
print_matrix(size, size, result);
}else{
printf("Your matrix do not have an inverse\n");
}
}else{
printf("There is an error with your input data.\nCheck them please.\n");
}
}
void transpose_interface(){
int rows, columns;
printf("Insert how many lines your matrix have: ");
scanf("%d", &rows);
printf("Inert how many columns your matrix have: ");
scanf("%d", &columns);
bool check_matrix = verify(2, rows, columns, 0, 0);
if(check_matrix == true){
int i, j;
float item;
double (**matrix) = createNewMatrix(rows, columns);
double (**result) = transpose(matrix, rows, columns);
print_matrix(columns, rows, result);
}else{
printf("There is an error with your input data.\nCheck them please.\n");
}
}
void scalar_multiplication_interface(){
int rows, columns;
float scalar;
printf("Type the scalar number: ");
scanf("%f", &scalar);
printf("Insert how many lines your matrix have: ");
scanf("%d", &rows);
printf("Inert how many columns your matrix have: ");
scanf("%d", &columns);
bool check_matrix = verify(2, rows, columns, 0, 0);
if(check_matrix == true){
int i, j;
float item;
double (**matrix) = createNewMatrix(rows, columns);
double (**result) = scalar_multiplication(matrix, rows, columns, scalar);
print_matrix(rows, columns, result);
}else{
printf("There is an error with your input data.\nCheck them please.\n");
}
}
void options(int option){
/* Switch router */
if(option <= 9){
if(option == 1)
determinant_interface();
if(option == 2)
identity_interface();
if(option == 3)
multiplication_interface();
if(option == 4)
sum_interface(0); /* Type 0 --> Add matrices */
if(option == 5)
sum_interface(1); /* Type 1 --> Add matrices */
if(option == 6)
inverse_interface();
if(option == 7)
transpose_interface();
if(option == 8)
scalar_multiplication_interface();
if(option == 9){
printf("¦¦¦+ ¦¦¦+ ¦¦¦¦¦+ ¦¦¦¦¦¦¦¦+¦¦¦¦¦¦+ ¦¦+¦¦¦¦¦¦¦+¦¦¦¦¦¦¦+¦¦¦¦¦¦¦+\n");
printf("¦¦¦¦+ ¦¦¦¦¦¦¦+--¦¦++--¦¦+--+¦¦+--¦¦+¦¦¦+--¦¦¦++¦¦+----+¦¦+----+\n");
printf("¦¦+¦¦¦¦+¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦ ¦¦¦¦¦¦++¦¦¦ ¦¦¦++ ¦¦¦¦¦+ ¦¦¦¦¦¦¦+\n");
printf("¦¦¦+¦¦++¦¦¦¦¦+--¦¦¦ ¦¦¦ ¦¦+--¦¦+¦¦¦ ¦¦¦++ ¦¦+--+ +----¦¦¦\n");
printf("¦¦¦ +-+ ¦¦¦¦¦¦ ¦¦¦ ¦¦¦ ¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦+¦¦¦¦¦¦¦+¦¦¦¦¦¦¦¦\n");
printf("+-+ +-++-+ +-+ +-+ +-+ +-++-++------++------++------+\n");
printf("\n");
printf("Type the number of what you want:\n");
printf("\n");
printf("[1] - Determinant || [2] - Identity\n");
printf("[3] - Multiply matrices || [4] - Add matrices\n");
printf("[5] - Subtract matrices || [6] - Inverse matrix\n");
printf("[7] - Transpose matrix || [8] - Scalar multiplication\n");
printf("[9] - Reshow menu || [10] - Quit\n");
}
}else{
printf("Invalid option\n");
}
printf("\n");
}
int main() {
/* Start program */
int option;
options(9);
while(run == true){
printf("Option: ");
scanf("%d", &option);
if(option == 10)
run = false;
else
options(option);
}
}