Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Positive neighbors Problem 2 (0 / 0)
- Write a program where a matrix
- A read from SI (maximum value of dimensions of the matrix is 100)
- is transformed to a new matrix B. Each element of the new matrix B
- is sum of positive neighbors of the corresponding element of the matrix A.
- Print the new matrix B on the standard output.*/
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int a[100][100],b[100][100],i,j,m,n;
- scanf("%d %d",&n,&m);
- for(i=0;i<n;i++)
- {
- for(j=0;j<m;j++)
- {
- scanf("%d",&a[i][j]);
- }
- }
- int suma=0;
- for(i=0;i<n;i++)
- {
- for(j=0;j<m;j++)
- {
- if(a[i+1][j]>0)
- {
- suma+=a[i+1][j];
- }
- if(a[i-1][j]>0)
- {
- suma+=a[i-1][j];
- }
- if(a[i][j+1]>0)
- {
- suma+=a[i][j+1];
- }
- if(a[i][j-1]>0)
- {
- suma+=a[i][j-1];
- }
- b[i][j]=suma;
- suma=0;
- }
- }
- //printf("\n");
- for(i=0;i<n;i++)
- {
- for(j=0;j<m;j++)
- {
- printf("%d ", b[i][j]);
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement