C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 5th, 2013, 01:57 PM
joerobert joerobert is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 2 joerobert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old March 5th, 2013, 03:17 PM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 22 h 45 m 15 sec
Reputation Power: 1736
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.

Reply With Quote
  #3  
Old March 5th, 2013, 05:16 PM
joerobert joerobert is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 2 joerobert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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: ............***.............

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Cellular Automata in C Homework Help!!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap