This is a discussion on Conway's Life - C Program - Errors have me stumped - Please help within the C Programming forums, part of the General Programming Boards category; Hey guys, =) First year programing student here I'm having some trouble running a program I have written for the ...

Hey guys, =) First year programing student here

I'm having some trouble running a program I have written for the C Program "Conway's Life"
Some help fixing my errors would be HUGELY appreciated. I have spent over 6 hours just trying to fix the errors alone! If smart a C Programming person could fix the errors in the code, I'd love you forever! :P

Enough of my ranting - The problem is as follows:
--------------------------------------------------------------------------------------------------------------
"Conway's life is a special kind of game. It isn't really a game - just a spectator sport. It is played on a chess board, where each cell is either alive or dead. Each turn consists of deciding which squares on the board stay the same or change (becoming either alive or dead) and then admiring the new pattern they produce.

A live square stays alive if it has exactly two or exactly three live neighbours, otherwise it dies.
A dead square becomes alive if it has exactly three live neighbours, otherwise it stays dead.

All births and deaths occur simultaneously. The next round depends only on the positions at the end of the previous round. A neighbouring square is one step away in any direction (including diagonals).

Write a program that uses a 20x20 board, initialised to a given pattern, to play the game. After each turn print out the board pattern using a . for a dead square and a * for a live square. Squares off the board are considered permanently dead.

Hint: you need two 2-D arrays (both global variables). Each one being an array of strings. Form the second array from the first array, using the above rules, then copy it back to the first array, print it and repeat. After each repetition ask the user whether they want to continue or stop.

If your program is working correctly this pattern should show a glider hitting a brick

**..................
**..................
....................
....................
....................
....................
....................
........***.........
........*...........
.........*..........
....................

all the remaining rows are filled with dead squares "
----------------------------------------------------------------------------------------------------
And here is my code:

Code:
```/*Conways Life, 0.45, Sarah Stacey */

#include <stdio.h>
#include <conio.h>
#include <string.h>

void display_fctn (char b1, char b2);
void new_board (char b1, char b2);
void copy_back (char b1, char b2);
int neighbours (int p, int q);

char b1[20][21] = {"**..................",
"**..................",
"....................",
"....................",
"....................",
"....................",
"....................",
"........***.........",
"........*...........",
".........*..........",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"...................."};

int main() {
char c;
display_fctn(b1, b2);
printf("Would you like to play? (y/n)");
c = getch();
while (c = 'y') {
new_board(b1, b2);
copy_back(b1, b2);
display_fctn(b1, b2);
c = getch();
}
}

void display_fctn(char b1, char b2) {
int i;
for (i=0; i<20; i++) {
printf("&#37;s \n", b1[i]);
}
}

void new_board(char b1, char b2) {
int i, j, p, q;
for (i=0; i<20; i++) {
for (j=0; j<20; j++) {
neighbours(p, q);
if (b1[i][j] = '*') {
if (neighbours == 2 || neighbours == 3) {
b2[i][j] = '*';
} else {
b2[i][j] = '.';
} else {
if (neighbours == 3) {
b2[i][j] = '*';                        /*	b2 corresponding position gets a '*' */
} else {
b2[i][j] = '.';			                                     /*b2 gets a '.' */
}
}
}
}
/*	b2[i][j] = '\0'; 	/* ----assign  ????? \0 */
}

void copy_back(char b1, char b2); {
int i;
for (i=0; i<20; i++) {
strcpy(b1[i], b2[i]);
}
}

int neighbours(int p, int q); {
int count;
/* ((8 ifs?????))*/
if (p!=0 && q!=19 && b1[p-1][q+1] == '*') {
count = p + q;
}
return count; /* return count */
}
}

/* initialize variables eg int i = 0 ?? */```

If someone could fix my code up for me, I'd be absolutely in your debt :P

Thanks again,
~Sarah87

2. Code:
`while (c = 'y')`
For one, "c = 'y'" sets c to 'y' so the while() condition will always be true. Use == instead.

Maybe you could post the errors that you're getting so we don't have to guess.

3. Originally Posted by Sarah87

If someone could fix my code up for me, I'd be absolutely in your debt :P

Thanks again,
~Sarah87
Hi Sarah,

Where is the second char 2D array (b2) ?

In the function neighbors(), p and q represent row and col, why not call them that? Yes, you'll need more than one if statement to detect all it's adjacent cells. Think of it like the face of a clock:

Detect neighbor's state at 12, 2, 4, 6, 8, or 10?

If you want to post in colors, be sure they are very DARK colors. The sky blue types are tough to read.
Edit:
==> Much Better!
Now about that second 2D char array b2?

4. I'll try something quickly.

5. C'mon, Salem!

There must be at least TWO whole words that are different, in that copy of her post on this forum!

Guess that's the "tao" of getting your programming homework done now - get a basic template from "somewhere", and then post problems you have with it on as many programming boards as you can find.

Then wait for the answers to come rolling back in. Repeat, as needed.

I almost pity them when it comes time to take their tests in class. The dread and the urge to cheat, must be thick enough to cut with a knife in that classroom.

6. You are consistently attempting to pass your 2-D arrays as single char values to your functions. If these arrays are global then you don't really need to pass them at all.

C'mon, Salem!

There must be at least TWO whole words that are different, in that copy of her post on this forum!

Guess that's the "tao" of getting your programming homework done now - get a basic template from "somewhere", and then post problems you have with it on as many programming boards as you can find.

Then wait for the answers to come rolling back in. Repeat, as needed.

I almost pity them when it comes time to take their tests in class. The dread and the urge to cheat, must be thick enough to cut with a knife in that classroom.
Actually, if you want me to be honest... I posted the problem in 2 discussion groups, so that I could get a range of responses in utter urgency of handing this assignment in (yes, it's already late) As for getting a "basic template from somewhere" I can swear to you that the code I have written is completely my own work (not that I am proud of it or anything, it's riddled with errors, as you can see) Basically, to tell you the truth, I've never had a problem with my assignments until now ... this is assignment 8 ... but this one has been driving me absolutely crazy, and it has gotten to the stage where I just want to get it done asap, so that I can move on, and learn from every error I made, making sure not to do them again.
/endrant

Anyways, thank you for the help thus far

8. The issue isn't helping you, as much as it is in the definition of "help". By telling you to provide more information, we believe we are indeed helping you. I believe you want us to hand over a solution, which we won't do. We'd rather be pointed in the direction of what your problem is.

• Does it compile? If not, where errors and warnings does the compiler throw at you?
• If it runs, does it crash? If so, try to limit where it crashes by placing printf() statements all over the place or use a debugger.
• If it runs, and doesn't crash, does it just give you the wrong answer? What results from the program were you expecting and what did you get?

If the errors have taken you six hours thus far to attempt to fix, then you put a lot of work into it, and you should try to finish it yourself, albeit perhaps with some push in the right direction as opposed to one of us just fixing it up for you. The feeling of getting a hard project done and done right should be well worth the struggle.

9. OK Sarah, no need to pick on you more than anyone else.

Did you change your code to:

1) Create a b2 array, same type as b1, and also global as per the assignment?

2) Did you change the parameters to the called functions, so the whole array can be worked on with one call, instead of just ONE cell?

Or did you decide that calling the same function 400 times (20 * 20), or did you decide that was OK?

(Not my preference, but it can work, making the 400 calls).

3) Did you change the "=" to "==" where it was indicated in a post above?

4) Did you think about changing variables like 'p' and 'q' to something more meaningful?

Please post up your current code, and tell us what your compiler errors are, or what results you're getting that are not correct.

Then we can help. Just asking "Can you help", leaves us wondering, "help with what?"

10. Oops, double post.

