Well, did you try it?
I see 25x25 grids now. They look like they change from generation to generation. I haven't analyzed to see if it's doing the right thing, (mostly because I don't know what that is) but it's doing something.
Well, did you try it?
I see 25x25 grids now. They look like they change from generation to generation. I haven't analyzed to see if it's doing the right thing, (mostly because I don't know what that is) but it's doing something.
Last edited by mike65535; 05-04-2011 at 09:11 AM.
@mike65535
Where did you add "printf("\n")"
Please share source code of that part...
You added it!
You're almost there. When you initialize the array[][] you should also assign the same values to n_array[][]. Otherwise n_array will have garbage in its outer edges because they are never initialized. This garbage will be placed in the array after 1st generation.
I didn't understand what you mean.
I think,I have already done what you want
Actually, the output is not working properly.
@nonoob
Please show over the code
output is:Code:#include <stdio.h> #include <stdlib.h> #include <time.h> #define ROWS 25 #define COLS 25 void game_of_life(char o_arr[][COLS], char n_array[][COLS]); int checkx(char arr[][COLS],int row, int col); int main(void) { char array[ROWS][COLS],n_array[ROWS][COLS]; srand(time(NULL)); int i, j, r_integer[ROWS][COLS], num_gen; for(i=0;i<ROWS;i++){ for(j=0;j<COLS;j++){ r_integer[j][i] = (rand() % 100) > 50 ? 1 : 0; } } for(i=0;i<ROWS;i++){ for(j=0;j<COLS;j++){ if(r_integer[j][i] == 0) array[j][i]=' '; else array[j][i]='X'; } } for(i=0;i<ROWS;i++){ printf("\n"); for(j=0;j<COLS;j++){ printf("%c", array[j][i]); } } for(num_gen=0;num_gen<4;num_gen++){ printf("\nGeneration: %d\n", num_gen+1); game_of_life(array,n_array); for(i=0;i<ROWS;i++){ for(j=0;j<COLS;j++){ array[j][i]=n_array[j][i]; } } for(i=0;i<ROWS;i++){ printf("\n"); for(j=0;j<COLS;j++){ printf("%c", n_array[j][i]); } } } return(0); } void game_of_life(char o_arr[][COLS], char n_array[][COLS]) { int i, j, num_neighbors=0; for(i=0;i<COLS;i++){ for(j=0;j<ROWS;j++){ if ((o_arr[j][i])=='X'){ num_neighbors = checkx(o_arr, j+1, i+1); if( num_neighbors <4 && num_neighbors >= 2) n_array[j][i]='X'; else if (num_neighbors >=4 || num_neighbors < 2) n_array[j][i]=' '; } else { num_neighbors=checkx(o_arr, j+1, i+1); if (num_neighbors == 3) n_array[j][i]='X'; else n_array[j][i]=' '; } } } } int checkx(char arr[][COLS],int row, int col) { int num_neighbors = 0; if ((arr[row-1][col-1])=='X') num_neighbors++; if ((arr[row-1][col])=='X') num_neighbors++; if ((arr[row][col-1])=='X') num_neighbors++; if ((arr[row][col+1])=='X') num_neighbors++; if ((arr[row+1][col-1])=='X') num_neighbors++; if ((arr[row+1][col])=='X') num_neighbors++; if ((arr[row+1][col+1])=='X') num_neighbors++; return(num_neighbors); }
Code:X XXX X X X XX XXX XX XX XXXXX X XXX X XXXX XX X XXXX XX XX X X X XX XX X XX X XX XXXX X XXXX X X X X X X X X XX X XXXX XXX XX XX XX XXXX XX XX XXX X XXX XX XX X X XXX XXXX X X X X XXX X X X X XXXXX X XX XX X XXXXX X XX X X X X X XXXXX X XXX XXXX X X XXX X X X X XX XXX X X X XX XXX X XXX XX XX XXX X X X X X X X XXX X X X XX XX XX XXX X XX X X X X XXX X X X XXX X XX XX XX XX X XX XX X XXX X X XXXXXX X XXXX X X XX XX XXXX XXX X X X XXXX XX XXXXX XXXX XX XXXXX Generation: 1 XX X XXX X X X XXXXX X XX X XX XX X XX X X X X XXX X XXX XXX XX XX X XXX X X XX X X X X X X XX X X X XXX XXX X X X XX XX X X X X X X XXX X XX X X X XX X XX XXXXX X X X X X X X X X XX X XXX X X XX X X XX X XX XX X X XX X X X XXX X X X X X X X X XX XX X XXX XX X X X X XX X X XX X XX XX X X X XX X X X X XX XX XX X X X X XX X XXX XXXX XXX XX X Generation: 2 X X XX X XXX XX XX XXXX X XX XX X X X XXXX X X X XXX X X XX XX X XXX X XX X X X X X X X X XX X XX X X X X XX XX X X XX XX X XX X X X X X X X XX X X XX X XXXX X XX X X X XX X X X XXX XX X XX XX X X X X XX XX X X X XX X X X X XXX X XXXXX XX X X XX X XXXX X XXX XX XX X X X X XXX XX X XX X XX X XX X X XX XXX X X X XX X XX X X Generation: 3 XXX X X XX X XX X XX X X XX X X X XX X X X X XX X XX X XXXX X XXX XX X X X X X X XX X X X XX X X X X X X X X XX X XX XX X XX X X XX X X X XX XX X X XX X XXX X XX X XX XX X X XX X X X X XX XX XX XX X X X X XX X X X X XXX XX XX XX X X X XX X X X X X XX XX X X X X X X X X XX X X XX XXX X XX X XXXX XXXXX XX XX XX X X X Generation: 4 X X X X X X XX X XXX X X XXXX X X X XX X X X X XX X X XX X X X XXX X X XX XX X X X XXX X X XX X X X X X XXX X XX X X XX X X X XXX XXX XX X XXXXX X X X X XXX X X X X XX XX XX X XX XXXXX X X X X X XX X X X XXX X XX XXXX XXX XX XXX XXXX X XX XX X X XX X X XXXX X XX X X X X XX XX X XXX X XX X X X X XX XXXXXX XX X X
Last edited by paranoidgnu; 05-04-2011 at 10:08 AM.
Last edited by paranoidgnu; 05-04-2011 at 10:01 AM.
It would be a lot easier to see if it was working, if you initialised your array to say a glider, rather than some random pattern.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I shrunk your ROWS and COLS to 5 for testing, which made it much easier. I noticed a three problems:
1. Why are you calling checkx with j+1 and i+1? You should just be using j and i, so it corresponds to the same grid coordinate you checked in if (o_arr[j][i]).
2. You're missing the following case from your checkx function: arr[row-1][col+1]
3. You don't properly handle the corner/edge cases. Your checkx code needs to make sure that it doesn't go out of bounds of the grid, so you need something like:
Do likewise for the other 7 neighbors.Code:if (row > 0 && col < COLS-1 && arr[row-1][col+1] == 'X') // only check the square to the top-right if it's in bounds
Last edited by anduril462; 05-04-2011 at 11:33 AM. Reason: typo
I noticed that in the results - it appeared to be ignoring some of the diags. I didn't dig into it. Good catch.2. You're missing the following case from your checkx function: arr[row-1][col+1]
The last thing I want to mention is your messy use of i and j. Your grid is row-major, meaning you defined it with array[ROWS][COLS], but you don't always access it that way, sometimes you access array[col][row]. This could easily cause a seg fault if your grid is non-square, and will definitely produce incorrect results as you iterate through generations. When doing something with a 2-d arrays that are "rows and columns", I recommend using more sensible index variable names, like r and c or row and col. That way, you always make sure you're doing things in the right order, since all your accesses are array[row][col], and things like array[col][row] stand out as wrong. I'll let your find/fix all your loops/array accesses.
You need to check that inside the function game_of_life(), the loops and the call to checkx don't go out of bounds.
I already pointed out that there was an error in checkx() but I was ignored.
paranoidgnu,
I think Mike got a good one. I haven't personally gone over your code yet, but...
Suspicious is an understatement. The number line draws that the Algebra teacher has one do would demonstrate that is the entire number line, and thus always true. Let's see...
So no mater where num_neighbors is on the number line, the conditional statement is true.... I hope I didn't make that too difficult, but those sorts of correctness proofs come in handy when dealing with logic errors. Please do keep them in mind.Code:=-------------------------------> (2nd) <----------------------------= (1st) <------------------------|-------|------|------|---------------------------------> 0 2 3 1st Condition: n <= 3 2nd Condition: n >= 2
Best Regards,
Kept the text books....
Went interdisciplinary after college....
Still looking for a real job since 2005....
During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.
@ new_ink2001 He claims to have fixed that.
But
Why so convoluted?Code:if( num_neighbors <4 && num_neighbors >= 2) n_array[j][i]='X'; else if (num_neighbors >=4 || num_neighbors < 2) n_array[j][i]=' ';
Doesn't that do the same thing?Code:if( num_neighbors == 2 || num_neighbors == 3) n_array[j][i]='X'; else n_array[j][i]=' ';
This is confusing, too:
Why add one to i and j (which should be row and column...or is it vice versa?) before you send? Why not send the actual current location and let checkx do the math?Code:checkx(o_arr, j+1, i+1)
I think checkx needs a bounds test as well (for when it runs up against the limits of the valid grid), but could be wrong.