Menu

help with 2d arrays inside of classes

Ted
2008-12-12
2012-09-26
  • Ted

    Ted - 2008-12-12

    Hello all,

    I'm currently taking an intro to C++ class and I have an assignment to write a "game of life" program using a 2d array, but the catch is that the array has to be inside of a class titled boolMatrix. I've written the class constructor and print member functions for boolMatrix (both of which work) and now I am trying to write a set member function and I can't get it to work. I've been fiddling with it for quite some time now, and at this point I'm really guessing. Here is my code with what seems like the most logical way to write the set function. Can someone point me in the right direction with an explanation of your thought process.

    I'm sure this is very basic, but this is brand new to me and very confusing. Thanks in advance.

    Ted

    main.cpp file:

    include <iostream>

    include <fstream>

    include "boolmatrix.h"

    using namespace std;

    /*

    */

    const int NUMBER_COLUMNS = 20;
    const int NUMBER_ROWS = 20;

    void setZeroGeneration (boolMatrix currentGeneration);
    //void calculateGenerations(const int gensToCalc,
    // char currentGeneration[NUMBER_ROWS][NUMBER_COLUMNS],
    // char nextGeneration[NUMBER_ROWS][NUMBER_COLUMNS]);

    int main()
    {
    boolMatrix currentGeneration;
    boolMatrix nextGeneration;
    int gensToCalc;

    setZeroGeneration(currentGeneration);
    cout &lt;&lt; &quot;----------THE GAME OF LIFE----------&quot; &lt;&lt; endl &lt;&lt; endl;
    cout &lt;&lt; &quot;Generation 0&quot; &lt;&lt; endl &lt;&lt; endl;
    currentGeneration.print();
    cout &lt;&lt; endl &lt;&lt; &quot;How many generations do you want to calculate? &quot;; 
    cin &gt;&gt; gensToCalc;
    

    // calculateGenerations(const gensToCalc, currentGeneration, nextGeneration);

    system(&quot;pause&quot;);
    

    }

    void setZeroGeneration (boolMatrix currentGeneration)
    {
    ifstream infile("data.in");
    int row;
    int column;

    infile &gt;&gt; row &gt;&gt; column;
    while (infile){
        currentGeneration.set(row, column, true);
        infile &gt;&gt; row &gt;&gt; column;
    }
    

    }

    /void calculateGenerations(const int gensToCalc,
    char currentGeneration[NUMBER_ROWS][NUMBER_COLUMNS],
    char nextGeneration[NUMBER_ROWS][NUMBER_COLUMNS])
    {
    for (int count = 0; count < gensToCalc; count++){
    //stuff goes here
    }
    }
    /


    boolMatrix.h file:

    ifndef BOOLMATRIX_H

    define BOOLMATRIX_H

    /*

    */
    class boolMatrix
    {
    public:

        boolMatrix();
        void boolMatrix::print() const;
        void boolMatrix::set(int row, int column, bool value);
    
    private:
    
               bool array[20][20];
    

    };

    endif // BOOLMATRIX_H


    boolmatrix.cpp file:

    include <iostream>

    include "boolmatrix.h" // class's header file

    using namespace std;

    const int NUMBER_COLUMNS = 20;
    const int NUMBER_ROWS = 20;

    // class constructor
    boolMatrix::boolMatrix()
    {
    for (int currentRow = 0; currentRow < NUMBER_ROWS; currentRow++)
    {
    for (int currentCol = 0; currentCol < NUMBER_COLUMNS; currentCol++)
    {
    array[currentRow][currentCol] = false;
    }
    }
    }

    //print
    void boolMatrix::print() const
    {
    char printMatrix[NUMBER_ROWS][NUMBER_COLUMNS];

    for (int currentRow = 0; currentRow &lt; NUMBER_ROWS; currentRow++)
    {
        for (int currentCol = 0; currentCol &lt; NUMBER_COLUMNS; currentCol++)
        {
            if(array[currentRow][currentCol] == true){
                printMatrix[currentRow][currentCol] = '*';
            } else
                printMatrix[currentRow][currentCol] = ' ';
        }
    }
    
    cout &lt;&lt; &quot;  01234567890123456789&quot; &lt;&lt; endl;
    for (int currentRow = 0; currentRow &lt; NUMBER_ROWS; currentRow++)
    {
        if (currentRow &lt; 10){
            cout &lt;&lt; &quot; &quot; &lt;&lt; currentRow;
        } else
            cout &lt;&lt; currentRow;
        for (int currentCol = 0; currentCol &lt; NUMBER_COLUMNS; currentCol++)
        {
            cout &lt;&lt; printMatrix[currentRow][currentCol];
            if(currentCol == 19)
                cout &lt;&lt; endl;
        }
    }
    

    }

    //set
    void boolMatrix::set(int row, int column, bool value)
    {
    if (value == true){
    array[row][column] = true;
    }
    }

     
    • cpns

      cpns - 2008-12-13

      ... as it happens, you should not feel bad, I happen to think that your code was pretty good.

      You have to realise that you can imagine any tone of voice when reading written text. The tone I applied in my head was one of helpful advice of the kind "in future it would be helpful if you could...", whereas you seem to have imagined "Ha ha, look at what the idiot has done!...!", which was not my intent at all!

      Be assured that if I thought you were an idiot, I would have just said that.

      Clifford

       
    • cpns

      cpns - 2008-12-12

      You wrote a lot (of unnecessary waffle), but did not actually explain what your problem was other tha, "it don't work". In what way does it not work, and what exactly is it supposed to do.

      Since you appear to have expended some effort, how about you explain your though process, then someone might point you in the right direction.

      I am sorry if that sounds harsh, but asking a smart precise question will get you the best answer. Also we have to be careful not to be duped into doing your homework.

      However at a guess I would imagine that what you actually need is simply:

      void boolMatrix::set(int row, int column, bool value)
      {
      array[row][column] = value ;
      }

      otherwise when value is false, the array would not be changed. That is not about coding, that is merely about clear thought.

      Clifford

       
      • Ted

        Ted - 2008-12-12

        Clifford,

        Thanks for what I assume is an attempt to make me feel bad.

        I actually found the error, I forgot to pass by reference in the client program, a minor oversight that is easy to miss. I was hoping to get a pair of fresh eyes on my code. Yes I understand that your version of code is more efficient. I'm new at this and am still figuring it all out.

        I find your response shocking and nothing more than an attempt to be hurtful. Shame on you.

         
    • cpns

      cpns - 2008-12-13

      > Yes I understand that your version of code is more efficient.

      No, my version works! Yours was just incorrect. You had no way to set to false.

      My point was simply, get to the point, ask the question, we don't need to wade through the preamble to find that you forgot to ask a question and have to guess at what you intended to ask. That way you get an answer and we don't waste any time, everyone's happy.

      Its a public forum, toughen up; there are far ruder and less helpful denizens of cyberspace out there than me!

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.