Jump to content

Almost done yay, just stuck on a toughie


CoolMaster

Recommended Posts

I knowI've been posting an absolute crapload but I've been working my butt of trying to get this done. here is what I have so far.

 

(it's alot beware)

 

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <time.h>


using namespace std;
const char slash = '_';
const char dot = '.';
const char star = '*';
const int maxrow = 20;
const int maxcolumn = 70;

void getfromfile(ifstream &infile, int generation[maxrow][maxcolumn], int &initrow, int &initcolumn);
double getrand();
void displaygen(int generation[maxrow][maxcolumn],int nextgen[maxrow][maxcolumn],int initrow, int initcolumn);
void generate (int generation[maxrow][maxcolumn], int nextgen[maxrow][maxcolumn], double like,int initrow, int initcolumn);
void copygen (int generation[maxrow][maxcolumn], int nextgen[maxrow][maxcolumn], int initrow, int initcolumn);


int main()
{
int generation[20][70];
int nextgen[20][70];

double like;
int cycles;

int initrow;
int initcolumn;
int counter = 0;
string filename;
ifstream infile;



cout << "What is the filename? ";
cin >> filename;
infile.open(filename.c_str());


if(!infile)
{

	cout << "File is corrupt or missing ending program..." << endl;
	return 1;
}

if(infile)
{
	cout << endl << "File Opened " << endl;
	cout << "How many time cycles would you like this program to run?: ";
	cin >> cycles;
	cout << "How likely is the virus to spread to a neighbor (0-1)? ";
	cin >> like;
	while(like < 0.0 || like > 1.0)
	{
		cin.clear();
		cout << "Incorrect Range, please enter a range from (0-1): ";
		cin >> like;
	}

	getfromfile(infile, generation, initrow, initcolumn);
	displaygen(generation,nextgen, initrow, initcolumn);

	while(counter < cycles)
	{

	 copygen (generation, nextgen, initrow, initcolumn);

	 generate(generation, nextgen, like,initrow,initcolumn);

	 displaygen(generation,nextgen, initrow, initcolumn);


	 counter++;
	}
}


 return 0;
}

double getrand() 
{
   srand((unsigned)time(NULL)); //Seed rand func
   double theRand = (rand() % 10 + 1) / 10.0;//gives a decimal back out
   return(theRand); 

}

void getfromfile(ifstream &infile, int generation[maxrow][maxcolumn], int &initrow, int &initcolumn)
{
 int rows,columns,a,b;
 int temp;
 infile >> initrow;
 infile >> initcolumn;

 for(int a = 0;a < initrow; a++)
 {
  for(int b = 0;b < initcolumn; b++)
  {
	  infile >> temp;
	  generation[a][b] = temp;
  }

 }

}

void displaygen(int generation[maxrow][maxcolumn],int nextgen[maxrow][maxcolumn],int initrow, int initcolumn)
{
for(int a = 0;a < initrow; a++)
{
  for(int b = 0; b < initcolumn; b++)
  {
	if(a == 0 || a == initrow-1)
	  {
		cout <<  slash;
	  }


	if(b == 0 && a != 0 && a != initrow-1)
	  {
	   cout <<  slash;
	  }

	if(b == initcolumn-1 && a != 0 && a != initrow-1)
	 {
	   cout <<  slash;
	 }



	if (generation[a][b] == 0 && a != 0 && b != 0 && a != initrow-1 && b != initcolumn-1)
	{
		cout << dot;
	}
	else if (generation[a][b] == 1)
	{
	 cout << star;
	}

  }
  cout << endl;
}
}


void generate (int generation[maxrow][maxcolumn], int nextgen[maxrow][maxcolumn], double like, int initrow, int initcolumn)
{

//generate your new generation into nextgen based upon generation your initial gen

for(int a = 0; a < initrow; a++)
{
	for (int b = 0; b < initcolumn; b++)
	{

	if(generation[a][b] == star)
	{
		nextgen[a][b] = slash;

	}



	}
}


}


void copygen (int generation[maxrow][maxcolumn], int nextgen[maxrow][maxcolumn], int initrow, int initcolumn)
{

 for(int i = 0; i < initrow; i++)
{
	for(int j = 0; j < initcolumn; j++)
	{

	generation[i][j] = nextgen[i][j]


	}

  }

}

 

 

 

 

 

 

---------------------------------------------------------------------------

ok now that you've seen it, im encurring a problem. when i do the copygen to copy generation [j] to nextgen[j]

and then it goes to display it, i get the first display ok, but after that it gets screwed up.

 

 

output ends up looking like this

 

 

--------------------------------------------

What is the filename? gen1.dat

File Opened
How many time cycles would you like this program to run?: 3
How likely is the virus to spread to a neighbor (0-1)? .3
______________________________
_...*........................_
_......*....*................_
_...........*................_
_............................_
_...........*................_
_............................_
_...........*................_
_............................_
_............................_
_...........*................_
______________________________
_______________________*_______
_..._
_....._
_.*............_
_....*.*._
_.._
_......_
_*..*..*.._
_.........._
_...................._
_*._
__________*_____*_______*__*______
_______________________*_______
_..._
_....._
_.*............_
_....*.*._
_.._
_......_
_*..*..*.._
_.........._
_...................._
_*._
__________*_____*_______*__*______
_______________________*_______
_..._
_....._
_.*............_
_....*.*._
_.._
_......_
_*..*..*.._
_.........._
_...................._
_*._
__________*_____*_______*__*______
Press any key to continue . . .


----------------------------------------------------

 

so something is going wrong when I change it from generation to nextgen, for my generate function i used a simple changing from stars (*) to Underlines (_)

 

yet it isnt even changing them, along with it completely messing up the rest of the outputs? I dont see what Im doing wrong?

 

 

 

im using a loop?

 

	 getfromfile(infile, generation, initrow, initcolumn);  <<< gets the initial stuff? ok that works
	displaygen(generation,nextgen, initrow, initcolumn); <<<< displays the initial stuff? that seems to be working.

	while(counter < cycles)   <<<<< this is where it messes up
	{

	 copygen (generation, nextgen, initrow, initcolumn); <<this should just convert it

	 generate(generation, nextgen, like,initrow,initcolumn);  <<< then change the *'s to _'s.....simple enough

	 displaygen(generation,nextgen, initrow, initcolumn);  <<< then redisplay it


	 counter++;
	}

 

sorry I know it's alot, but it's been like 6-7 hours and Im burnt out and I've tried switching like EVERYTHING in the loop and generate functions around........

 

anyone have any ideas at what the heck Im doing wrong...thx

Edited by hardnrg

Share this post


Link to post
Share on other sites

the first iteration displays generation[][]

 

the copygen function copies nextgen[][] TO generation[][]... and nextgen hasn't been populated...

 

also a slash is /, backslash is \, underscore is _, and a hyphen or dash is -

 

the "dead" specification shows a hyphen/dash

 

the generate function needs to look at the 8 neighbouring cells... so (row - 1) to (row +1), (col - 1) to (col +1), and figure out if it wraps around or just ignores on the boundaries

Share this post


Link to post
Share on other sites

Yeah I understand the neighboring cells thing, but why is it giving my crap output when the copygen comes into play?

 

from my understanding the copygen function works? it's just giving me wrong output, ..

 

 

OH Wait I see....i made nextgen = gen, it should be other way around.........

 

ok i changed it, it's not giving me crappy output anymore.

 

but the generate function is still not working, should I make it check the nextgen function? im guessing?

Share this post


Link to post
Share on other sites

ok i added in this

if(generation[a][b] == star)
{
	 nextgen[a][b] = slash;

	}

 

so it should've changed the stars to slashes (which are technically "_" ' s but w/e)

and I changed it from being generation[j] = nextgen[j]

to nextgen[j] = generation[j]

 

(yea dumb mistake)

 

 

but yet it keeps giving me the same output as the first one.....i mean it should be changing them?

 

I mean....is my program even on the right track? or am i due for a makeover :(

Edited by CoolMaster

Share this post


Link to post
Share on other sites

pseudo code to describe what is happening would help you here

 

* display generation[][]

* copy generation[][] to nextgen[][]

* make changes to nextgen[][]

* display generation[][]

 

see the problem here? you modify nextgen[][], but don't use it anywhere

 

it might be a good idea to copy the next iteration back to generation[][]...

Share this post


Link to post
Share on other sites

So I need to display nextgen in the loop I have created correct?

 

but wouldnt I need a Whole seperate function to do that, cause Im limited to that 1 function to display stuff, like if I could just make another display function for the nextgen array it wouldnt be as bad...but since I just have that one display function, how can I change the display function to display the new "nextgen" array, from what it normally displays. which is the original generation array......if that makes any sense. lol sorry Im prolly confusing as hell.

 

and btw does my program look at least somewhat ok so far?

 

btw I've been emailing my prof. too.....she's pretty much of no help. she told me to print out my array after generate function to see if it is right, but it's like i said....it keeps printing out same stuff

Edited by CoolMaster

Share this post


Link to post
Share on other sites

it might be a good idea to copy the next iteration back to generation[][]...

 

how can I change the display function to display the new "nextgen" array, from what it normally displays. which is the original generation array.....

 

i already told you how

 

copygen (nextgen, generation, initrow, initcolumn);

 

also your display function has two arrays in the parameters but only uses one... the names can be anything... like this:

 

void displaygen(int matrix[maxrow][maxcolumn], int initrow, int initcolumn)
{
for(int a = 0;a < initrow; a++)
{
  for(int b = 0; b < initcolumn; b++)
  {
	if(a == 0 || a == initrow-1)
	  {
		cout <<  slash;
	  }


	if(b == 0 && a != 0 && a != initrow-1)
	  {
	   cout <<  slash;
	  }

	if(b == initcolumn-1 && a != 0 && a != initrow-1)
	 {
	   cout <<  slash;
	 }



	if (matrix[a][b] == 0 && a != 0 && b != 0 && a != initrow-1 && b != initcolumn-1)
	{
		cout << dot;
	}
	else if (matrix[a][b] == 1)
	{
	 cout << star;
	}

  }
  cout << endl;
}
}

 

and you could call it like:

 

displaygen(generation, initrow, initcolumn);

// OR

displaygen(nextgen, initrow, initcolumn);

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...