Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <omp.h>
  3. #include <bits/types/clock_t.h>
  4. #include <time.h>
  5.  
  6. #define TAILLE_SOUS_MATRICE 50
  7. /* On peut avoir 7 * 7 cellules vivantes */
  8. #define TAILLE_SUR_MATRICE 52
  9. /* On place une bordure autour qui facilite la vie du programmeur... */
  10.  
  11.  
  12. /* Initialisation de la matrice */
  13. void init(int matrice [][TAILLE_SUR_MATRICE ]) {
  14. /****************************************/
  15. int i,j;
  16.  
  17. for(i=0; i<TAILLE_SUR_MATRICE; i++) {
  18. for(j=0; j<TAILLE_SUR_MATRICE; j++) {
  19. if (i<=j && i>0 && j<=TAILLE_SOUS_MATRICE)
  20. matrice[i][j]=1;
  21. else
  22. matrice[i][j]=0;
  23. }
  24. }
  25. /* On pourrait aussi faire une initialisation aléatoire */
  26. }
  27. /****************************************/
  28. /* Calcul du nombre de voisins vivants */
  29. int nombre_voisins (int matrice[][TAILLE_SUR_MATRICE ], int ligne, int colonne) {
  30. /****************************************/
  31. int compte=0; /* compteur de cellules */
  32. int i,j;
  33. /* On additionne les 9 cellules centrées en ligne,colonne */
  34. for (i=ligne-1;i<=ligne+1;i++)
  35. for(j=colonne-1;j<=colonne+1;j++)
  36. compte=compte+matrice[i][j];
  37.  
  38. /* Puis on retire celle du milieu... */
  39. compte -= matrice[ligne][colonne];
  40. return compte;
  41. }
  42. /****************************************/
  43. /* Correspond à l'étape n+1 */
  44. void mise_a_jour(int matrice[ ][TAILLE_SUR_MATRICE ]) {
  45. /****************************************/
  46. int i,j;
  47. int nbr_voisins;
  48. int matrice_densite[TAILLE_SOUS_MATRICE][TAILLE_SOUS_MATRICE];
  49. /* matrice qui comptabilise le nombre de voisins */
  50. /* et cela, case par case */
  51. for(i=0; i< TAILLE_SOUS_MATRICE; i++)
  52. #pragma omp parallel for
  53. for(j=0; j< TAILLE_SOUS_MATRICE; j++)
  54. matrice_densite[i][j]=nombre_voisins(matrice,i+1,j+1);
  55. /* i+1 et j+1 car on passe de la SOUS_MATRICE à la MATRICE */
  56.  
  57. for(i=0; i< TAILLE_SOUS_MATRICE; i++)
  58. #pragma omp parallel for
  59. for(j=0; j< TAILLE_SOUS_MATRICE; j++) {
  60. nbr_voisins=matrice_densite[i][j];
  61. if(nbr_voisins==2)
  62. matrice[i+1][j+1]=1;
  63. else if (nbr_voisins==0 || nbr_voisins==4)
  64. matrice[i+1][j+1]=0;
  65. }
  66. }
  67.  
  68. /****************************************/
  69. /* Tracé d'une ligne */
  70. void ligne(int largeur) {
  71. /****************************************/
  72. int i;
  73. for(i=0; i<largeur; i++)
  74. printf("--");
  75. printf("\n");
  76. }
  77.  
  78. /****************************************/
  79. /* Affichage à l'écran des cellules vivantes */
  80. void affiche_matrice(int matrice[ ][TAILLE_SUR_MATRICE ]) {
  81. /****************************************/
  82. int i,j;
  83. for(i=1; i<=TAILLE_SOUS_MATRICE; i++) {
  84. ligne(TAILLE_SOUS_MATRICE);
  85. for(j=1; j<= TAILLE_SOUS_MATRICE; j++)
  86. if (matrice[i][j]==1)
  87. printf("|%c",'1');
  88. else
  89. printf("|%c",'0');
  90. printf("|\n");
  91. }
  92. ligne(TAILLE_SOUS_MATRICE);
  93. }
  94.  
  95. int main() {
  96. int i;
  97. int nbr_cycles;
  98. double wtime;
  99.  
  100. wtime = omp_get_wtime();
  101.  
  102. int matrice[TAILLE_SUR_MATRICE] [TAILLE_SUR_MATRICE ];
  103. char s[2];
  104. printf("Nombre de cycles : ");
  105. scanf("%i",&nbr_cycles);
  106. init(matrice);
  107. // printf("La population au départ : \n");
  108. // affiche_matrice(matrice);
  109. // printf("Pressez sur ENTER pour continuer...\n");
  110. // gets(s);
  111. for(i=0; i<nbr_cycles; i++) {
  112. mise_a_jour (matrice);
  113. // printf("La population après %d cycles: \n", i+1);
  114.  
  115. // printf("Pressez sur ENTER pour continuer...\n");
  116. // gets(s);
  117. }
  118. affiche_matrice (matrice);
  119.  
  120.  
  121.  
  122. wtime = omp_get_wtime() - wtime;
  123. printf("Temps: %lf",wtime);
  124. return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement