-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathej_4.c
More file actions
82 lines (75 loc) · 2.09 KB
/
Copy pathej_4.c
File metadata and controls
82 lines (75 loc) · 2.09 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
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <time.h>
#include <unistd.h>
/* Función que imprime una matriz m de rows filas y cols columnas */
void print_matrix(int **m, int rows, int cols)
{
printf("Matriz:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d\t", m[i][j]);
}
printf("\n");
}
}
/* Función que genera un número double aleatorio entre min y max */
int aleatorioInt(int min, int max)
{
int numeroAleatorio = rand() % (max - min + 1) + min;
return numeroAleatorio;
}
int main(int argc, char **argv)
{
if (argc != 3)
{
printf("Los parámetros al ejecutar el programa son:\n 1- nombre del archivo \n 2- cantidad de filas de la matriz \n 3- cantidad de columnas de la matriz \n");
return -1;
}
int smallest, largest;
int i, j;
int rows = atoi(argv[1]);
int cols = atoi(argv[2]);
int **matrix = malloc(sizeof(int *) * rows);
for (i = 0; i < rows; i++)
{
matrix[i] = (int *)malloc(sizeof(int) * cols);
}
// Inicializar matriz con núemros enteros aleatorios
srand(time(NULL));
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
matrix[i][j] = aleatorioInt(1, 100);
}
}
print_matrix(matrix, rows, cols);
// Establecer la cantidad de hilos
omp_set_num_threads(8);
// Establecer como mínimo y máximo inicial al primer elemento de la matriz
smallest = matrix[0][0];
largest = matrix[0][0];
#pragma omp parallel for private(i, j) reduction(min : smallest) reduction(max : largest)
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (matrix[i][j] < smallest)
{
smallest = matrix[i][j];
}
if (matrix[i][j] > largest)
{
largest = matrix[i][j];
}
}
}
printf("Menor elemento de la matriz: %d\n", smallest);
printf("Mayor elemento de la matriz: %d\n", largest);
free(matrix);
return 0;
}