iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
When you've actually got some code together post back. Have a go.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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!
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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
Eko
Junior Poster in Training
60 posts since Nov 2006
Reputation Points: 12
Solved Threads: 1
>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 do this:
[code]
// code goes here!
[/code]
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 ;).
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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 readrow & col and they are out of bounds? You should check for errors.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944