11. Code:
```neighbours(p, q);
if (b1[i][j] = '*') {
if (neighbours == 2 || neighbours == 3) {```
p and q are uninitialized, I think you meant to use i and j there. And neighbours isn't a variable. And that single =. Maybe use this:
Code:
```// you didn't use p in your function anyway, you can
//delete the declaration of q from this function as this is not used
p=neighbours(i,j);
if(b1[i][j]='*'){
if(p==2||p==3){```
Sorry, I'm in a hurry...

12. Wow, progress (kinda) at last... I now have far less errors.

Here is my new code:

Code:
```#include <stdio.h>
#include <string.h>

void display_fctn (char b1[][21], char b2[][21]);
void new_board (char b1[][21], char b2[][21]);
void copy_back (char b1[][21], char b2[][21]);
int neighbours (int p, int q);

char b1[20][21] = {"**..................",
"**..................",
"....................",
"....................",
"....................",
"....................",
"....................",
"........***.........",
"........*...........",
".........*..........",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"...................."};

char b2[20][21];

int main() {
char c;
display_fctn(b1, b2);
printf("Would you like to play? (y/n)");
c = getchar();
while (c == 'y') {
new_board(b1, b2);
copy_back(b1, b2);
display_fctn(b1, b2);
c = getchar();
}
}

void display_fctn(char b1[][21], char b2[][21]) {
int i;
for (i=0; i<20; i++) {
printf("&#37;s \n", b1[i]);
}
}

void new_board(char b1[][21], char b2[][21]) {
int i, j, p;
for (i=0; i<20; i++) {
for (j=0; j<20; j++) {
p=neighbours(i,j);
if(b1[i][j]=='*'){
if(p==2||p==3){
b2[i][j] = '*';
} else {
b2[i][j] = '.';
} else {
if (neighbours == 3) {
b2[i][j] = '*';                        /*	b2 corresponding position gets a '*' */
} else {
b2[i][j] = '.';			                                     /*b2 gets a '.' */
}
}
}
}
/*	b2[i][j] = '\0'; 	 ----assign  ????? \0 */
}

void copy_back(char b1[][21], char b2[][21]) {
int i;
for (i=0; i<20; i++) {
strcpy(b1[i], b2[i]);
}
}

int neighbours(int p, int q) {
int count;
/* ((8 ifs?????))*/
if (p!=0 && q!=19 && b1[p-1][q+1] == '*') {
count = p + q;
}
return count; /* return count */
}
}

/* initialise variables eg int i = 0 ?? */```

and my new set of errors: ( they all seem to be very similar now, hopefully this means I'm not to far off :P)

cc1.exe: warning: command line option "-fpermissive" is valid for C++/ObjC++ but not for C
: In function `main':
:46: warning: control reaches end of non-void function
: In function `new_board':
:66: error: syntax error before "else"
: At top level:
:76: error: syntax error before '}' token
:93: error: syntax error before '}' token
Failure

A big thank you to those working with me to solve my problems =D

Code:
`return 0;`

Second this is illegal, both in syntax and in logic:
Code:
```if (cond)
then;
else
then;
else
then;```
which is what you have in the new_board() function. Additionally your brace and indent style in that function can lead to some confusion. Also if you count up the number of opening braces and the number of closing braces you'll find you are missing one.

14. your indentation style is very hard to follow...
You have one missing } because of it
Code:
```void new_board(char b1[][21], char b2[][21])
{
int i, j, p;
for (i=0; i<20; i++)
{
for (j=0; j<20; j++)
{
p=neighbours(i,j);
if(b1[i][j]=='*')
{
if(p==2||p==3)
{
b2[i][j] = '*';
}
else
{
b2[i][j] = '.';
}
} //missing
else
{
if (neighbours == 3)
{
b2[i][j] = '*';                        /*	b2 corresponding position gets a '*' */
}
else
{
b2[i][j] = '.';			                                     /*b2 gets a '.' */
}
}
}
}
/*	b2[i][j] = '\0'; 	 ----assign  ????? \0 */
}```

Page 1 of 2 12 Last