# Homework 5-1 寻找矩阵中的最大值和最小值及其坐标

``````#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main()
{
int m, n;
scanf("%d%d", &m, &n);
int *mat = malloc(m * n * sizeof(int)), ma = 0, mi = 0, pre = time(0);
#pragma omp parallel for
for (int i = 0; i < m * n; ++i)
rand_r(&pre), mat[i] = pre;
#pragma omp parallel for
for (int i = 0; i < m * n; ++i)
{
#pragma omp critical
if (mat[ma] < mat[i])
ma = i;
#pragma omp critical
if (mat[mi] > mat[i])
mi = i;
}
for (int i = 0; i < m * n; ++i)
printf("%d%c", mat[i], i % n == n - 1 ? '\n' : ' ');
printf("max: %d(%d,%d)\nmin: %d(%d,%d)", mat[ma], ma / n, ma % n, mat[mi], mi / n, mi % n);
free(mat);
}
``````

``````-925438579 -1126332182 -534488686 -1767211810
956745691 -2014164158 1868749196 173473471
-8830651 1176503991 1066009373 -1406904493
-1899449319 159599030 -609169565 1729478076
-880143610 -130409017 1504147669 -2027289992
-217405600 1943131529 -2112678481 -79255408
1178127908 -1166815919 -189038604 811456910
max: 1943131529(5,1)
min: -2112678481(5,2)
``````