Advertisement
parallelogram

Untitled

Aug 20th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. /*Positive neighbors Problem 2 (0 / 0)
  2.  
  3. Write a program where a matrix
  4. A read from SI (maximum value of dimensions of the matrix is 100)
  5. is transformed to a new matrix B. Each element of the new matrix B
  6. is sum of positive neighbors of the corresponding element of the matrix A.
  7. Print the new matrix B on the standard output.*/
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. int main()
  13. {
  14.     int a[100][100],b[100][100],i,j,m,n;
  15.  
  16.     scanf("%d %d",&n,&m);
  17.  
  18.     for(i=0;i<n;i++)
  19.     {
  20.         for(j=0;j<m;j++)
  21.         {
  22.             scanf("%d",&a[i][j]);
  23.         }
  24.     }
  25.  
  26.     int suma=0;
  27.  
  28.     for(i=0;i<n;i++)
  29.     {
  30.         for(j=0;j<m;j++)
  31.         {
  32.             if(a[i+1][j]>0)
  33.             {
  34.                 suma+=a[i+1][j];
  35.             }
  36.  
  37.             if(a[i-1][j]>0)
  38.             {
  39.                 suma+=a[i-1][j];
  40.             }
  41.  
  42.             if(a[i][j+1]>0)
  43.             {
  44.                 suma+=a[i][j+1];
  45.             }
  46.  
  47.             if(a[i][j-1]>0)
  48.             {
  49.                 suma+=a[i][j-1];
  50.             }
  51.  
  52.             b[i][j]=suma;
  53.  
  54.             suma=0;
  55.  
  56.         }
  57.     }
  58.  
  59.  
  60.     //printf("\n");
  61.  
  62.      for(i=0;i<n;i++)
  63.     {
  64.         for(j=0;j<m;j++)
  65.         {
  66.             printf("%d ", b[i][j]);
  67.         }
  68.         printf("\n");
  69.     }
  70.  
  71.  
  72.  
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement