Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- void min_max_avg(float **grid, int N, int M,
- float *min, float *max, float *avg,
- float *col_min, float *col_max, float *col_avg,
- float *row_min, float *row_max, float *row_avg)
- {
- //N = rows, M = cols
- int i, j;
- float tempmin = INFINITY;
- float tempmax = -INFINITY;
- float tempavg = 0;
- float temprow_min[N];
- float temprow_max[N];
- float temprow_avg[N];
- // compute stats by row
- for (i = 0; i < N; i++) {
- temprow_min[i] = +INFINITY;
- temprow_max[i] = -INFINITY;
- temprow_avg[i] = 0;
- for (j = 0; j < M; j++) {
- if (grid[i][j] < temprow_min[i]) {
- temprow_min[i] = grid[i][j];
- row_min[i] = temprow_min[i];
- }
- if (grid[i][j] > temprow_max[i]) {
- temprow_max[i] = grid[i][j];
- row_max[i] = temprow_max[i];
- }
- temprow_avg[i] += grid[i][j];
- }
- if (M == 0) {
- temprow_avg[i] = NAN;
- } else {
- temprow_avg[i] /= (float)M;
- }
- row_avg[i] = temprow_avg[i];
- }
- // compute stats by col
- float tempcol_min[M];
- float tempcol_max[M];
- float tempcol_avg[M];
- float sumavg = 0;
- for (j = 0; j < M; j++) {
- tempcol_min[j] = +INFINITY;
- tempcol_max[j] = -INFINITY;
- tempcol_avg[j] = 0;
- for (i = 0; i < N; i++) {
- if (grid[i][j] < tempcol_min[j]) {
- tempcol_min[j] = grid[i][j];
- col_min[j] = tempcol_min[j];
- if(tempcol_min[j] < tempmin) {
- tempmin = tempcol_min[j];
- }
- }
- if (grid[i][j] > tempcol_max[j]) {
- tempcol_max[j] = grid[i][j];
- col_max[j] = tempcol_max[j];
- if (tempcol_max[j] > tempmax)
- {
- tempmax = tempcol_max[j];
- }
- }
- tempcol_avg[j] += grid[i][j];
- }
- if (N == 0) {
- tempcol_avg[j] = NAN;
- } else {
- tempcol_avg[j] /= (float)N;
- }
- col_avg[j] = tempcol_avg[j];
- sumavg += tempcol_avg[j];
- }
- *min = tempmin;
- *max = tempmax;
- if (N * M > 0) {
- tempavg = sumavg / (float)(M);
- } else {
- tempavg = NAN;
- }
- *avg = tempavg;
- } // min_max_avg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement