473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

cellular automata c++ help

Hi,

I've been doin a project for my C++ class pertaining to Cellular Automata. I'm having a little trouble so before I start I'll give you guys all the details so what I know, you'll know.

Provide the program to state (On or OFF) for all of the elements of a 10 X 10 2D array for some time periods.

For this program any cell will be ON if 3 or 4 of the 5 relevant cells are ON. Relevant cells are itself and it's neighbors -- up, down, left, and right.

There is NO wrap around so any neighbors in the array are OFF. Input the number of periods the program is to run and all of the cells to be ON at the start of the run.

Use two arrays; one holding the result of the last frame calculations and the new one being created in this time frame. At the ends of each time frame print out the new data array. There must be ON cells on for each iteration.

Use modules to give your program maximum flexibility.
In a nutshell I know I have to start with an array in "Generation 0" and the code essentially will create a copy but I don't know how to start.

Can you guys help me? I mostly just need a template to create the 2D array and a template to pass in a file. (ie fstream) I never understood how to do that.

Thanks in advance.

EDIT:

Here's a copy of the text file I have to pass in:
ON OFF ON ON OFF OFF OFF OFF ON ON
ON ON OFF ON OFF ON OFF ON ON ON
OFF ON OFF ON ON OFF ON ON OFF OFF
ON OFF OFF ON ON ON ON OFF OFF OFF
ON ON ON ON OFF OFF OFF OFF ON ON
OFF ON ON OFF ON ON ON OFF ON ON
OFF ON OFF ON OFF OFF OFF ON OFF OFF
OFF ON OFF ON OFF ON OFF OFF OFF OFF
ON OFF ON ON ON OFF ON ON ON OFF
ON OFF ON OFF ON OFF OFF OFF ON OFF
Dec 3 '08 #1
6 3422
Studlyami
464 Expert 256MB
You need to break this down into simpler steps

First you need to set up your cells (your 2D array)
Then you need to read in a file (you were on the right track with the file)
map the keyword OFF/ON to the location in the array (verify you are not stepping outside of your array)

Then you need the input for the number of generations/steps the program should run.

Now in each generation check itself and all its neighbors and apply your rules.

If you are having a specific problem post it here. Also there are a lot of resources on opening files and parsing files including the article located on this site.
Dec 3 '08 #2
Thanks man, I've been working on it and here's what I have so far. Unfortunately, I can't even get it to read in the file (As I suspected would be the problem).

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. ifstream inFile;
  8. ifstream inFile2;
  9. ofstream outFile;
  10.  
  11. void printOut (int[10][10]);
  12. int OFForON (int[10][10],int,int);
  13.  
  14.  
  15. void main ()
  16. {
  17.     inFile.open("C:\\ONandOFF.txt"); 
  18.  
  19.     for (int j = 0; j < 10; j++)
  20.     {
  21.         for (int i = 0; i < 10; i++)
  22.         {
  23.             inFile >> cellArray[j][i];
  24.         }
  25.     }
  26.     inFile >> numRuns;
  27.     inFile.close();                                   
  28.  
  29.     int count = 0;
  30.     while (count < numRuns)
  31.     {
  32.         if (count > 0)
  33.         {
  34.             for (int d = 0; d < 10; d++) 
  35.             {
  36.                 for (int m = 0; m < 10; m++)
  37.                 {
  38.                     cellArray[d][m] = newArray[d][m];
  39.                     newArr[m] = 0;
  40.                 }
  41.             }
  42.         }
  43.         runTimePeriod(cellArray, newArray);
  44.         count++;
  45.  
  46.         printOut(cellArray);
  47.     }
  48.  
  49.     outFile.close(); 
  50.  
  51.     inFile2.open("C:\\Results.txt"); 
  52.     while (getline(inFile2, line))        
  53.     {
  54.         cout << line << endl;
  55.     }
  56.  
  57. }
  58.  
  59. void printOut (int someArray[10][10])
  60. {
  61.     for (int p = 0; p < 10; p++)
  62.     {
  63.         for (int h = 0; h < 10; h++) 
  64.         {
  65.             outFile << someArray[p][h] << " ";
  66.         }
  67.         outFile << "\n";
  68.     }
  69.     outFile << "\n\n";
  70. }
  71. int OFFOrON (int arrayCells[10][10], int row, int column)
  72. {
  73.     int neighborsOn = 0;
  74.  
  75.     if (arrayCells[row-1][column] == 1) 
  76.     {
  77.         neighborsOn++;
  78.     }
  79.         if (arrayCells[row+1][column] == 1) 
  80.     {
  81.         neighborsOn++;
  82.     }
  83.     if (arrayCells[row][column] == 1) 
  84.     {
  85.         neighborsOn++;
  86.     }
  87.     if (arrayCells[row][column+1] == 1) 
  88.     {
  89.         neighborsOn++;
  90.     }
  91.     if (arrayCells[row][column-1] == 1) 
  92.     {
  93.         neighborsOn++;
  94.     }
  95.     if (neighborsOn == 3 || neighborsOn == 4)
  96.     {
  97.         return 1;
  98.     } 
  99.     else
  100.     {
  101.         return 0;
  102.     }
  103.  
  104. }
I gotta be honest I'm blindly typing in code, lol.
Dec 3 '08 #3
vmpstr
63
It looks pretty good so far, I mean... I think some variables are missing? Did you post the whole code? If so, does it compile?

One note in the OFFOrON function: make sure you don't go outside of the boundary of the array. For instance, if row is 0, then arrayCells[row-1][column] will result in a segmentation fault. You should do something like:

if ( (row > 0) && (arrayCells[row-1][column] == 1) )

(and similar things for the rest of the if statements in there)
Dec 3 '08 #4
Like So...? I'm on my PC at home, so I don't have a compiler one here. Can you tell me if it at least looks... right? :-\

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. ifstream inFile;
  8. ifstream inFile2;
  9. ofstream outFile;
  10.  
  11. void printOut (int[10][10]);
  12. int OFForON (int[10][10],int,int);
  13.  
  14.  
  15. void main ()
  16. {
  17.     inFile.open("C:\\ONandOFF.txt"); 
  18.  
  19.     for (int j = 0; j < 10; j++)
  20.     {
  21.         for (int i = 0; i < 10; i++)
  22.         {
  23.             inFile >> cellArray[j][i];
  24.         }
  25.     }
  26.     inFile >> numRuns;
  27.     inFile.close();                                   
  28.  
  29.     int count = 0;
  30.     while (count < numRuns)
  31.     {
  32.         if (count > 0)
  33.         {
  34.             for (int d = 0; d < 10; d++) 
  35.             {
  36.                 for (int m = 0; m < 10; m++)
  37.                 {
  38.                     cellArray[d][m] = newArray[d][m];
  39.                     newArr[m] = 0;
  40.                 }
  41.             }
  42.         }
  43.         runTimePeriod(cellArray, newArray);
  44.         count++;
  45.  
  46.         printOut(cellArray);
  47.     }
  48.  
  49.     outFile.close(); 
  50.  
  51.     inFile2.open("C:\\Results.txt"); 
  52.     while (getline(inFile2, line))        
  53.     {
  54.         cout << line << endl;
  55.     }
  56.  
  57. }
  58.  
  59. void printOut (int someArray[10][10])
  60. {
  61.     for (int p = 0; p < 10; p++)
  62.     {
  63.         for (int h = 0; h < 10; h++) 
  64.         {
  65.             outFile << someArray[p][h] << " ";
  66.         }
  67.         outFile << "\n";
  68.     }
  69.     outFile << "\n\n";
  70. }
  71. int OFFOrON (int arrayCells[10][10], int row, int column)
  72. {
  73.     int neighborsOn = 0;
  74.  
  75.     if ( (row > 0) && (arrayCells[row-1][column] == 1) ) 
  76.     {
  77.         neighborsOn++;
  78.     }
  79.     if ( (row > 0) && (arrayCells[row-1][column] == 1) ) 
  80.     {
  81.         neighborsOn++;
  82.     }
  83.         if ( (row > 0) && (arrayCells[row-1][column] == 1) ) 
  84.     {
  85.         neighborsOn++;
  86.     }
  87.         if ( (row > 0) && (arrayCells[row-1][column] == 1) )
  88.     {
  89.         neighborsOn++;
  90.     }
  91.         if ( (row > 0) && (arrayCells[row-1][column] == 1) )
  92.     {
  93.         neighborsOn++;
  94.     }
  95.         if ( (row > 0) && (neighborsOn == 3 || neighborsOn == 4)
  96.     {
  97.         return 1;
  98.     } 
  99.     else
  100.     {
  101.         return 0;
  102.     }
  103.  
  104. }
Dec 3 '08 #5
Studlyami
464 Expert 256MB
First thing to do is get a compiler for your home PC. You can get the visual studio c++ express for free.

just briefly looking at your code I have a few comments

One is you shouldn't have global file objects.
Another issue is you never verify you text file is open and no errors occur.
I don't see where you have created your array; you just use an array.

Also, for getting the number of generations to run the CA I would get that input from the console. Getting that value from the text file you would need to ensure that the value is an integer. Also getting that value at the end of the file would increase the chance that the number will never be reached (if your text file is off at all).

Expand|Select|Wrap|Line Numbers
  1.      while (count < numRuns)
  2.      {
  3.          if (count > 0)
  4.          {
  5.              for (int d = 0; d < 10; d++) 
  6.              {
  7.                  for (int m = 0; m < 10; m++)
  8.                  {
  9.                      cellArray[d][m] = newArray[d][m];
  10.                      newArr[m] = 0;
  11.                  }
  12.              }
  13.          }
  14.          runTimePeriod(cellArray, newArray);
  15.          count++;
  16.  
  17.          printOut(cellArray);
  18.      }
  19.  
This appears back words to me.

For each generation for CA you should
Create new array.
step through the old array to assign value to new array
copy the new array into the old array (only have you have finished updating the generation)
Print the new generation to the screen.

I don't see anywhere in your assignment where you were suppose to write/read to/from a text file. Reading the array is fine, but I would just print the array to the screen and sleep for a short period of time and not mess with an output file.

where is your definition for runTimePeriod?

Your OnorOff function doesn't check the edges of your array and appears to be more cluttered than needed, but it could be functional. Also all the if statements are checking the same row position.

Its been a while since i had to pass around multidimensional arrays, but the way you are doing it doesn't appear to be correct.

Please get a compiler, get your code to compile and then post issues you are having (or specific errors in compiling). This would make it easier on everyone involved.
Dec 4 '08 #6
vmpstr
63
As Studlyami said, your ONOrOFF function just checks array[row-1][column]. It has the correct row > 0 check, but you also need to check other array positions.

row < 9 && array[row+1][column]
column > 0 && array[row][column-1]

etc
Dec 4 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: f | last post by:
I am programming j2me. Is there a way we can write a program to access information in a cellular phone from a pc or access pc information from cellular phone? Thanks, ff
2
by: Atz | last post by:
1.) If i want to make application which should communicate with laptop and cellular phone (thru cable or infrared) , should i use c++ / vc++ or something else.... Basicly, i want to send...
0
by: XMLGuy | last post by:
I initially posted this question in comp.theory but did not get much response. Please help! I think tree automata is very well studied area in math and computer science. Tree automata is also used...
7
by: defcon8 | last post by:
Hello. I have recently been experimenting with cellular automata and I would like to know how I could convert a 2d list of 0's and 1's into white and black squares on an image. I have tried to...
5
by: defcon8 | last post by:
I thought people would be interested in this little script I wrote to reproduce the 256 simple automata that is shown in the first chapters of "A New Kind of Science". You can see a few results...
4
by: onkar | last post by:
Can any one suggest me a game which uses FA as central concept and to be written in C.
1
by: lakshmiraj | last post by:
Hi everyone, I was trying a C++ program to implement a finite automata which reads the input from a text file. Can some one please help me with this? Thanks Lakshmi
2
by: conics | last post by:
i need to create an automata state machine simulator using c.. i know how it runs, problem is, i do not know the exact algorithm to put it in "C" another thing is, most samples i find now are...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.