fork(6) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define FieldSize 20
  5. #define TimeMax 20
  6.  
  7. /*フィールドの初期化 (ファイルの読み込み)*/
  8. void init_field(int field[FieldSize][FieldSize], char *fname){
  9. int i,j;
  10. FILE *fp;
  11.  
  12. if((fp=fopen(fname,"r"))==NULL){
  13. printf("init_field(): Cannot open \"%s\"\n",fname);
  14. exit(1);
  15. }
  16.  
  17. for(i=0;i<FieldSize;i++){
  18. for(j=0;j<FieldSize;j++){
  19. fscanf(fp,"%d ",&field[i][j]);
  20. }
  21. }
  22. fclose(fp);
  23. }
  24.  
  25. int count(int I, int J, int field[FieldSize][FieldSize]){
  26. int i,j;
  27. int i_tmp,j_tmp;
  28. int sum;
  29.  
  30. sum=0;
  31. for(i=-1;i<=1;i++){
  32. for(j=-1;j<=1;j++){
  33.  
  34. field[i][j]=getrand(0,1);
  35. }
  36. }
  37. return sum;
  38. }
  39.  
  40. void update_field(int field[FieldSize][FieldSize]){
  41. int i,j;
  42. int around;
  43. int new_field[FieldSize][FieldSize];
  44.  
  45. for(i=0;i<FieldSize;i++){
  46. for(j=0;j<FieldSize;j++){
  47. around=count(i,j,field);
  48.  
  49. if( (new_field[i][j] >= 3) && !(field[i][j]) ) {
  50. field[i][j]=1;
  51. }
  52. /* ˆÛŽ */
  53. else if ( ((new_field[i][j] == 2) || (new_field[i][j] == 3)) && (field[i][j]) ) {
  54. field[i][j]=1;
  55. }
  56. /* Ž€–S(‚»‚êˆÈŠO) */
  57. else {
  58. field[i][j]=0;
  59. }
  60. new_field[i][j]=0;
  61. }
  62.  
  63. }
  64. for(i = 0;i < 20;i++){
  65. for(j = 0;j < 20;j++){
  66. field[i][j] = new_field[i][j];
  67. }
  68. }
  69.  
  70. for(i=0;i<20;i++){
  71. for(j=0;j<20;j++){
  72. field[i][j] = new_field[i][j];
  73. if(field[i][j]==1){
  74. printf("■");
  75. }else{
  76. printf("□");
  77. }
  78. }
  79. printf("\n");
  80. }
  81. }
  82.  
  83.  
  84. void display(int field[FieldSize][FieldSize]){
  85. int i,j;
  86.  
  87.  
  88. for(i=0;i<20;i++){
  89. for(j=0;j<20;j++){
  90. if(field[i][j]==1){
  91. printf("■");
  92. }else{
  93. printf("□");
  94. }
  95. }
  96. printf("\n");
  97. }
  98.  
  99. }
  100.  
  101. int main(int argc, char *argv[]){
  102. int t;
  103. int field[FieldSize][FieldSize];
  104.  
  105. if(argc!=2){
  106. printf("実行方法 : ./a.out 初期状態のファイル\n");
  107. }
  108.  
  109. //system("clear");
  110. init_field(field, argv[1]);
  111. //display(field);
  112. //sleep(1);
  113. //system("clear");
  114. for(t=0;t<TimeMax;t++){
  115. update_field(field);
  116. display(field);
  117. //sleep(1);
  118. //system("clear");
  119. }
  120.  
  121. return 0;
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘init_field’:
prog.c:19: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result
prog.c: In function ‘count’:
prog.c:34: warning: implicit declaration of function ‘getrand’
prog.c:27: warning: unused variable ‘j_tmp’
prog.c:27: warning: unused variable ‘i_tmp’
prog.c:34: warning: array subscript is below array bounds
/home/ZWNh2T/ccurMBxy.o: In function `count':
prog.c:(.text+0x9b): undefined reference to `getrand'
prog.c:(.text+0xb3): undefined reference to `getrand'
prog.c:(.text+0xcb): undefined reference to `getrand'
collect2: ld returned 1 exit status
stdout
Standard output is empty