Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 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.  
  11. //N = rows, M = cols
  12. int i, j;
  13. float tempmin = INFINITY;
  14. float tempmax = -INFINITY;
  15. float tempavg = 0;
  16. float temprow_min[N];
  17. float temprow_max[N];
  18. float temprow_avg[N];
  19.  
  20.  
  21. // compute stats by row
  22. for (i = 0; i < N; i++) {
  23. temprow_min[i] = +INFINITY;
  24. temprow_max[i] = -INFINITY;
  25. temprow_avg[i] = 0;
  26. for (j = 0; j < M; j++) {
  27. if (grid[i][j] < temprow_min[i]) {
  28. temprow_min[i] = grid[i][j];
  29. row_min[i] = temprow_min[i];
  30. }
  31. if (grid[i][j] > temprow_max[i]) {
  32. temprow_max[i] = grid[i][j];
  33. row_max[i] = temprow_max[i];
  34. }
  35. temprow_avg[i] += grid[i][j];
  36. }
  37. if (M == 0) {
  38. temprow_avg[i] = NAN;
  39. } else {
  40. temprow_avg[i] /= (float)M;
  41. }
  42. row_avg[i] = temprow_avg[i];
  43. }
  44.  
  45.  
  46. // compute stats by col
  47. float tempcol_min[M];
  48. float tempcol_max[M];
  49. float tempcol_avg[M];
  50. float sumavg = 0;
  51.  
  52.  
  53. for (j = 0; j < M; j++) {
  54. tempcol_min[j] = +INFINITY;
  55. tempcol_max[j] = -INFINITY;
  56. tempcol_avg[j] = 0;
  57. for (i = 0; i < N; i++) {
  58. if (grid[i][j] < tempcol_min[j]) {
  59. tempcol_min[j] = grid[i][j];
  60. col_min[j] = tempcol_min[j];
  61. if(tempcol_min[j] < tempmin) {
  62. tempmin = tempcol_min[j];
  63. }
  64. }
  65. if (grid[i][j] > tempcol_max[j]) {
  66. tempcol_max[j] = grid[i][j];
  67. col_max[j] = tempcol_max[j];
  68. if (tempcol_max[j] > tempmax)
  69. {
  70. tempmax = tempcol_max[j];
  71. }
  72. }
  73. tempcol_avg[j] += grid[i][j];
  74.  
  75. }
  76. if (N == 0) {
  77. tempcol_avg[j] = NAN;
  78. } else {
  79. tempcol_avg[j] /= (float)N;
  80. }
  81. col_avg[j] = tempcol_avg[j];
  82. sumavg += tempcol_avg[j];
  83. }
  84. *min = tempmin;
  85. *max = tempmax;
  86. if (N * M > 0) {
  87. tempavg = sumavg / (float)(M);
  88. } else {
  89. tempavg = NAN;
  90. }
  91. *avg = tempavg;
  92.  
  93. } // min_max_avg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement