質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Q&A

解決済

1回答

3467閲覧

c言語のライフゲームに関して

kuroni

総合スコア4

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

0グッド

0クリップ

投稿2020/07/24 15:40

編集2020/07/24 17:08

前提・実現したいこと

c言語のライフゲームをセルオートマトンの応用によって作ろうとしています。
関数に分けて作成したいです。

発生している問題・エラーメッセージ

コンパイルは成功するが全然違う実行結果になってしまう

### 該当のソースコード #include <stdio.h> #include <stdlib.h> #define MAXT 100 #define M 40 #define N 40 int cell[M+2][N+2]={0}; void init(){ cell[15][N/2]=1; cell[16][N/2]=1; cell[17][N/2]=1; cell[18][N/2]=1; cell[19][N/2]=1; cell[20][N/2]=1; cell[21][N/2]=1; cell[22][N/2]=1; cell[23][N/2]=1; cell[24][N/2]=1; return; } void dump(){ int i; int j; for(i=0;i<M;i++){ for(j=0;j<N;j++){ if(1 ==cell[i][j]){ printf("@"); }else{ printf("."); } } print("\n"); return; } } void update(){ int c[M][N] = {0}; int i; int j; int count = 0; if(cell[i-1][j] == 1 && i != 0) count++; if(cell[i+1][j] == 1 && i != N-1) count++; if(cell[i][j-1] == 1 && j != 0) count++; if(cell[i][j+1] == 1 && j != N-1) count++; if(cell[i-1][j-1] == 1 && i != 0 && j != 0) count++; if(cell[i-1][j+1] == 1 && i != 0 && j != N-1) count++; if(cell[i+1][j-1] == 1 && i != N-1 && j != 0) count++; if(cell[i+1][j+1] == 1 && i != N-1 && j != N-1) count++; if(cell[i][j]==0){ if(count==3){ c[i][j]=1; } } if(cell[i][j]==1){ if(count==2||count==3){ c[i][j]=1; } } if(cell[i][j]==1){ if(count<=1){ c[i][j]=0; } } if(cell[i][j]==1){ if(count>=4){ c[i][j]=0; } } for(i=0;i<M;i++){ for(j=0;j<N;j++){ cell[i][j]=c[i][j]; } } } int main(){ int t; system("clear"); init(); dump(); system("sleep 0.1s"); for(t=1;t<=MAXT;t++){ update(); system("clear"); dump(); system("sleep 0.1s"); } return 0; } ```ここに言語名を入力 c言語 ソースコード ```#include <stdio.h> #include <stdlib.h> #define MAXT 100 #define M 40 #define N 40 int cell[M+2][N+2]={0}; void init(){ cell[15][N/2]=1; cell[16][N/2]=1; cell[17][N/2]=1; cell[18][N/2]=1; cell[19][N/2]=1; cell[20][N/2]=1; cell[21][N/2]=1; cell[22][N/2]=1; cell[23][N/2]=1; cell[24][N/2]=1; return; } void dump(){ int i; int j; for(i=0;i<M;i++){ for(j=0;j<N;j++){ if(1 ==cell[i][j]){ printf("@"); }else{ printf("."); } } print("\n"); return; } } void update(){ int c[M][N] = {0}; int i; int j; int count = 0; if(cell[i-1][j] == 1 && i != 0) count++; if(cell[i+1][j] == 1 && i != N-1) count++; if(cell[i][j-1] == 1 && j != 0) count++; if(cell[i][j+1] == 1 && j != N-1) count++; if(cell[i-1][j-1] == 1 && i != 0 && j != 0) count++; if(cell[i-1][j+1] == 1 && i != 0 && j != N-1) count++; if(cell[i+1][j-1] == 1 && i != N-1 && j != 0) count++; if(cell[i+1][j+1] == 1 && i != N-1 && j != N-1) count++; if(cell[i][j]==0){ if(count==3){ c[i][j]=1; } } if(cell[i][j]==1){ if(count==2||count==3){ c[i][j]=1; } } if(cell[i][j]==1){ if(count<=1){ c[i][j]=0; } } if(cell[i][j]==1){ if(count>=4){ c[i][j]=0; } } for(i=0;i<M;i++){ for(j=0;j<N;j++){ cell[i][j]=c[i][j]; } } } int main(){ int t; system("clear"); init(); dump(); system("sleep 0.1s"); for(t=1;t<=MAXT;t++){ update(); system("clear"); dump(); system("sleep 0.1s"); } return 0; } ### 試したこと ### 補足情報(FW/ツールのバージョンなど) main関数は変えないでください ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

全体的に手を入れてみました。
ソースの差分を取って、どこが変わったか探してみてください。

c

1#include <stdio.h> 2#include <stdlib.h> 3 4#define GENERATIONS (100) 5#define HEIGHT (40) 6#define WIDTH (40) 7 8#define DEAD (0) 9#define ALIVE (1) 10 11int cell[HEIGHT][WIDTH] = {DEAD}; 12 13void init() { 14 cell[15][WIDTH / 2] = ALIVE; 15 cell[16][WIDTH / 2] = ALIVE; 16 cell[17][WIDTH / 2] = ALIVE; 17 cell[18][WIDTH / 2] = ALIVE; 18 cell[19][WIDTH / 2] = ALIVE; 19 cell[20][WIDTH / 2] = ALIVE; 20 cell[21][WIDTH / 2] = ALIVE; 21 cell[22][WIDTH / 2] = ALIVE; 22 cell[23][WIDTH / 2] = ALIVE; 23 cell[24][WIDTH / 2] = ALIVE; 24} 25 26void dump() { 27 for (int y = 0; y < HEIGHT; y++) { 28 for (int x = 0; x < WIDTH; x++) { 29 printf(cell[y][x] == DEAD ? "." : "@"); 30 } 31 printf("\n"); 32 } 33} 34 35void generate() { 36 int next[HEIGHT][WIDTH]; 37 38 for (int y = 0; y < HEIGHT; y++) { 39 for (int x = 0; x < WIDTH; x++) { 40 int count = 0; 41 if (y > 0 && cell[y - 1][x] == ALIVE) 42 count++; 43 if (y < HEIGHT - 1 && cell[y + 1][x] == ALIVE) 44 count++; 45 if (x > 0 && cell[y][x - 1] == ALIVE) 46 count++; 47 if (x < WIDTH - 1 && cell[y][x + 1] == ALIVE) 48 count++; 49 if (y > 0 && x > 0 && cell[y - 1][x - 1] == ALIVE) 50 count++; 51 if (y > 0 && x < WIDTH - 1 && cell[y - 1][x + 1] == ALIVE) 52 count++; 53 if (y < HEIGHT - 1 && x > 0 && cell[y + 1][x - 1] == ALIVE) 54 count++; 55 if (y < HEIGHT - 1 && x < WIDTH - 1 && cell[y + 1][x + 1] == ALIVE) 56 count++; 57 58 if (cell[y][x] == DEAD) { 59 next[y][x] = count == 3 ? ALIVE : DEAD; 60 } else { 61 next[y][x] = count == 2 || count == 3 ? ALIVE : DEAD; 62 } 63 } 64 } 65 for (int y = 0; y < HEIGHT; y++) { 66 for (int x = 0; x < WIDTH; x++) { 67 cell[y][x] = next[y][x]; 68 } 69 } 70} 71 72int main() { 73 system("clear"); 74 init(); 75 dump(); 76 system("sleep 0.1s"); 77 78 for (int generation = 1; generation <= GENERATIONS; generation++) { 79 generate(); 80 system("clear"); 81 dump(); 82 system("sleep 0.1s"); 83 } 84 return 0; 85}

投稿2020/07/24 16:26

編集2020/07/25 00:14
shiracamus

総合スコア5406

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

e-watt

2020/07/24 18:13

回答者は同手直ししたか(質問者のコードのどこに問題があったか)を追記した方が良いと思うのです。
episteme

2020/07/25 00:25

↑「それじゃ勉強にならん」と回答者が判断したんでしょ。それはそれで良いと思う。
kuroni

2020/07/25 07:06

解決しましたありがとうございました。利用が初めてで、操作がままならない中の投稿でしたが、回答していただきありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問