Language: C
Write a function that produces the next generation of cellular automata
Background:
Function signature:
note that an unsigned long for the next gen is returned as a result.
Function description:
// here is the full code with explanation
// output is attached
?// plz comment if you need further clarification
?// hit a thumbs up if you liked it
?// CODE
#include <stdio.h>
// this function will return 1 if bit in the position pos is 1
in n
// or 0 if the bit in the position pos is 0 in n
// EX: 0101 0010 0011 0101 -> is a 16 bit number be n
// then getBit(n, 2) is 1 because, 2nd bit from lsb(starting from
0) is 1
// getBit(n, 15) is 0 because 15th bit(the msb bit) is 0
int getBit(unsigned long n, int pos) {
unsigned long n1 = 1;
n1 = n1 << pos; // this will set n1 as a
number in which only pos bit is set and rest are unset
if(pos < 0 || pos >= 64) return 0;
//printf(" n: %lu , pos : %d val :
%d n1 : %lu\n", n, pos, (n & n1) > 0, n1);
return (n & n1) > 0; // n & n1 is non
zero of pos bit is set else it is 0
}
unsigned long nextGen(unsigned long gen, unsigned char ruleset)
{
unsigned long nextGeneration = 0; // holds the
answer
for(int i = 0; i < 64; i++) {
int left = getBit(gen,
i-1);
int cur = getBit(gen,
i);
int right = getBit(gen,
i+1);
// we have left,
current, and right bits
// lets create a 3 bit
number
// which will give us
the position of the bit in ruleset
int bitPosition =
4*right + 2*cur + 1*left;
// find the bit at the
bitPosition in the given ruleset
// if it is 1 then ith
bit in the nextGeneration will be 1 else 0;
unsigned long result =
getBit(ruleset, bitPosition);
// setting the value of
result to the ith bit in the nextGeneration
nextGeneration =
nextGeneration | (result << i);
}
return nextGeneration;
}
int main() {
//take a number num = 18446744073709551615(in
actual code it is -1 but both are same binary number)
// its binary representation will be 64
1's
// 1111 1111 1111 1111 1111 1111 1111 1111 1111
1111 1111 1111 1111 1111 1111 1111
// now take ruleset = 92
// its binary representaion will be 0101 1100(
for this lsb at position 0 and msb at position 7)
// for this example the answer should be
9223372036854775809 i,e.,
// 1000 0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0001
// only msb and lsb bits are set
// for bit 1 in the nextGen number,
// left bit : 1
// cur bit : 1
// right bit: 0 ( as
there is no bit right to the lsb)
// so number will be 110 which equals binary
6.
// 6th bit in ruleset is 1
// so bit 1 in nuxtGen is live(means 1).
// but for others upto 63 bit,
// left bit : 1
// cur bit : 1
// right bit: 1
// so number is 7 and
the 7th bit in ruleset is 0, so all are zeros
// for 64th bit
// left bit : 0 ( as
there is no bit left to msb)
// cur bit : 1
// right bit: 1
// so number is 3, and
the 3rd bit in ruleset is 1.
// so number will have only msb and lsb are set
and rest as unset
unsigned long num = -1; // lets create a
number
unsigned char ruleset = 92;
unsigned long nextNum = nextGen(num,
ruleset);
printf("Given Number : %lu \nnextGeneration :
%lu",num, nextNum);
return 0;
}
OUTPUT:
Language: C Write a function that produces the next generation of cellular automata Background: Function signature:...
Objective: Write a program that implements the Game of Life cellular automata system invented by John Conway. 1. Create two game grids of size at least 50x50. These grid cells can be either Boolean or integer. In the following, I’ll refer to these as gridOne and gridTwo. 2. Set all cells in both grids to false. 3. Start by initializing gridOne. Allow the user to specify two different ways of initializing the grid: 1) by specifying a pattern file to...