Like Tree1Likes

Conway's Life - C Program - Errors have me stumped - Please help

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

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    12

    Question Conway's Life - C Program - Errors have me stumped - Please help

    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
    Last edited by Salem; 05-21-2007 at 10:36 AM. Reason: Colour abuse removed

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    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.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Sarah87 View Post

    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?
    Last edited by Adak; 05-21-2007 at 10:43 AM.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I'll try something quickly.
    Last edited by maxorator; 05-21-2007 at 10:18 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    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 support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    12
    Quote Originally Posted by Adak View Post
    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

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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?"

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Oops, double post.
    Last edited by maxorator; 05-22-2007 at 01:28 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    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

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    First add a
    Code:
    return 0;
    to your main() function.

    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.

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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 */
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 10:38 PM
  2. Errors with program to replace string with another
    By Dan17 in forum C Programming
    Replies: 3
    Last Post: 09-14-2006, 10:15 AM
  3. Need help to solve program errors
    By pattop in forum C++ Programming
    Replies: 6
    Last Post: 05-28-2006, 01:57 AM
  4. C++ Game of Life Program
    By rayrayj52 in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 03:58 PM
  5. Simple Program wont execute after compiles w. no errors
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 02-03-2002, 03:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21