Cellular automata are fascinating models which have been studied extensively and have a wide range of applications in many fields. In this task you will implement an elementary cellular automaton.
Details
Cellular automata have a long history in mathematics and computer science and were first discovered in the 1940s by John von Neumann and Stanislaw Ulam, two of the founding fathers of computer science. Since then they have found many applications in diverse field including artificial life, physics and biology. While simple to describe and implement, they are capable of generating amazingly complex behaviour, including chaotic dynamics and universal computation.
For this task you will implement the simplest type of cellular automaton – an elementary cellular automaton. .
Your program when run should take three values from the user as command line arguments. These values are:
These three arguments should be specified in this order on the command line when the user runs your program. You should check that the arguments are all valid and within the ranges given. If the user enters the wrong number of arguments or invalid arguments e.g. the number of cells as -10, then your program should print an error message and exit with a non-zero exit value.
For the values specified by the user, your program should calculate and print each generation of the cellular automata on a newline. You should print a * (asterisk) character to show a cell is alive, and a (space) character to show a cell is dead. For the initial generation, you should start with a row of cells that are all dead, except for the centre cell (or left of centre cell for an even number of cells) which is alive. You should assume that your row wraps around, that is, the neighbour of the left-most cell is the right-most cell and vice versa.
For example, the output when the users run your program with 30 cells for 20 generations with rule 110 should be:
$ ./task1 30 20 110
*
**
***
** *
*****
** *
*** **
** * ***
******* *
** ***
*** ** *
** * *****
***** ** *
** * *** **
*** **** * ***
** * ** ***** *
******* ** *** *
**** ** * **
** * ***** ***
*** **** * ** *
You are free to choose whichever method you want to implement your program with the following restrictions:
Some hints for possible ways to implement your program are given below.
You could store the states of cells in the current and next generations in two arrays. The length of each array will be the number of cells specified by the user. Since this length is specified at runtime, you will need to allocate the memory for these arrays dynamically using malloc or calloc.
The main computation in your program could be performed by two nested loops. The outer loop would iterate over each generation. For each generation, an inner loop would iterate over all the cells in the current generation array. Based on the state in the current generation of each cell and its two neighbours, you can calculate the state in the next generation and store this in the next generation array. After calculating the next generation states for all cells in the current generation, you can then print out the next generation. Finally you can copy the next generation array to the current generation array, and perform the whole procedure again for the next generation.
To calculate the state of a cell in the next generation from it and its neighbours current state, you can use some simple binary manipulation. For example, say you were using the ruleset with Wolfram code 110 and you wanted to calculate the state in the next generation of a dead cell with two live neighbours i.e. 101. First, treat 101 as a binary number and convert it to decimal i.e. 5. Now right shift the rule number i.e. 110 by 5 and look at the value of the least significant bit i.e. 0. This is the state of the cell in the next generation. A complete set of pictures for each Wolfram code is given here. Rule 90 and Rule 110 are particular interesting.
Your program should handle dynamically allocated memory correctly i.e. free all dynamically allocated memory.
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more