-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauxiliar.c
More file actions
52 lines (43 loc) · 1.36 KB
/
auxiliar.c
File metadata and controls
52 lines (43 loc) · 1.36 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]) {
if (argc != 5) {
printf("Informe o tamanho das matrizes M1 e M2: %s x1 y1 x2 y2\n", argv[0]);
return 1;
}
int x1 = atoi(argv[1]);
int y1 = atoi(argv[2]);
int x2 = atoi(argv[3]);
int y2 = atoi(argv[4]);
srand(time(NULL)); // inicializa o gerador de números aleatórios
// Gera a matriz aleatória
FILE *file1 = fopen("M1.txt", "w");
if (!file1) {
printf("Error ao abrir a matriz M1.txt\n");
return 1;
}
fprintf(file1, "%d %d\n", x1, y1); // primeira linha exibindo o tamanho da matriz
for (int i = 0; i < x1; i++) {
for (int j = 0; j < y1; j++) {
fprintf(file1, "%d ", rand() % 90 + 10); // escreve um elemento aleatório no arquivo entre 0 e 99
}
fprintf(file1, "\n"); // nova linha para cada linha da matriz
}
fclose(file1);
FILE *file2 = fopen("M2.txt", "w");
if (!file2) {
printf("Error ao abrir a matriz M2.txt\n");
return 1;
}
fprintf(file2, "%d %d\n", x2, y2);
for (int i = 0; i < x2; i++) {
for (int j = 0; j < y2; j++) {
fprintf(file2, "%d ", rand() % 90 + 10);
}
fprintf(file2, "\n");
}
fclose(file2);
printf("Matrizes M1 e M2 geradas com sucesso!\n");
return 0;
}