Hi all,

I looking for somebody to help me through a final class program. I would be happy to pay somebody to tutor me through this or make a donation. I am not looking for somebody to just give me the answer, but I DEFINITELY need help. It is due on Monday. Only two people in the entire class have had any success with it. I'm pretty lost. If someone can help me, please let me know. I have all the info pertaining to the assignment. Thanks alot.

THe actual assignment is 3 pages long with requirements, so I'll just give you the cut and dry version:

Write a program that plays the game of life. Create a class with the name "boolMatrix" for storing and processing a two dimensional array of bool values. We need to read in a dat file that basically contains a pair of coordinates representing the row / col of the locations containing the true values (live cells). the array must be 20 by 20 to start. We need 8 member functions.

This is basically it, with a bunch more specs about whether a cell lives in the future generations and such... The problem is that this is the first time we have used either classes or 2 d arrays and I'm having a terrible time using them together.

1. Plan what you think you need to do on paper, i.e NO CODE
2. Draw a flow chart, how does your program go from start to finish?
3. Play with 2d arrays, filling and printing them out.
4. Learn how a basic class can be built.
5. Think about what methods your class will have.

Well I have spent quite a bit of time working on this... I just don't really understand it and the class is online and the teacher is never available for help. Which is probably why 22 people have dropped. Here's what I've come up with so far. It's not much and I truthfully don't understand exactly where I'm going with it.

Is there a way to attach the entire assgnment?

#include <iostream>
#include <fstream>
#include "boolMatrix.h"
using namespace std;
const int num_rows = 20;
const int num_cols = 20;
boolMatrix booleanMatrix[row_num][col_num];
int row = 0;
int col = 0;


boolMatrix::boolMatrix()
{
     for(int row = 0; row < num_rows; row++){
         for(int col = 0; col < num_cols; col++){
              return false;
         }                          
     }
}


boolMatrix boolMatrix::get( boolMatrix booleanMatrix)const
{
     return booleanMatrix (row, col);      
}

void boolmatrix::set (boolMatrix booleanMatrix)
{
     ifstream infile("location_alive.dat");
     int row, col;          
     
     
     while(infile){
           infile >> row >> col;
           booleanMatrix.set(row,col,"TRUE");
     }    
}

Please post your code with code tags. See the watermark in the background.

Ok, briefly tell me what all your member functions are for your class and what you expect them to do?

At the bottom of the "Advanced reply", you can attach a number of different types of files. And if you do post the code right in your post, please use Code tags. (More info in my sig)

Just a note: you're not allowed to return anything in a class's constructor, which is what you're trying to do in boolMatrix::boolMatrix()

Can't see any more until you post the other files... but it looks good.

Also what does BoolMatrix.h look like?

What does your dat file look like.

Post an example please and use code tags.

[edit]I need sleep, I'll be back in the morning.[/edit]

Remember plan it first before you do any coding!

Hi ProgrammerJoe,

I uploaded the file, I hope it worked. Please let me know. Inside the main assignment are links to the input file. I'm not sure if they were cut out when I attached though.

I'm not sure what "tags" are. I haven't used them thus far in C++ other CSS HTML and ASP yes, but C++ not sure what they are.

I'm basically stuck at this point.

I had almost the same problem 2 weeks ago ,but i've done it in c (we aren't learning c++ until next semester)
The ideea is the same ,try to convert it in c++

/*  Autor:Matei Dragos
    Grupa:133
    Problema:No1 din lab6.pdf 
    Enunt:Plecand de la o generatie de virusi data, sa se scrie un program care sa calculeze
          numarul de virusi ai fiecarei generatii si sa se afiseze configuratia corespunzatoare,
          timp de un numar dat de generatii, stiind ca populatia evolueaza astfel:
          a) un virus moare cand e izolat (are mai putin de 2 vecini) sau sufocat (mai mult
             de 3 vecini)
          b) un virus apare daca are 3 vecini.
   Observatie:Acest program a fost creat si rulat cu succes cu Visual C++ 2005 Express 
              Edition pe Win XP.


*/

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define SIZE 100


int vecini(int v[SIZE][SIZE],int i,int j)

{   
    int k,q,suma=0;

    for(k=i-1;k<=i+1;k++)
        for(q=j-1;q<=j+1;q++)
            suma=suma+v[k][q];


    if(suma<3 || suma>4) return 1;
    else if(suma==3) return 2;

}



