467,925 Members | 1,869 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,925 developers. It's quick & easy.

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
  • viewed: 3074
Share:
6 Replies
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
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
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
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

Post your reply

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

Similar topics

4 posts views Thread by f | last post: by
2 posts views Thread by Atz | last post: by
reply views Thread by XMLGuy | last post: by
7 posts views Thread by defcon8 | last post: by
4 posts views Thread by onkar | last post: by
1 post views Thread by lakshmiraj | last post: by
2 posts views Thread by conics | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.