Search code examples
c++classobjectautomaton

cellular automaton in c++ with class and object


I did my cellular automaton in c but now I want to convert it to c++ with using class and object. I am new in c++ that is why I need your help. My program crashes after typing decimal number. I think data is not transfered properly between the functions, but I send few hours on it and I cannot get it. I would be pleased if I could get any advice with finding when my error is. I've got 3 files. One is my main, one is file with functions, and the last one is a header.

Main:

#include <iostream>
#include "cellular.h"
using namespace std;

int main()
{
    CA myCA;
    myCA.run();
    return 0;
}

File with functions:

#include "cellular.h"
#include <cstdio>


CA::CA()
{
    int WIDTH = 59;
    int numOfRules = 8;
    currentState = new int [WIDTH];
    nextState = new int[WIDTH];
    storeTheRules = new int[numOfRules];
}
CA::~CA()
{
    delete [] currentState;
    delete [] nextState;
    delete [] storeTheRules;
}

void CA::run()
{
    int x;
    int t;

    //enter which cellular you want to print out
    printf("Enter the number of cellular you want to print out 0-255 (-1 to end):\n");
    scanf("%d", &number);

    while(number != -1) {

        if(number >= 0 && number <= 255) {

            for(x = 0; x < WIDTH; x++) {

                currentState[x] = 0;
            }

            for(x = 0; x < WIDTH; x++) {

                t = (int)WIDTH/2;
                currentState[t] = 1;
            }

            // convert decimal number to binary
            decimalToBinary(number);
            // print binary number
            printf("In binary:");

            for(x = 0; x < numOfRules; x++)
            {
                printf("%d", storeTheRules[x]);
            }

            printf("\n");
            //print current state
            printCellular();
            printf("\n");
            // calculate for next generation
            calcNextGeneration();
            // update array
            updateArray();
        }

        else {

            printf("\nWrong number entered! Try again\n");
        }

        //enter which cellular you want to print out
        printf("\nEnter the number of cellular you want to print out 0-255 (-1 to end):\n");
        scanf("%d", &number);
    }
}
void CA::calcNextGeneration()
{
    int i;
    int j;
    int LENGHT = 27;
    for(j = 0; j < LENGHT; j++) {

        for (i = 0; i < WIDTH; i++) {

            left = currentState[i-1];
            middle = currentState[i];
            right = currentState[i+1];
            nextState[i] = rules(left, middle, right);

        }
        updateArray();
        printCellular();
        printf("\n");
    }
}

int CA::rules(int left,int middle, int right)
{

    if(left == 1 && middle == 1 && right == 1)

        return storeTheRules[0];

    else if(left == 1 && middle == 1 && right == 0)

        return storeTheRules[1];

    else if(left == 1 && middle == 0 && right == 1)

        return storeTheRules[2];

    else if(left == 1 && middle == 0 && right == 0)

        return storeTheRules[3];

    else if(left == 0 && middle == 1 && right == 1)

        return storeTheRules[4];

    else if(left == 0 && middle == 1 && right == 0)

        return storeTheRules[5];

    else if(left == 0 && middle == 0 && right == 1)

        return storeTheRules[6];

    else if(left == 0 && middle == 0 && right == 0)

        return storeTheRules[7];

    return 0;
}

void CA::printCellular()
{
    int i;
    for(i = 0; i < WIDTH; i++) {

        if(nextState[i] == 1 || currentState[i] == 1)

            printf("#");

        else
            printf(" ");
    }
}

void CA::updateArray()
{
    int i;
    for(i = 0; i < WIDTH; i++) {

        currentState[i] = nextState[i];
    }
}

// function to convert decimal number to binary
void CA::decimalToBinary(int n)
{
  int k;
  int i = 0;

  for (numOfRules = 7; numOfRules >= 0; numOfRules--) {

    k = n >> numOfRules;

    if (k & 1)

      storeTheRules[i] = 1;

    else

      storeTheRules[i] = 0;
      i++;
  }
  printf("\n");
}

Header:

#ifndef CELLULAR_H_INCLUDED
#define CELLULAR_H_INCLUDED


// A cellular automaton class
class CA {
    private:
        int WIDTH;
        int numOfRules;
        int *currentState;
        int *nextState;
        int *storeTheRules;
        int number;
        int left, middle, right;

    public:
        // Constructor
        CA();
        // Destructor
        ~CA();
        // Functions
        void run();
        int rules(int left, int middle, int right);
        void calcNextGeneration();
        void printCellular();
        void updateArray();
        void decimalToBinary(int n);
};

#endif // CELLULAR_H_INCLUDED

I am making my code in CodeBlocks. And.. include cstdio is because I didn't changed my printf's from C code yet.

Thank you for any help. Regards, Nel


Solution

  • I didn't read through everything, but a few issues upon first glance:

    • In your constructor you are creating local variables instead of accessing the class variables you intend to modify.

      int WIDTH = 59;
      int numOfRules = 8;
      
    • Also, just as a personal preference, I wouldn't organize this in such a way that a data entry loop getting input from the user is a part of a class. That could just be personal preference though.