Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this project, consider the one-dimensional cellular automaton, consisting of

ID: 3621429 • 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

Sample runs:
libra% java 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% java 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 methods. The first
one is displayCells():
public static void displayCells(int data[])
The data[] array contains data.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 method is updateCells():
public static void updateCells(int data[])
The data[] array contains data.length cells, either 1 or 0. updateCells() updates each
cell in the data[] array, according to the rules defined earlier in this handout. (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 method returns.)

Explanation / Answer

please rate - thanks

import java.util.*;
public class cellular_automation
{public static void main(String []args)
{Scanner in=new Scanner(System.in);
int n,i,steps;
System.out.println("Welcome to the cellular automaton simulation!");
System.out.print("Enter number of cells (<= 80): ");
n=in.nextInt();
while(n<1||n>80)
     {System.out.println("invalid length");
        System.out.print("Enter number of cells (<= 80): ");
       n=in.nextInt();
        }
int []cells=new int[n];
for(i=0;i<n;i++)
     cells[i]=0;
System.out.print("Enter number of time steps: ");
steps=in.nextInt();
System.out.print("Enter the index of occupied cells (negative index to end): ");
i=in.nextInt();
while(i>=0)
{if(i<n)
       cells[i]=1;
   i=in.nextInt();
    }
System.out.println();
for(i=0;i<n;i++)
   System.out.print(i%10);
System.out.println();   
System.out.println();
displayCells(cells);
for(i=0;i<steps;i++)
     {updateCells(cells);
    displayCells(cells);
    }
}
public static void displayCells(int data[])
{int i;
for(i=0;i<data.length;i++)
   if(data[i]==0)
        System.out.print(" ");
    else
        System.out.print("#");
System.out.println();
}
public static void updateCells(int a[])
{int n,i;
n=a.length;
int []t=new int[n];
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];

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote