| | |
C Program help - Conway's Life - Stumped by some errors.
Thread Solved |
•
•
Join Date: May 2007
Posts: 4
Reputation:
Solved Threads: 0
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:
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
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:
C Syntax (Toggle Plain Text) - #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() {
- display_fctn();
- printf("Would you like to play? (y/n)");
- c = getch();
- while (c = 'y') {
- new_board();
- copy_back();
- display_fctn(b1);
- 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;
- 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 */
- }
- }
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!
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
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.
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.
C Syntax (Toggle Plain Text)
char b1[20][21] = { {"**.................."}, {"**.................."}, {"...................."}, {"...................."}, {"...................."}, {"...................."}, {"...................."}, {"........***........."}, {"........*..........."}, {".........*.........."}, {"...................."}, {"...................."}, {"...................."}, {"...................."}, {"...................."}, {"...................."}, {"...................."}, {"...................."}, {"...................."}, {"...................."} };
Also look at your function declarations.
*Voted best profile in the world*
> 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.
> 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.
•
•
Join Date: May 2007
Posts: 4
Reputation:
Solved Threads: 0
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!
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.
•
•
Join Date: May 2007
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
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
C Syntax (Toggle Plain Text)
if (b1[i][j] = '*')
There's also a problem with a "hanging" else.
Last edited by Aia; May 22nd, 2007 at 9:12 pm.
![]() |
Other Threads in the C Forum
- Previous Thread: Need Help
- Next Thread: Linking Static Libraries
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






