LegoDrifter

Lab 6 - 1

Dec 9th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int m, n, i, j, zbir;
  5. scanf("%d%d", &m, &n);
  6. int a[m][n], b[m][n];
  7. for(i = 0; i < m; i++)
  8. for(j = 0; j < n; j++)
  9. {
  10. scanf("%d", &a[i][j]);
  11. b[i][j] = a[i][j];
  12. }
  13. for(i = 0; i < m; i++)
  14. for(j = 0; j < n; j++)
  15. {
  16. zbir = 0;
  17. if(a[i][j] == 0)
  18. {
  19. if(j - 1 >= 0)
  20. zbir = zbir + a[i][j-1]; //sosed levo od elementot
  21. if(j + 1 < n)
  22. zbir = zbir + a[i][j+1]; //sosed desno od elementot
  23. if(i - 1 >= 0)
  24. zbir = zbir + a[i-1][j]; //sosed nad elementot
  25. if(i + 1 < m)
  26. zbir = zbir + a[i+1][j]; //sosed pod elementot
  27. if(i - 1 >= 0 && j - 1 >= 0)
  28. zbir = zbir + a[i-1][j-1]; //sosed gore levo od elementot
  29. if(i - 1 >= 0 && j + 1 < n)
  30. zbir = zbir + a[i-1][j+1]; //sosed gore desno od elementot
  31. if(i + 1 < m && j - 1 >= 0)
  32. zbir = zbir + a[i+1][j-1]; //sosed dolu levo od elementot
  33. if(i + 1 < m && j + 1 < n)
  34. zbir = zbir + a[i+1][j+1]; //sosed dolu desno od elementot
  35. b[i][j] = zbir;
  36. }
  37. else b[i][j] = -1;
  38. }
  39.  
  40. for(i = 0; i < m; i++){
  41. for(j = 0; j < n; j++)
  42. {
  43. if(b[i][j] == -1)
  44. printf("* ");
  45. else
  46. printf("%d ", b[i][j]);
  47. }
  48. printf("\n");
  49. }
  50.  
  51.  
  52.  
  53.  
  54. return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment