944,124 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 5109
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
May 20th, 2007
0

C Program help - Conway's Life - Stumped by some errors.

Expand Post »
Hi there, I'm a first time poster and first year C programming student.

I am a little stuck on the following question:

-----------------------------------------------------------------------------
"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 "
-------------------------------------------------------




I have given it a very serious attempt, believe me, I've been trying all week and was up all night last night trying to fix the errors in it
I can see there are many errors showing in the compiler, but whenever I try to fix the remaining errors, more appear!

Here is the code I have come up with so far, I got the basic layout from a tutorial at my college, so that should be ok. My code is as follows:

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. void display_fctn (char b1, char b2);
  6. void new_board (char b1, char b2);
  7. void copy_back (char b1, char b2);
  8. int neighbours (int p, int q);
  9.  
  10. char b1[20][21] = {"**.................."
  11. "**.................."
  12. "...................."
  13. "...................."
  14. "...................."
  15. "...................."
  16. "...................."
  17. "........***........."
  18. "........*..........."
  19. ".........*.........."
  20. "...................."
  21. "...................."
  22. "...................."
  23. "...................."
  24. "...................."
  25. "...................."
  26. "...................."
  27. "...................."
  28. "...................."
  29. "...................."};
  30.  
  31.  
  32. int main() {
  33. display_fctn();
  34. printf("Would you like to play? (y/n)");
  35. c = getch();
  36. while (c = 'y') {
  37. new_board();
  38. copy_back();
  39. display_fctn(b1);
  40. c = getch();
  41. }
  42. }
  43.  
  44.  
  45. void display_fctn(char b1, char b2) {
  46. int i;
  47. for (i=0; i<20; i++) {
  48. printf("%s \n", b1[i]);
  49. }
  50. }
  51.  
  52. void new_board(char b1, char b2) {
  53. int i, j;
  54. for (i=0; i<20; i++) {
  55. for (j=0; j<20; j++) {
  56. neighbours(p,q);
  57. if (b1[i][j] = '*') {
  58. if (neighbours == 2 || neighbours == 3) {
  59. b2[i][j] = '*';
  60. } else {
  61. b2[i][j] = '.';
  62. } else {
  63. if (neighbours == 3) {
  64. b2[i][j] = '*'; /* b2 corresponding position gets a '*' */
  65. } else {
  66. b2[i][j] = '.'; /*b2 gets a '.' */
  67. }
  68. }
  69. }
  70. }
  71. b2[i][j] = '\0'; /* ----assign \0 */
  72. }
  73.  
  74. void copy_back(char b1, char b2); {
  75. int i;
  76. for (i=0; i<20; i++) {
  77. strcpy(b1[i], b2[i]);
  78. }
  79. }
  80.  
  81. int neighbours(int p, int q); {
  82. int count;
  83. /* ((8 ifs?????))*/
  84. if (p!=0 && q!=19 && b1[p-1][q+1] == '*') {
  85. count = p + q;
  86. }
  87. return count; /* return count */
  88. }
  89. }




Sorry if I waffled on a bit, I would really like to get this running and the errors out of the way, so that I can move on with my programing. Thank you very much to anyone who takes the time to help, I would really appreciate it! Hopefully one day I could return the favor, you look like a great bunch of people, and a great community to be a part of!
Thanks.


edit: Incase anyone wanted to know, these are the errors showing up in my compiler (A lot, I know): - Every time I go through to try and fix them, more come up.