int main()
{

    int v[SIZE][SIZE],n,i,j,virusi=0,d;
    int nr,generatii;

    printf("N= ");
    scanf("%d",&n);
    srand((unsigned)time(0));

    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            v[i][j]=rand()%2;//generating values(0 or 1)

    printf("Numarul de generatii: ");
    scanf("%d",&nr);
    printf("Matricea initiala :\n");

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(v[i][j]==1) virusi++;
            printf("%d ",v[i][j]);
        }
        printf("\n");

    }

    printf("\nNumarul de virusi : %d",virusi);
    printf("\n");


    //adding a 0 border

    for(j=0;j<=n+1;j++)
        v[0][j]=v[n+1][j]=0;

    for(i=0;i<=n;i++)
        v[i][0]=v[i][n+1]=0;

    //elements checking
    generatii=nr;

    do 
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            { 
                d=vecini(v,i,j);
                if(v[i][j]==1 && d==1)
                    v[i][j]=0;
                else if(v[i][j]==0 && d==2)
                    v[i][j]=1;
            }
        }
        generatii--;
    }while(generatii!=0);

    //afisarea
    virusi=0;
    printf("\nMatricea dupa %d generatii :\n",nr);


    for(i=1;i<=n;i++)
    {  

        for(j=1;j<=n;j++)
        {   

            printf("%d ",v[i][j]);
            if(v[i][j]==1) virusi++;

        }
        printf("\n");

    }


    printf("\nNumarul de virusi : %d\n",virusi);

    return 0;

}

I'll try to come later and post some comments and change the code in english,but right now i have to go

I hope it helped

I'm not sure what "tags" are.

Did you click the link in my signature about code tags? It clearly describes how to use them. In case that doesn't make sense, just use them like this:

// code goes here!

Your code then ends up looking like this

//Nicely formatted code with code tags.

Also try inlinecode tags for use in a sentance like this.

Oh, and perhaps you want to attach the code files rather than the document. That's sorta more helpful ;).

Edited 3 Years Ago by happygeek: fixed formatting

Hey Joe,

I'm not sure what code files you want. The only code I have written so far is what I've put above, the three member functions. I thought you wanted the assignemtn specs, sorry...

Please describe what this method does:

boolMatrix::boolMatrix()
{
     for(int row = 0; row < num_rows; row++){
         for(int col = 0; col < num_cols; col++){
              return false;
         }                          
     }
}

The function makes no sense

void boolmatrix::set (boolMatrix booleanMatrix)
{
     ifstream infile("location_alive.dat");
     int row, col;          
     
     
     while(infile){
           infile >> row >> col;
           booleanMatrix.set(row,col,"TRUE");
     }    
}

What happens if you read row & col and they are out of bounds? You should check for errors.

Well my logic was to initialize everything to false. then read in the values of the file which are coordinates to true (live) cells.

I'm not sure how to check for errors. This is why I'm here. I'm desparately needing help. Our class started with 28 people and it's down to 5. The teacher doesn't really explain this stuff very well. If you could help me, I'd really appreciate it.

Well my logic was to initialize everything to false.

Good logic. How does this function do that? The first time you enter both loops you exit the entire function, never setting a thing.

I'm not sure how to check for errors. This is why I'm here. I'm desparately needing help. Our class started with 28 people and it's down to 5. The teacher doesn't really explain this stuff very well. If you could help me, I'd really appreciate it.

if ((row < 0) || (row >= num_rows))
{
    // error condition
}

Display the values as you read them to make sure they are being read properly.

OKay so the function should not be a value returning function?

What is it that needs to be set to false? The particular location of the array right? But in the default constructor there is no array?

I'm so confused!

Good logic. How does this function do that? The first time you enter both loops you exit the entire function, never setting a thing.

Would it even compile...
He's returning a boolean from a constructor, AFAIK constructors aren't allowed to have returnvalues (but I admit my C++ is rusty).

But in the default constructor there is no array?

You need to SET the values, not return them...
But first you must create the array.

Okay so I have my 2D array which is of type "boolMatrix" I'll call the array "booleanMatrix". So in my default constructor I hvae the nested for loops that go down the rows and across the column. Instead of returning false, does this work:

boolMatrix::boolMatrix()
{
    for(int row = 0; row< num_rows; row++){
         for(int col= 0; col<num_cols; col++){
booleanMatrix[row][col] = false;
}
}
}

I'm not clear on where to place the error checking?

Is this a start?

Okay so I have my 2D array which is of type "boolMatrix" I'll call the array "booleanMatrix". So in my default constructor I hvae the nested for loops that go down the rows and across the column. Instead of returning false, does this work:

boolMatrix::boolMatrix()
{
    for(int row = 0; row< num_rows; row++){
        for(int col= 0; col<num_cols; col++){
            booleanMatrix[row][col] = false;
        }
    }
}

MUCH better. :mrgreen: Although you want to format your code, so I took the liberty...;)

I'm not clear on where to place the error checking?

Error checking goes where you might have errors, such as reading in a value from the user or a file that's bogus.

Hey thanks WaltP... I really appreciate it... if you have some free time that we can work on this straight through, I would be happy to pay you for your time... I just desparately need to get this in by Monday at midnight...

SO i don't really need any error checking in thsi function then right?

Anyways so now on to the "set" and "get" member functions. I the code that I wrote at the beginning was what I THOUGHT was right, but apparantly it's not. Can you help me with those? THanks

I'm not sure what code files you want. The only code I have written so far is what I've put above, the three member functions.

I assumed when you posted this,

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

that you had already written boolMatrix.h . In any case, writing the header file of a class first is usually the way most people design their classes, as defining member functions first is kind of backwards...

He's returning a boolean from a constructor, AFAIK constructors aren't allowed to have returnvalues (but I admit my C++ is rusty).

Yes, that is correct. Neither constructors nor destructors are allowed to have a return type.

SO i don't really need any error checking in thsi function then right?

You mean for the boolMatrix::boolMatrix()? No. Remember, programs would almost always run perfectly if they weren't given input that they couldn't predict. It's the users that will do something unexpected, so you need error checking whenever there's something that could potentially be what you don't expect it to be. Then, think of all the different unexpected possibilities, and then make your error checking accomodate this. For example, let's say a user is supposed to enter a number from 1-5. There are 3 possibilities:

  • The user enters a number in the correct range (1-5)
  • The user enters a number larger than 5
  • The user enters a number smaller than 1

So, in your error checking, you would need to check the expression (number > 5 || number < 1).

Hope this helps

So can anybody help me with the next two function? Assuming the first one is solid... which I'm still unsure of.

Ok one down a few more to go:

default constructor: initialize all of the array elements to false
get: return the current contents of a single array element. Use arguments to indicate the row and column of the array element that should be returned.
· set: set the contents of a single array element. Use arguments to indicate the row and column of the array element that should be set, and the value to which it should be set.
· rowcount: return the number of true values that exist in any given row
· colcount: return the number of true values that exist in any given column
· totalcount: return the number of true values that exist in the entire array
· print: display the contents of the array, including the row and column indices (see the posted correct output). For each element of the array, a true value must be displayed as an asterisk ("*") and a false value must be displayed as a space. This member function is the only one that displays output.

Just out of interest what does your sample text files look like? You gave a description but an example would be better.

So can anybody help me with the next two function? Assuming the first one is solid... which I'm still unsure of.

Yes, but you have to post what you currently have for the next two functions (at least an attempt), and a detailed description of what the functions are supposed to do. If you haven't made any changes, you haven't done anything we told you about. If you have, we don't know what the code is now.

Hey guys, THanks for everything so far. I really appreciate it. I will be working on this all day until I get it as it is due Tomorrow.

iamthwee, what exactly are you looking for with the text files? you mean the initial file that has to be read in?

Ok on to the "get"function here's what I have. What I'm not understanding is how it works. The purpose of this function is so you can check whether a particular cell is alive or dead (true or false) right? Where is the specification as to the particular cell (i.e location)? Does this come from the client code?

boolMatrix boolMatrix::get(booleanMatrix)const
{
      return booleanMatrix[row][col];
}

iamthwee, what exactly are you looking for with the text files? you mean the initial file that has to be read in?

Yes.

Ok on to the "get"function here's what I have. What I'm not understanding is how it works. The purpose of this function is so you can check whether a particular cell is alive or dead (true or false) right? Where is the specification as to the particular cell (i.e location)? Does this come from the client code?

I don't think your get function is correct. Re-read your instructions.
It has to return what is in a single array, i.e it has to return a char most probably.

char boolMatrix::get(int row, int col)
{
    if ( matrix[row][col] == true)
      return '*' //return a star
    else
       return ' ' //return a space
}

The syntax may not be 100% correct but should give you an idea of how to continue.

And why have you left this to the last minute? Dearie me.

Yes, but you have to post what you currently have for the next two functions (at least an attempt), and a detailed description of what the functions are supposed to do. If you haven't made any changes, you haven't done anything we told you about. If you have, we don't know what the code is now.

So, no detailed description. We don't know if you are supposed to test in the function, simply return the value to be tested outside the function, or what. Details....

iamthwee, what exactly are you looking for with the text files? you mean the initial file that has to be read in?

Again, he's asking for details. What is it that makes the program work? Code files, data files, header files. Basically what we need to understand the problem.

Ok on to the "get"function here's what I have. What I'm not understanding is how it works. The purpose of this function is so you can check whether a particular cell is alive or dead (true or false) right? Where is the specification as to the particular cell (i.e location)? Does this come from the client code?

Probably.

boolMatrix boolMatrix::get(booleanMatrix)const
{
      return booleanMatrix[row][col];
}

The way this is written, somewhere else row and col are set to the specific location that is to be tested. This function returns the value of that position. I assume this value is then tested after the function returns.

Hi guys. I've attached the data file. It's just a data file that sets all the live cells initially. I believe it's just a pair of coordinates for the grid location of living cells. I'll also re attach the assignment specs.

Iamthwee ok I see what you're saying. So in the client code then I would need nested for loops that call the get function for every cell and stores the contents before calling to the print function that prints through the 2d array?

Yeah I know not good to wait til the last minute, but I"m taking 22 units this sem. and unfortunately the teacher waited to assign this until finals week. So I'm just trying to pull this out of the fire at this point.

Attachments
0   0
   0   5
   0   9
   0  19
   1   6
   1  14
   1  16
   2   4
   2   7
   2  11
   2  14
   2  15
   2  16
   2  18
   3   2
   3   5
   3   9
   3  10
   3  17
   3  19
   4   0
   4   3
   4   5
   4   9
   5   2
   5   3
   5   5
   5  15
   5  16
   5  17
   6   5
   6   6
   6  10
   6  11
   6  13
   6  14
   6  17
   6  18
   7   6
   7   8
   7  14
   7  16
   8   1
   8   5
   8   8
   8  10
   8  17
   9   3
   9   4
   9   6
   9   7
  10   0
  10   9
  10  19
  11   1
  11   2
  11   3
  11   5
  11   7
  11   8
  11  14
  11  17
  11  19
  12   4
  12   5
  12   6
  12   8
  13   0
  13   1
  13   7
  13  10
  13  11
  13  14
  13  15
  13  16
  13  17
  13  18
  14   2
  14  11
  14  13
  14  15
  14  17
  14  19
  15  11
  15  13
  15  18
  16   1
  16   2
  16   5
  16   8
  16  17
  17   5
  17   6
  18   6
  18  16
  18  18
  19   0
  19   3
  19   4
  19   7

Your code should begin to look like the following below. I've left the main methods for you to fill in.

#include <iostream>

using namespace std;

class boolMatrix
{
//define Public member functions
public:
   boolMatrix(); //default constructor
   ~boolMatrix();//default destructor
   
   char get( int,int );
   void set( int, int, bool );
   int rowCount(int);
   int colCount(int);
   int totalCount();
   void print();
   
//define Private member functions
private:
      bool booleanMatrix[20][20];
};


//Our default constructor
boolMatrix::boolMatrix()
{
    for(int row = 0; row < 20; row++)
    {
        for(int col= 0; col < 20; col++)
        {
            booleanMatrix[row][col] = false;
        }
    }
}

//Our default destructor
boolMatrix::~boolMatrix()
{
 //do its work
}

//get: return the current contents of a single array element
//Returns a single char, either a star or a space
char boolMatrix::get( int row, int col )
{
    if ( booleanMatrix[row][col] == true)
      return '*'; //return a star
    else
       return ' '; //return a space
}

//set: set the contents of a single array element. Use arguments to 
//indicate the row and column of the array element that should be set, 
//and the value to which it should be set.
void boolMatrix::set( int row, int col, bool var )
{
  if ( var == true )
  {
    booleanMatrix[row][col] = true;
  }
  else if ( var == false )
  {
    booleanMatrix[row][col] = false;
  }
}
//rowcount: return the number of true values that exist in any given row 
//as an integer
int boolMatrix::rowCount( int row )
{
    
}

//colcount: return the number of true values that exist in any given column 
//as an integer
int boolMatrix::colCount( int col )
{
    

}

//- totalcount: return the number of true values that exist in the entire array 
int boolMatrix::totalCount()
{
    
    
}

//print: display the contents of the array, including the row and 
//column indices (see the posted correct output). For each element
//of the array, a true value must be displayed as an asterisk ("*") 
//and a false value must be displayed as a space. This member function
// is the only one that displays output. 
void boolMatrix::print() 
{
     
     
}


int main()
{
    boolMatrix a; //test object
    a.set( 10, 10, true );
    cout << a.get( 10, 10 );
    
    cin.get();
}

Your turn.

This article has been dead for over six months. Start a new discussion instead.