Thread: Help with a C program assignment. Please?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    Help with a C program assignment. Please?

    Ok I am currently working on an assignment that creates a 10x15 two-dimensional array for John Conway's "Game of Life". I need to get user input for the "occupied" slots in the array and then print the grid. I have this done. I then need to generate new generations of the the grid based on the specifications in the game. This requires two functions, one deciding the number of "neighbors" around a coordinate and one deciding the the new generation and putting it into a new array.

    I am having problems with my
    Code:
    generate()
    function. I know I need a temporary array to be able to write over my old array once I have my new generation. I am not sure how I can take the new generated array and be able to print it using my
    Code:
    printgrid()
    function.

    Here is my code so far. Any suggestions for my problem? I am very confused and lost on how to achieve thise.

    Code:
    #include <stdio.h>
    
    #define SIZE 10
    #define SIZE2 15
    #define max(A,B) ( (A) > (B) ? (A):(B))
    #define min(A,B) ( (A) < (B) ? (A):(B))
    
    void initconfig(int [][SIZE2], int, int, int);
    int occ(int [][SIZE2], int, int);
    int* generate(int [][SIZE2], int, int);
    void printgrid(int [][SIZE2], int, int );
    
    int main ()
    {
    
      int coordArray[SIZE][SIZE2] = {0}, x = 0, y = 0,
          generations = 0;
    
      initconfig(coordArray, x, y, generations);
    
      printf("\n\n\nInitial State\n\n");
      printgrid(coordArray, SIZE, SIZE2);
    
      printgrid(generate(coordArray, SIZE, SIZE2), SIZE, SIZE2);
    
      return 0;
    
    }
    
    void initconfig(int coordArray[][SIZE2], int x, int y, int generations)
    {
       printf("\nEnter the number of generations: ");
       scanf("%d", &generations);
    
       printf("\nEnter coordinates: ");
       scanf("%d%d", &x, &y);
    
      while (x != -1){
    
        if(x<1 || x>10 || y<1 || y>15)
          printf("\nInvalid input.");
         else
          coordArray[x-1][y-1] = 1;
    
       printf("\nEnter coordinates: ");
       scanf("%d%d", &x, &y);
    }
    }
    
    int* generate(int coordArray[][SIZE2], int row, int col)
    {
      int tempArray[SIZE][SIZE2] = {0}, i=0, j=0;
    
    
      for(i=0; i <= row - 1; i++){
        for(j=0; j <= col - 1; j++){
    
          if(coordArray[i][j] == 1){
            if(occ(coordArray, i, j) == 2)
              tempArray[i][j] = 1;
            if(occ(coordArray, i, j) == 3)
              tempArray[i][j] = 1;
            else
              tempArray[i][j] = 0;}
    
          if(coordArray[i][j] == 0){
            if(occ(coordArray, i, j) == 3)
              tempArray[i][j] = 1;
            else
              tempArray[i][j] = 0;}
        }}
    
      for(i=0; i<= row - 1; i++){
        for(j=0; j <= col - 1; j++){
          coordArray[i][j] = tempArray[i][j];}}
    
      return coordArray;
    }
    
    
    int occ( int coordArray[][SIZE2], int x, int y )
    {
      int neighbourCount = 0, i, j;
            for(i = max( 0, x - 1 ) ; i < min( SIZE2, x + 1 ); i++ )
            {
                    for(j = max( 0, y - 1 ) ; j < min( SIZE, y + 1 ); j++ )
                    {
                            if ( ( x == i ) & ( y == j ) )
                            {
                                    continue;
                            }
                            if ( coordArray[ i ][ j ] == 1 )
                            {
                                    neighbourCount++ ;
                            }
                    }
            }
            return neighbourCount;
    }
    
    void printgrid(int coordArray[][SIZE2], int row, int col)
    {
      int i, j;
    
      printf("\n");
    
      for(i=0; i <= row - 1; i++){
        for(j=0; j <= col - 1; j++){
          if(coordArray[i][j] == 1){
            printf("*");}
            else
              printf("-");}
    
          printf("\n\n");
      }
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    for(i=0; i <= row - 1; i++){
    could be written much more simply as
    Code:
    for(i = 0; i < row; i ++) {
    Code:
    int* generate(int coordArray[][SIZE2], int row, int col)
    {
        /* ... */
      return coordArray;
    }
    You're returning an int** as int*.

    And you should try to indent your code, too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I believe your code should look something like this...

    Code:
    void generate(int coordArray[][SIZE2], int row, int col)
    {
      int tempArray[SIZE][SIZE2] = {0}, i=0, j=0;
    
    
      for(i=0; i < row ; i++){
        for(j=0; j < col; j++){
    
          if(coordArray[i][j] == 1){
            if(occ(coordArray, i, j) == 2)
              tempArray[i][j] = 1;
            if(occ(coordArray, i, j) == 3)
              tempArray[i][j] = 1;
            else
              tempArray[i][j] = 0;}
    
          if(coordArray[i][j] == 0){
            if(occ(coordArray, i, j) == 3)
              tempArray[i][j] = 1;
            else
              tempArray[i][j] = 0;}
        }}
    
      for(i=0; i< row; i++){
        for(j=0; j < col; j++){
          coordArray[i][j] = tempArray[i][j];}}
    
      //return coordArray;}
    
    
    int main ()
    {
    
      int coordArray[SIZE][SIZE2] = {0}, x = 0, y = 0,
          generations = 0;
    
      initconfig(coordArray, x, y, generations);
    
      printf("\n\n\nInitial State\n\n");
      printgrid(coordArray, SIZE, SIZE2);
    generate(coordArray, SIZE, SIZE2);
     printgrid(coordArray, SIZE, SIZE2);
    
      return 0;
    
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Programming Assignment
    By JHaney in forum C++ Programming
    Replies: 18
    Last Post: 11-08-2005, 03:45 AM
  2. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM