For this project, consider the one-dimensional cellular automaton, consisting of
ID: 3632752 • Letter: F
Question
For this project, consider the one-dimensional cellular automaton, consisting of an array
of cells. Each cell contains an integer, 1 or 0. (A cell containing a 1 is occupied, a 0 is
unoccupied.) On each time step, we update each cell according to the following rules, one
for each combination of a cell and its left and right neighbors:
Left Neighbor Current Cell[i] Right neighbor New Cell[i]
1 1 1 0
1 1 0 1
1 0 1 1
1 0 0 0
0 1 1 1
0 1 0 1
0 0 1 1
0 0 0 0
The cells at each end of the array have only one neighbor each, and are not updated.
For example, suppose we have an array of 8 cells, with 1’s at elements 4 and 6, and 0’s
elsewhere. Initially, we see
i 0 1 2 3 4 5 6 7
Cell[i] step 0 0 0 0 0 1 0 1 0
After one time step, the array becomes:
i 0 1 2 3 4 5 6 7
Cell[i] step 0 0 0 0 0 1 0 1 0
Cell[i] step 1 0 0 0 1 1 1 1 0
After two time steps, the array becomes:
i 0 1 2 3 4 5 6 7
Cell[i] step 0 0 0 0 0 1 0 1 0
Cell[i] step 1 0 0 0 1 1 1 1 0
Cell[i] step 2 0 0 1 1 0 0 1 0
After three time steps, the array becomes:
i 0 1 2 3 4 5 6 7
Cell[i] step 0 0 0 0 0 1 0 1 0
Cell[i] step 1 0 0 0 1 1 1 1 0
Cell[i] step 2 0 0 1 1 0 0 1 0
Cell[i] step 3 0 1 1 1 0 1 1 0
And so on. Your program should:
Prompt the user to enter the number of cells C
Declare an integer array cell[] with C elements
Prompt the user to enter the number of time steps N
Prompt the user to enter the index of cells that contain 1
(enter negative index to finish)
Run the cellular automaton for N time steps, using the rules defined above
On each time step, display the cells, printing a ‘#’ if the cell contains a 1,
a space if the cell contains a 0
Your program should produce
identical output to the sample executable. Sample runs:
libra% ./CA
Welcome to the cellular automaton simulation!
Enter number of cells (<= 80): 10
Enter number of time steps: 10
Enter the index of occupied cells (negative index to end): 4 6 -1
0123456789
# #
####
## #
### ##
# ####
### #
# # ##
######
# #
# ##
# ###
libra% ./CA
Welcome to the cellular automaton simulation!
Enter number of cells (<= 80): 40
Enter number of time steps: 30
Enter the index of occupied cells (negative index to end): 38 -1
0123456789012345678901234567890123456789
#
##
###
## #
#####
## #
### ##
## # ###
####### #
## ###
### ## #
## # #####
##### ## #
## # ### ##
### #### # ###
## # ## ##### #
######## ## ###
## #### ## #
### ## # #####
## # ### #### #
##### ## ### # ##
## # ##### # ## ###
### ## ## ######## #
## # ###### ## ###
####### # ### ## #
## # #### # #####
### ## ## ### ## #
## # ### ### ## # ### ##
##### ## ### ###### ## # ###
## # ##### ### ######## #
### #### ### # ## ###
libra%
You must organize your program so that your main program calls two functions. The first
one is displayCells():
void displayCells(int data[], int length)
The data[] array contains length cells, either 1 or 0. displayCells() prints each cell of
the data[] array on the same line, displaying a ‘#’ if the cell contains a 1, a space if the
cell contains a 0.
The second function is updateCells():
void updateCells(int data[], int length)
The data[] array contains length cells, either 1 or 0. updateCells() updates each cell in
the data[] array, according to the rules defined earlier in this handout, for one step. (Hint:
you will need to declare a temporary array in updateCells(), make the updates into the
temporary array, and copy the temporary to the data[] array before the function returns.)
Explanation / Answer
please rate - thanks
# include<stdio.h>
#include<conio.h>
void updateCells(int [],int);
void displayCells(int[],int );
int main()
{int n,i,steps;
int cells[80]={0};
printf("Welcome to the cellular automaton simulation! ");
printf("Enter number of cells (<= 80): ");
scanf("%d",&n);
while(n<1||n>80)
{printf("invalid length");
printf("Enter number of cells (<= 80): ");
scanf("%d",&n);
}
for(i=0;i<n;i++)
cells[i]=0;
printf("Enter number of time steps: ");
scanf("%d",&steps);
printf("Enter the index of occupied cells (negative index to end): ");
scanf("%d",&i);
while(i>=0)
{if(i<n)
cells[i]=1;
scanf("%d",&i);
}
printf(" ");
for(i=0;i<n;i++)
printf("%d",i%10);
printf(" ");
printf(" ");
displayCells(cells,n);
for(i=0;i<steps;i++)
{updateCells(cells,n);
displayCells(cells,n);
}
getch();
return 0;
}
void displayCells(int data[],int n)
{int i;
for(i=0;i<n;i++)
if(data[i]==0)
printf(" ");
else
printf("#");
printf(" ");
}
void updateCells(int a[],int n)
{int i;
int t[80];
t[0]=a[0];
t[n-1]=a[n-1];
for(i=1;i<n-1;i++)
t[i]=1;
for(i=1;i<n-1;i++)
if((a[i-1]==1&&a[i]==1&&a[i+1]==1)||
(a[i-1]==1&&a[i]==0&&a[i+1]==0)||
(a[i-1]+a[i]+a[i+1]==0))
t[i]=0;
for(i=0;i<n;i++)
a[i]=t[i];
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.