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.

Member Avatar for iamthwee

What's the problem then?

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.

Member Avatar for iamthwee

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.

Member Avatar for iamthwee

When you've actually got some code together post back. Have a go.

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");
     }    
}
Member Avatar for iamthwee

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.

Member Avatar for iamthwee

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 ;).

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.

Member Avatar for iamthwee

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];
}
Member Avatar for iamthwee

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.

Member Avatar for iamthwee

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.