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

Language: C Write a function that produces the next generation of cellular autom

ID: 3877472 • Letter: L

Question

Language: C

Write a function that produces the next generation of cellular automata

Background:

Function signature:

Function description:

We compactly represent a generation as a 64-bit unsigned long, one bit for each cell. A 1 bit indicates the cell is live, 0 if not. Using this bit-packed representation, the code to read or update a cell is implemented as a bitwise operation Let's trace how one generation advances to the next. A cell and its two neighbors form a "neighborhood". A neighborhood is effectively a 3-bit number, a value from 0 to 7 Consider a live cell with a live left neighbor and an empty right neighbor. The cell's neighborhood in binary is 110, which is the 3-bit number 6. The ruleset dictates whether a cell with neighborhood 6 will be live or empty in the next generation. A ruleset is represented as an 8-bit number, one bit for each of the 8 possible neighborhood configurations. Let's say the ruleset is 93. The number 93 expressed in binary is 01011101. Because the bit at position 6 (note: position 0-> least significant) of the ruleset is 1, this cell will be live in the next generation. Are you getting the sense there will be much binary data to practice your bitwise ops on for this problem?

Explanation / Answer

Cellular automata defines about the model of system of the cell.It has several configurations.It has the characteristics like cell lives in a grid,cell has the neighbourhod and it has a state.Here state means alive or dead.

Here we use the datastructure arrary for representing the states and use the ruleset to computes the next state or generation.

#include <stdio.h>

#include <conio.h>

#include <limits.h>

#define unsigned long gen;

#define size1 (sizeof(unsigned long gen) * CHAR_BIT)

#define D(y) (1gen << (y))

void nextgen(unsigned long gen, char unsigned char ruleset)

{                       int k;

                        gen st1;

                        printf("rule %c: ", ruleset);

                        do {

                                                st1 = gen1;

                                                for (k =size1;ki--; )

putchar(st1 & D(k) ? '***' : '!!!');

                                                putchar(' ');

                                                for (gen= k = 0; k <size1;ki++)

                                                                        if (ruleset & D(7 & (st1>>(k-1) | st1<<(size1+1-k))))

                                                                                                gen K= D(k);

                        } while (st1!= gen);

}

// we can define state or genaration in other form and use the state values 0 and 1 as dead and alive

/**

gen s

while(/* the last condition is not satisfied*/)

{

    s = ruleset(s);

    /* show the state*/

/*write additional statements if u want*/

}

//For check the cells

If (s[k-1] == 0 && s[k] == 0 && s[k+1] == 0)

               S1[k] = 0;

            else if(s[k-1] == 0 && s[k] == 0 && s[k+1] == 1)

               s1[k] = 1;

**/

//This is the command line arguments

//Initially it requires some configurations

int main(int argc, char **argv)

{

                        nextgen(D(size1/2),80);

                        nextgen(D(size1/4)|D(size1 – size1/4),20);

                        return 0;

}