4 Replies - 3674 Views - Last Post: 08 May 2010 - 01:44 AM Rate Topic: -----

#1 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Cellular Automaton Program

Posted 26 February 2010 - 04:41 PM

I am writing a program to simulate a cellular automaton. I think it works (technically), but I want to be sure if I should tweak it some more because it doesn't look quite like some of the other models I've seen. In other words, I think I'm missing something.

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


Is This A Good Question/Topic? 0
  • +

Replies To: Cellular Automaton Program

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,222
  • Joined: 23-August 08

Re: Cellular Automaton Program

Posted 26 February 2010 - 04:52 PM

temp is an array of WIDTH ints. Arrays are indexed by 0, so where do you think you're writing in memory with this code?
temp[WIDTH] =

Was This Post Helpful? 0
  • +
  • -

#3 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Re: Cellular Automaton Program

Posted 26 February 2010 - 05:34 PM

View PostJackOfAllTrades, on 26 February 2010 - 03:52 PM, said:

temp is an array of WIDTH ints. Arrays are indexed by 0, so where do you think you're writing in memory with this code?
temp[WIDTH] =


I would say that since variable 'WIDTH' is a global constant, then temp's ints are global? I really don't know now. :| I've tried replacing the size of the temp array with a local variable and I still get the same results when I run the program.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_MadScientist305*


Reputation:

Re: Cellular Automaton Program

Posted 26 February 2010 - 06:39 PM

You have a few errors let's see if I can help you with any.

What is the purpose of the temp array? Are you using it only to hold values temporarily?

Also, in the following code:
 temp[WIDTH] = findNextCellState(0, row[0], row[1]); 


you are attempting to access temp[80], which is out-of-bounds for your array. Since arrays start their indexing at 0, the valid indexes are temp[0]...temp[WIDTH-1]. Also, if the temp array is only a placeholder for values, why would you set its value here? Even if you fix the what I mentioned, you will overwrite the value you get here at the end of the for loop.

Also, on the for loops first iteration, row[i-1], where i == 0, which means you are attempting to set row[-1], which will cause an error. And the opposite occurs on the loops last iteration - when i == 79, row[i+1] is row[80], and just like the temp array, you are attempting access a piece of memory that has not been allocated for that array.
Was This Post Helpful? 0

#5 Guest_EM-kay*


Reputation:

Re: Cellular Automaton Program

Posted 08 May 2010 - 01:44 AM

can u do it in 2-D array ??

0 0 0 0 0 0 * 0
0 0 0 0 0 * 0 0
0 0 0 0 * * * 0
0 0 0 0 0 0 0 0
Was This Post Helpful? 0

Page 1 of 1