Advertisement
filip710

Z2.2: Prosjek podmatrica

Jul 9th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. U ovom zadatku cilj je napisati program koji će omogućiti učitavanje dvodimenzionalne matrice i na ekran ispisati koja podmatrica unutar učitane matrice ima najmanju prosječnu vrijednost.
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<float.h>
  6. int main()
  7. {
  8.     int i,j,R,C,n,dim1,dim2;
  9.     float **matrix;
  10.     float sum,min=FLT_MAX;
  11.        
  12.     scanf("%d",&R);
  13.     scanf("%d",&C);
  14.  
  15.     matrix=(float**)malloc(R*sizeof(float*));
  16.  
  17.     for(i=0;i<R;i++)
  18.         *(matrix+i)=(float*)malloc(C*sizeof(float));
  19.  
  20.     scanf("%d",&n);
  21.  
  22.     for(i=0;i<R;i++)
  23.     {
  24.         for(j=0;j<C;j++)
  25.         {
  26.             scanf("%f",&matrix[i][j]);
  27.         }
  28.     }
  29.  
  30.  
  31.     dim1 = R - n + 1;
  32.     dim2 = C - n + 1;
  33.         for (int i1 = 0; i1 < dim1; i1++) {
  34.             for (int j1 = 0; j1 < dim2; j1++) {
  35.                 sum=0;                              
  36.                 for (i = 0; i < n; i++) {
  37.                     for (j = 0; j < n; j++) {
  38.                         sum+=matrix[i+i1][j+j1];
  39.                     }
  40.                 }
  41.                 if(sum<min) min=sum;
  42.             }
  43.         }
  44.     printf("REZULTATI:\n%.2f",min/(n*n));
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement