Advertisement
Guest User

Untitled

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