Advertisement
huutho_96

Untitled

Apr 1st, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <conio.h>
  4. //mình lười nhập nên khai bao thêm 2 thư viện này. bạn chỉ cần
  5. //nhập kích thước thì sẽ có 1 mảng bất kì
  6. //chương trình chạy đúng nhưng sẽ phải so sánh và tính toán nhiều
  7. //mình chỉ sửa lại khúc so sánh. bạn thử rút gọn chương trình xuống cho tối ưu nha
  8. #include <Windows.h>
  9. #include <time.h>
  10. #define max 100
  11. typedef int matran[max][max];
  12. matran a, b;
  13.  
  14. void nhap(matran a, int &m, int &n)
  15. {
  16. printf("\nNhap so dong m = "); //m dòng
  17. scanf("%d", &m);
  18. printf("\nNhap so cot n = "); //n cột
  19. scanf("%d", &n);
  20. srand(time(NULL));
  21. for (int i = 0; i < m; i++)
  22. {
  23. for (int j = 0; j < n; j++)
  24. {
  25. //printf("\n a[%d][%d] = ", i, j);
  26. //scanf("%d", &a[i][j]);
  27. a[i][j] = rand() % 5;
  28. }
  29.  
  30. }
  31. }
  32. void bai6(int a[][max], int n, int m, int b[][max])
  33. {
  34. int dem;
  35. for (int i = 0; i < n; i++)
  36. {
  37. dem = 0;
  38. for (int j = 0; j < m; j++)
  39. {
  40. if (a[i - 1][j - 1] % 2 == 0 && a[i - 1][j - 1] > 0) dem++;
  41. if (a[i - 1][j + 1] % 2 == 0 && a[i - 1][j + 1] > 0) dem++;
  42. if (a[i - 1][j] % 2 == 0 && a[i - 1][j] > 0) dem++;
  43. if (a[i + 1][j - 1] % 2 == 0 && a[i + 1][j - 1] > 0) dem++;
  44. if (a[i + 1][j + 1] % 2 == 0 && a[i + 1][j + 1] > 0) dem++;
  45. if (a[i + 1][j] % 2 == 0 && a[i + 1][j] > 0) dem++;
  46. if (a[i][j - 1] % 2 == 0 && a[i][j - 1] > 0) dem++;
  47. if (a[i][j + 1] % 2 == 0 && a[i][j + 1] > 0) dem++;
  48. b[i][j] = dem;
  49. }
  50. }
  51. }
  52.  
  53.  
  54. void xuat(matran a, int m, int n)
  55. {
  56.  
  57. for (int i = 0; i<m; i++)
  58. {
  59. for (int j = 0; j < n; j++)
  60. printf("\t %5d", a[i][j]);
  61. printf("\n");
  62. }
  63. }
  64.  
  65.  
  66.  
  67. int main()
  68. {
  69. int m, n;
  70. nhap(a, m, n);
  71.  
  72. bai6(a, m, n, b);
  73. xuat(a, m, n);
  74. xuat(b, m, n);
  75. _getch();
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement