Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4.  
  5. void min_max_avg(float **grid, int N, int M,
  6.                  float *min, float *max, float *avg,
  7.                  float *col_min, float *col_max, float *col_avg,
  8.                  float *row_min, float *row_max, float *row_avg) {
  9.  
  10.     int i, j;
  11.     int N_times_M = N * M;
  12.     float min_temp = INFINITY;
  13.     float max_temp = -INFINITY;
  14.     float row_min_temp;
  15.     float row_max_temp;
  16.     float avg_temp;
  17.     float grid_temp;
  18.     float cast_M = (float)M;
  19.     float cast_N = (float)N;
  20.  
  21.     // compute overall min
  22.     // compute overall max
  23.     // compute stats by row
  24.     for (i = 0; i < N; i++) {
  25.         row_min_temp = +INFINITY;
  26.         row_max_temp = -INFINITY;
  27.         avg_temp = 0;
  28.         for (j = 0; j < M; j++) {
  29.             grid_temp = grid[i][j];
  30.             min_temp = grid_temp < min_temp ? grid_temp : min_temp;
  31.             max_temp = grid_temp > max_temp ? grid_temp : max_temp;
  32.             avg_temp += grid_temp;
  33.             row_min_temp = grid_temp < row_min_temp ? grid_temp : row_min_temp;
  34.             row_max_temp = grid_temp > row_max_temp ? grid_temp : row_max_temp;
  35.         }
  36.         row_avg[i] = M == 0 ? NAN : avg_temp / cast_M;
  37.     }
  38.  
  39.     avg_temp = N_times_M > 0 ? avg_temp / (float)(N_times_M): NAN;
  40.     *avg = avg_temp;
  41.     *min = min_temp;
  42.     *max = max_temp;
  43.  
  44.     // compute stats by col
  45.     for (j = 0; j < M; j++) {
  46.         min_temp = +INFINITY;
  47.         max_temp = -INFINITY;
  48.         avg_temp = 0;
  49.         for (i = 0; i < N; i++) {
  50.             grid_temp = grid[i][j];
  51.             min_temp = grid_temp < min_temp ? grid_temp : min_temp;
  52.             max_temp = grid_temp > max_temp ? grid_temp : max_temp;
  53.             //col_avg[j] += grid_temp;
  54.             avg_temp += grid_temp;
  55.         }
  56.         avg_temp = N == 0 ? NAN : avg_temp / cast_N;
  57.         col_avg[j] = avg_temp;
  58.         col_min[j] = min_temp;
  59.         col_max[j] = max_temp;
  60.     }
  61. } // min_max_avg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement