• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

solved... Damn (maxRow -1) statements

Fullmetal Chocobo

Moderator<br>Distributed Computing
Moderator
I'm doing this 'Game of Life' program for CS 2308. I know what I have to do, but I can't get it to work. I'm having issues with writing the second 2d array, and then making the first second one the first one.

I don't know what the hell is going on with this project... I will post the source code if anyone wants to take a gander and tell me what I'm screwing up, or what to research to find the solution (I don't mind researching an answer--just don't know where to look).

I believe the problem is with assigning an int (return from deadOrAlive() ) to a cell in the 2d array.

Conways Game of Life

Project description

Source code:
source code

I've got the output correct now. I just have to figure out why the bottom two lines make it crash after the second iteration... I need these two lines to prevent memory leaks, and to swap up the arrays as to create the 'animation'.

delete [] text;
text = newtext;

 
Originally posted by: Colt45
It just adds the value off the array (at row, col) , plus 1, to 'temp', right?

That's what I was thinking. And the (char) part just type casts it to type char.
 
Originally posted by: dighn
which line?

The line was "temp += (char) (text[row][col] +1);", but I think I understand that part. Now I'm researching how to get the integer into the 2d array, and then get them swapped in the iterateGeneration() function...
 
I can't see your code, but if you really need to swap two large arrays, you probably need to represent them with pointers.

Now, representing a 2d array with a malloc-ed array is a little tricky, but not bad. You just need to realize that text[row][col] in the original is equivalent to mtext[row*colSize+col], where mtext is a malloc-ed 1D array (of size rowSize*colSize), and colSize is the width of your array columns. Once you do that, you can swap pointers with impunity.

I have some other ideas I could introduce here. For instance, you could sum 3 values vertically into array 2; then sum array 2 horizontally and subtract the single center cell from array 1, writing the result back into array 1. But if that confuses you, just ignore it. (I don't think I've tried it, anyway.)
 
Ugh, I'm so close. I finally figured out everything else. Now I just can't get the damn arrays to swap more than once... At least I'm at a point where I can trouble shoot though...
 
The swap is not where it's crashing. By inserting printf statements, I've narrowed it down to...well, why don't you try it and see?

You can either insert printf or cout statements in key places (in general, after key operations, and add statements inside loops as you find it necessary), or use gdb (which I don't remember how to use.)

And don't forget...C++ arrays start at 0 and end at ______?
 
Originally posted by: Ken g6
The swap is not where it's crashing. By inserting printf statements, I've narrowed it down to...well, why don't you try it and see?

You can either insert printf or cout statements in key places (in general, after key operations, and add statements inside loops as you find it necessary), or use gdb (which I don't remember how to use.)

And don't forget...C++ arrays start at 0 and end at ______?

LOL. Very impressive! Yes, I found the location when I went to tutoring today for about an hour. It was the result of a lot of cout << "I am here" statements and drawing on the whiteboard. It was dying when it was calling the countNeighbors the 2nd time. And all because of (numRows - 1) and (numCols -1).

Thank you very much for taking a look at the program. Program is pretty cool when loaded with a large array. 🙂
 
Back
Top