More info on cellular automata: http://en.wikipedia....lular_automaton
I was provided with a skeleton file for this assignment, and all I have to do is code the setupRow(), printRow(), and updateRow() functions. I'm pretty sure I did the first two correctly, but it's the updateRow() function that may have some problems. Can anyone please point me in the right direction?
#include <iostream>
#include <cstdlib>
using namespace std;
void setupRow();
void printRow();
void updateRow();
int findNextCellState(int left_neighbor, int cell, int right_neighbor);
const int WIDTH = 80;
int row[WIDTH];
int i = 0;
int temp[WIDTH];
int main()
{
setupRow();
printRow();
for (int i = 0; i < 100; i++)
{
updateRow();
printRow();
}
return 0;
}
// The setupRow function creates an initial state for the cellular autonoma
void setupRow()
{
for(i = 0; i < 80; ++i)
{
if(i != 39)
row[i] = 0;
if(i == 39)
row[i] = 1;
}
}
// The printRow function outputs the state of the row to the user. For all
// cells that are 0, it outputs a space (" "), for all cells that are 1, it
// outputs a star ("*")
void printRow()
{
for(i = 0; i < 80; ++i)
{
if(row[i] == 0)
{
cout << " ";
}
if(row[i] == 1)
{
cout << "*";
}
}
}
// The updateRow function updates the contents of the row after calculating
// the next states using the findNextCellState function
void updateRow()
{
temp[WIDTH] = findNextCellState(0, row[0], row[1]);
for(i = 0; i < 80; ++i)
{
temp[i] = findNextCellState(row[i-1], row[0], row[i+1]);
row[i] = temp[i];
}
}
// The states for this are:
//
// 111 110 101 100 011 010 001 000
// 0 0 0 1 1 1 1 0
int findNextCellState(int left_neighbor, int cell, int right_neighbor)
{
if (0 == cell)
{
if (1 == (left_neighbor + right_neighbor))
{
return 1;
}
else
{
return 0;
}
}
else
{
if (1 == left_neighbor + right_neighbor)
{
return 0;
}
else
{
return 1;
}
}
}
Edit: I forgot to mention that the output may look different if using different compilers. It looks different if you compile with g plus plus and view it in a UNIX environment, as opposed to how it may look in Visual C++. It looks a tad better in Visual if you ask me.
This post has been edited by nautica17: 26 February 2010 - 04:42 PM

New Topic/Question
Reply



MultiQuote





|