:31: warning: initializer-string for array of chars is too long
: In function `int main()':
:7: error: too few arguments to function `void display_fctn(char, char)'
:35: error: at this point in file
:37: error: `c' undeclared (first use this function)
:37: error: (Each undeclared identifier is reported only once for each function it appears in.)
:8: error: too few arguments to function `void new_board(char, char)'
:39: error: at this point in file
:9: error: too few arguments to function `void copy_back(char, char)'
:40: error: at this point in file
:41: warning: invalid conversion from `char (*)[21]' to `char'
:7: error: too few arguments to function `void display_fctn(char, char)'
:41: error: at this point in file
: In function `void display_fctn(char, char)':
:50: error: invalid types `char[int]' for array subscript
: In function `void new_board(char, char)':
:58: error: `p' undeclared (first use this function)
:58: error: `q' undeclared (first use this function)
:59: error: invalid types `char[int]' for array subscript
:60: error: ISO C++ forbids comparison between pointer and integer
:60: error: ISO C++ forbids comparison between pointer and integer
:61: error: invalid types `char[int]' for array subscript
:63: error: invalid types `char[int]' for array subscript
:64: error: expected primary-expression before "else"
:64: error: expected `;' before "else"
:73: error: invalid types `char[int]' for array subscript
:79: error: invalid types `char[int]' for array subscript
:79: error: invalid types `char[int]' for array subscript
:89: warning: return-statement with a value, in function returning 'void'
Failure
Last edited by m.s; May 20th, 2007 at 11:02 am. Reason: Added more info
m.s
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m.s is offline Offline
4 posts
since May 2007
May 20th, 2007
0

Re: C Program help - Conway's Life - Stumped by some errors.

Compiler errors are compounded. The first error creates another and another, and so on.

For example: I see that you use a variable named c a few lines
below main, but you never declared that variable.
p and q suffers the same problem. Fix those and things start looking better.

you prototype:
void copy_back (char b1, char b2);
However you use it as:
copy_back();
You did not pass any argument, and it needs two.

void display_fctn (char b1, char b2); has the same problem.
Ckeck all arguments in the functions.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
May 20th, 2007
0

Re: C Program help - Conway's Life - Stumped by some errors.

  1. char b1[20][21] = {
  2. {"**.................."},
  3. {"**.................."},
  4. {"...................."},
  5. {"...................."},
  6. {"...................."},
  7. {"...................."},
  8. {"...................."},
  9. {"........***........."},
  10. {"........*..........."},
  11. {".........*.........."},
  12. {"...................."},
  13. {"...................."},
  14. {"...................."},
  15. {"...................."},
  16. {"...................."},
  17. {"...................."},
  18. {"...................."},
  19. {"...................."},
  20. {"...................."},
  21. {"...................."}
  22. };

Also look at your function declarations.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 20th, 2007
1

Re: C Program help - Conway's Life - Stumped by some errors.

> I can see there are many errors showing in the compiler,
> but whenever I try to fix the remaining errors, more appear!
Always start with the first one in the list. Everything else in the list can be a consequence of the compiler trying to make a guess as to how to fix the problem. Though with practice, you'll be able to filter out those and fix later actual errors as well in the same edit/compile cycle.

Also, it is entirely possible to write your code one line at a time, then press compile to see that it is OK. Then you never get into the situation of having lots of errors and no clue.
http://cboard.cprogramming.com/showthread.php?t=88495
In essence, never write more lines than you're prepared to deal with being wrong.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 21st, 2007
0

Re: C Program help - Conway's Life - Stumped by some errors.

Thanks very much for the help so far guys. Taking what you said on board, I managed to reduce my errors by about half ... am still very lost with the rest though.

I just had a look in my compiler options and the language was set to C++ instead of C, (I'm assuming this should be set to C considering I am learning the C language...right?) now it comes up with some different looking errors and more of them ... doh! This is driving me up the wall, I'm already late in handing this in (i realise this is entirely my fault )

I also remembered I never declared my b2 variable, as I wasn't sure how / where to do it. Could anyone show me how to do this?

Also ... it appears I have a conflict between integer comparison which is causing a lot of errors ... but I have no idea how to fix this.

Any further tips / corrections would be very much appreciated.

Thank you once again!
Last edited by m.s; May 21st, 2007 at 11:47 am.
m.s
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m.s is offline Offline
4 posts
since May 2007
May 21st, 2007
0

Re: C Program help - Conway's Life - Stumped by some errors.

So what's your latest code look like.

All the functions which pass a board back and forth need to be declared thus
void display_fctn (char b1[][21], char b2[][21]);

> int neighbours(int p, int q); {
Remove that ;
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 22nd, 2007
0

Re: C Program help - Conway's Life - Stumped by some errors.

Click to Expand / Collapse  Quote originally posted by Salem ...
So what's your latest code look like.

All the functions which pass a board back and forth need to be declared thus
void display_fctn (char b1[][21], char b2[][21]);

> int neighbours(int p, int q); {
Remove that ;


Latest code:
#include <stdio.h>
#include <conio.h>
#include <string.h>

void display_fctn (char b1[][21], char b2[][21]);
void new_board (char b1, char b2);
void copy_back (char b1, char b2);
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 = 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("%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 */
}
}


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







Latest errors: (Now that I am set the compiler to C instead of C++):
.c:4:
../lib/gcc/mingw32/3.4.0/../../../../include/conio.h:61: error: syntax error before "_COORD"
../lib/gcc/mingw32/3.4.0/../../../../include/conio.h:62: error: syntax error before '*' token
: In function `main':
:61: warning: passing arg 1 of `new_board' makes integer from pointer without a cast
:61: warning: passing arg 2 of `new_board' makes integer from pointer without a cast
:62: warning: passing arg 1 of `copy_back' makes integer from pointer without a cast
:62: warning: passing arg 2 of `copy_back' makes integer from pointer without a cast
: At top level:
:69: error: conflicting types for 'display_fctn'
:7: error: previous declaration of 'display_fctn' was here
:69: error: conflicting types for 'display_fctn'
:7: error: previous declaration of 'display_fctn' was here
: In function `display_fctn':
:72: error: subscripted value is neither array nor pointer
: In function `new_board':
:81: error: subscripted value is neither array nor pointer
:82: warning: comparison between pointer and integer
:82: warning: comparison between pointer and integer
:83: error: subscripted value is neither array nor pointer
:85: error: subscripted value is neither array nor pointer
:86: error: syntax error before "else"
:90: error: subscripted value is neither array nor pointer
:95:22: warning: "/*" within comment
: At top level:
:96: error: syntax error before '}' token
: In function `copy_back':
:101: error: subscripted value is neither array nor pointer
:101: error: subscripted value is neither array nor pointer
: At top level:
:113: error: syntax error before '}' token
Failure






Am I making progress?




Thanks again
m.s
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m.s is offline Offline
4 posts
since May 2007
May 22nd, 2007
0

Re: C Program help - Conway's Life - Stumped by some errors.

> All the functions which pass a board back and forth need to be declared thus
Yes, all of them.
Not just the single line I posted.

And remove conio.h and replace the getch() call with getchar()
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 22nd, 2007
0

Re: C Program help - Conway's Life - Stumped by some errors.

As a side note:
Instead of using [inlinecode][\inlinecode] surrounding your code,use [code=c] [\code].
The back slash '\" needs to be forward "/" inside the tag.
Last edited by Aia; May 22nd, 2007 at 7:39 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
May 22nd, 2007
0

Re: C Program help - Conway's Life - Stumped by some errors.

  1. if (b1[i][j] = '*')
This will always evaluate to TRUE, probably you ment "=="

There's also a problem with a "hanging" else.
Last edited by Aia; May 22nd, 2007 at 9:12 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need Help
Next Thread in C Forum Timeline: Linking Static Libraries





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC