Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. double dohvati_max(double num1, double num2, double num3, double num4) {
  6.     double max = num1;
  7.     if (num2 > max) max = num2;
  8.     if (num3 > max) max = num3;
  9.     if (num4 > max) max = num4;
  10.     return max;
  11. }
  12.  
  13. double** max_pool_2d(double** mat, int N, int M) {
  14.     double** rezMat = (double**) calloc(N/2, sizeof(double*));
  15.     for (int i = 0, indexI = 0; i < N; i += 2, indexI++) {
  16.         rezMat[indexI] = (double*) calloc(M/2, sizeof(double));
  17.         for (int j = 0, indexJ = 0; j < M; j += 2, indexJ++) {
  18.             rezMat[indexI][indexJ] = dohvati_max(mat[i][j], mat[i][j + 1], mat[i + 1][j], mat[i + 1][j + 1]);
  19.         }
  20.     }
  21.  
  22.     return rezMat;
  23. }
  24.  
  25. int main() {
  26.     int N;
  27.     int M;
  28.     scanf("%d %d", &N, &M);
  29.     if(N % 2 != 0 || M % 2 != 0) {
  30.         printf("Dimenzije ulazne matrice moraju biti parne.");
  31.         return 0;
  32.     }
  33.  
  34.     double** mat = (double**) calloc(N, sizeof(double*));
  35.     for (int i = 0; i < N; i++) {
  36.         mat[i] = (double*) calloc(M, sizeof(double));
  37.         for (int j = 0; j < M; j++) {
  38.             scanf("%lf", &mat[i][j]);
  39.         }
  40.     }
  41.  
  42.     FILE* fout = fopen("pool.txt", "w");
  43.     double** rezMat = max_pool_2d(mat, N, M);
  44.     for (int i = 0; i < N/2; i++) {
  45.         for (int j = 0; j < M/2; j++) {
  46.             fprintf(fout, "%.3f ", rezMat[i][j]);
  47.         }
  48.         fprintf(fout, "\n");
  49.     }
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement