The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Cellular Automata in C Homework Help!!
Discuss Cellular Automata in C Homework Help!! in the C Programming forum on Dev Shed. Cellular Automata in C Homework Help!! C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 5th, 2013, 01:57 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 2
Time spent in forums: 32 m 13 sec
Reputation Power: 0
|
|
|
Cellular Automata in C Homework Help!!
Hello all,
I am extremely new here, and almost as new to the C language, and this homework for my CS101 class is killing me!
The idea is that I need to write a program using the very basics of C (Arrays are the most advanced thing we've covered) that will simulate one-dimensional cellular automata. It has to read an initial state, then compute the specified number of subsequent states.
This is the assignment page:
http://faculty.ycp.edu/~dhovemey/spring2013/cs101/assign/assign03.html
My program will print the first generation (Gen 0) with no problems, but after that it says all of the following generations are all dead.
This is the code have written so far:
Code:
//Joe Robert
//CS101
//Assign_03: Rule 30
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main (void)
{
int A[100]; //current generation
int cells; //number of cells
int gen = 0; //number of generations
int genNum = 0; //current generation number
int B[100]; //"next" generation
int i;
printf("How many cells? ");
scanf ("%i", &cells); //cells per generation
printf("Enter the cells: "); //1=alive, 0=dead
for (i = 1; i < cells+1; i++) {
scanf ("%i", &A[i]); //stores cell values in an array
}
printf("How many generations? ");
scanf ("%i", &gen); //number of generations to make
printf("\n");
printf("Gen 0: ");
for (i = 1; i < cells+1; i++) {
if (A[i] == 1) //output:0=., 1=*
printf("*");
else
printf(".");
}
printf("\n");
for (i = 1; i < gen+1; i++){
if (A[i-1] == 0 && A[i] == 0 && A[i+1] == 0)
B[i] = 0;
else if (A[i-1] == 0 && A[i] == 0 && A[i+1] == 1)
B[i] = 0;
else if (A[i-1] == 0 && A[i] == 0 && A[i+1] == 0)
B[i] = 0;
else if (A[i-1] == 0 && A[i] == 1 && A[i+1] == 1) //calculate
B[i] = 1; //next
else if (A[i-1] == 1 && A[i] == 0 && A[i+1] == 0) //generation
B[i] = 0;
else if (A[i-1] == 1 && A[i] == 0 && A[i+1] == 1)
B[i] = 1;
else if (A[i-1] == 1 && A[i] == 1 && A[i+1] == 0)
B[i] = 1;
else if (A[i-1] == 1 && A[i] == 1 && A[i+1] == 1)
B[i] = 0;
genNum++;
printf("Gen %2i: ", genNum);
for (int j = 0; j < cells; j++)
if (B[j] == 1)
printf("*");
else //print next generation
printf(".");
printf("\n");
A[i] = B[i]; // stores next generation as first generation,
} // so the next can be calculated
return 0;
}
For what it's worth, we use Notepad++ to write our code and Cygwin terminal to compile. I'm not sure how much that matters.
Thanks very much in advance for any help you can give. I have been at this for days with no luck.
|

March 5th, 2013, 03:17 PM
|
 |
Lord of the Dance
|
|
|
|
|
You have some mistakes with the logic.
You have only three states where it can be alive, but the documentation have four.
When you copied the first if check with A[i], you forgot to change one of them, so you have one duplicate and one missing check.
How much should one loop do?
Think you have to make the alive/dead validation of the complete row, before you print out the next row.
|

March 5th, 2013, 05:16 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 2
Time spent in forums: 32 m 13 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by MrFujin You have some mistakes with the logic.
You have only three states where it can be alive, but the documentation have four.
When you copied the first if check with A[i], you forgot to change one of them, so you have one duplicate and one missing check.
How much should one loop do?
Think you have to make the alive/dead validation of the complete row, before you print out the next row. |
I understand your first correction, that was just a silly mistake.
Your second comment confuses me a bit, though. If I understand correctly, you're saying I should use a nested loop?
I took out the last for loop (the one that would print the output) and put the print statements into the newly nested loop like this:
Code:
for (i = 1; i < gen+1; i++){
genNum++;
printf("Gen %2i: ", genNum);
for (int k = 1; k < cells+1; k++) {
if (A[i-1] == 1 && A[i] == 0 && A[i+1] == 0) {
B[i] = 1;
printf("*");
}
else if (A[i-1] == 0 && A[i] == 1 && A[i+1] == 1) {
B[i] = 1;
printf("*");
}
else if (A[i-1] == 0 && A[i] == 1 && A[i+1] == 0) {
B[i] = 1;
printf("*");
}
else if (A[i-1] == 0 && A[i] == 0 && A[i+1] == 1) { //calculate
B[i] = 1; //next
printf("*"); //generation
}
else if (A[i-1] == 0 && A[i] == 0 && A[i+1] == 0) {
B[i] = 0;
printf(".");
}
else if (A[i-1] == 1 && A[i] == 1 && A[i+1] == 0) {
B[i] = 0;
printf(".");
}
else if (A[i-1] == 1 && A[i] == 0 && A[i+1] == 1) {
B[i] = 0;
printf(".");
}
else if (A[i-1] == 1 && A[i] == 1 && A[i+1] == 1) {
B[i] = 0;
printf(".");
}
}
Is this what you had meant? Now this is giving different results, but still not close to the right ones.
EDIT: Not sure what made me decide to use a 'k' in the nested loop, but I've switched that to an 'i' and now it's printing one subsequent generation nearly properly, but no farther than that.
This is what it prints:
Code:
How many cells? 30
Enter the cells: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
How many generations? 14
Gen 0: ..............*...............
Gen 1: ............***.............
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|