3 Replies - 9225 Views - Last Post: 10 October 2010 - 07:51 PM Rate Topic: -----

#1 jvelasco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 07-October 10

C++ finite state cellular automata program?

Posted 10 October 2010 - 06:44 PM

#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 main()
{
    setupRow();
    printRow();

    for (int i = 0; i < 40; i++)
    {
        updateRow();
        printRow();
    }
}

// The setupRow function creates an initial state for the cellular autonoma
void setupRow()
{
   int row[ WIDTH ] = {};
	row[39]=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 ( int i=0; i < WIDTH; i++)
	{		
	    cout << ( row[i] ? "*" : " " );
	}
}

// The updateRow function updates the contents of the row after calculating
// the next states using the findNextCellState function
void updateRow() 
{
	int tempRow[WIDTH];

	for ( int i =0; i < WIDTH; i++ )
	{
	    tempRow[i] = row[i];	
	}

	for ( int i = 0; i < WIDTH; i++)
	{
	    int left  = 0;
	    int cellt = 0;
	    int right = 0;
	
	    if ( i > 0 )
		{
		    left = tempRow[i-1];
		}
		    cellt = tempRow[i+1];
	    if (i < WIDTH - 1 )
		{
		    right = tempRow[i+1];		    
		}
	   
	row[i] = findNextCellState (left, cellt, right );

	}

    
}

// The findNextCellState function implements the Rule 30 celular autonoma rules.
// 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)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
}


I've been working on this homework problem and got it to compile, but it's not giving me the correct output. Here are the guidelines that we got:
Write a program that will simulate 40 new states of a finite-state cellular automata
In computer science we will find that complicated behavior can arise from very simple rules. One example of this is Cellular Automatons.

* Use a global integer array of width 80 to represent the current state
* When the main starts, use a function to populate the array with all 0s, except for the middle (40th) element which should be set to 1
* Implement a method that will print theh row to the screen. Whenever the state is a 0, output a space (" ") and whenever the state is a 1, output a star("*")
* Implement a method that will look at every element in the current array, determine the next state, and save it in to a new array. When the method is finished, copy all elements of the new array in to the global array.
* Run a loop in the main method which will update the state and print out the new state 40 times

Am I doing this correctly? What do I need to change in order to get correct output?

This post has been edited by jvelasco: 10 October 2010 - 06:50 PM
Reason for edit:: Added code tags. Please use them in the future.


Is This A Good Question/Topic? 0
  • +

Replies To: C++ finite state cellular automata program?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ finite state cellular automata program?

Posted 10 October 2010 - 07:42 PM

So what output are you getting right now?
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: C++ finite state cellular automata program?

Posted 10 October 2010 - 07:48 PM

This is a local array, not the global one you want to initialize:

void setupRow()
{
   int row[ WIDTH ] = {};
    row[39]=1;
}


Was This Post Helpful? 0
  • +
  • -

#4 jvelasco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 07-October 10

Re: C++ finite state cellular automata program?

Posted 10 October 2010 - 07:51 PM

View PostOler1s, on 10 October 2010 - 06:42 PM, said:

So what output are you getting right now?

Right now I am getting 40 lines of output with one "*" one each of those lines. And that star is always on the first column.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1