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) {
- int i, j;
- int N_times_M = N * M;
- float min_temp = INFINITY;
- float max_temp = -INFINITY;
- float row_min_temp;
- float row_max_temp;
- float avg_temp;
- float grid_temp;
- float cast_M = (float)M;
- float cast_N = (float)N;
- // compute overall min
- // compute overall max
- // compute stats by row
- for (i = 0; i < N; i++) {
- row_min_temp = +INFINITY;
- row_max_temp = -INFINITY;
- avg_temp = 0;
- for (j = 0; j < M; j++) {
- grid_temp = grid[i][j];
- min_temp = grid_temp < min_temp ? grid_temp : min_temp;
- max_temp = grid_temp > max_temp ? grid_temp : max_temp;
- avg_temp += grid_temp;
- row_min_temp = grid_temp < row_min_temp ? grid_temp : row_min_temp;
- row_max_temp = grid_temp > row_max_temp ? grid_temp : row_max_temp;
- }
- row_avg[i] = M == 0 ? NAN : avg_temp / cast_M;
- }
- avg_temp = N_times_M > 0 ? avg_temp / (float)(N_times_M): NAN;
- *avg = avg_temp;
- *min = min_temp;
- *max = max_temp;
- // compute stats by col
- for (j = 0; j < M; j++) {
- min_temp = +INFINITY;
- max_temp = -INFINITY;
- avg_temp = 0;
- for (i = 0; i < N; i++) {
- grid_temp = grid[i][j];
- min_temp = grid_temp < min_temp ? grid_temp : min_temp;
- max_temp = grid_temp > max_temp ? grid_temp : max_temp;
- //col_avg[j] += grid_temp;
- avg_temp += grid_temp;
- }
- avg_temp = N == 0 ? NAN : avg_temp / cast_N;
- col_avg[j] = avg_temp;
- col_min[j] = min_temp;
- col_max[j] = max_temp;
- }
- } // min_max_avg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement