2 Replies - 624 Views - Last Post: 09 October 2011 - 09:19 PM Rate Topic: -----

#1 RH1991  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-October 11

Need help with finite state cellular automata

Posted 09 October 2011 - 05:24 PM

Have been trying to figure this out for school, played around with it and got half of the pattern that i needed, but tried to change some things around and now i get the error message pa5.cpp: In function `void updateRow()':
pa5.cpp:77: error: incompatible types in assignment of `int' to `int[80]'

anyway i was wondering if anyone could give me some hints about what i am doing wrong.

here is the code

#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;

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()
{
    for (int i = 0; i < WIDTH; i++)
    {
        if( i == WIDTH)
          {
           row[i] = 1;
          }
        else
          {
           row[i] = 0;
          }
    }
}

// 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++)
   {
       if( row[i] == 0)
        {
          cout << " ";
        }

  }
   cout << endl;
}

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

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

    for ( int i = 0; i < WIDTH; i++)
     {
       int left_neighbor = i-1;
       int cell = i;
       int right_neighbor = i+1;

       if ( left_neighbor > WIDTH)
        {
           left_neighbor = 0;
        }

       if ( right_neighbor > WIDTH)
        {
           right_neighbor = 0;
        }

       row[i] = findNextCellState( temp_row[left_neighbor],
       temp_row[right_neighbor], temp_row[cell]);

     }
}

// 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;
        }
    }
}



Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with finite state cellular automata

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: Need help with finite state cellular automata

Posted 09 October 2011 - 05:53 PM

In the following snippet:
   int temp_row[WIDTH];

    for( i = 0; i < WIDTH; i++)
     {
         temp_row = row[i]

You have declared temp_row as an array, but in your loop you try to access this variable as a single integer. You need to tell the compiler which element of this array to assign the value of row[i].

Edit: You should also try to avoid using global variables when ever possible.

Jim

This post has been edited by jimblumberg: 09 October 2011 - 05:56 PM

Was This Post Helpful? 1
  • +
  • -

#3 RH1991  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-October 11

Re: Need help with finite state cellular automata

Posted 09 October 2011 - 09:19 PM

Ah thanks that first one was an easy fix but now the real problem I've been having is that when I run the program only half of the correct pattern shows up. I have only been using c++ for a few weeks now and I can't find any resources about this problem in my book or on the net, if anyone can point me in the right direction it would be greatly appreciated!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1