I am new to C programs. I have tried to write the code for game of life. I started with just the first part. But its still not done. There are some errors. could you please point out where I am wrong?

#include<stdio.h>
#include<stdio.h>

#define NUM_ROWS 100
#define NUM_COLS 100

int game_of_life(int h[][NUM_COLS], int row, int col);

int main()
{
   int a[NUM_ROWS][NUM_COLS] = { {0, 0, 0, 0, 0, 0, 0},
                                 {0, 0, 1, 0, 1, 0, 0},
                                 {0, 1, 0, 1, 0, 1, 0},
                                 {0, 0, 1, 0, 1, 0, 0},
                                 {0, 1, 0, 1, 0, 1, 0}, 
                                 {0, 0, 1, 0, 1, 0, 0}, 
                                 {0, 0, 0, 0, 0, 0, 0} };
   int num_rows= 7;
   int num_cols = 7;
   int life_test;
   
   life_test = game_of_life(a, num_rows, num_cols);

printf ("%d", &life_test);

   return 0;
}

int game_of_life(int h[][NUM_COLS], int row, int col)

{
  int i, j;
  int a=0;
 
  for( i = 2; i < row; i++)
      for( j = 2; j < col; j++)
        
        if (h[i][j-1]=0)
        a++;
          else 
                if (h[i-1][j-1] =0)
                a++;
            else 
                if(h[i-1][j] = 0)
            a++;
               else 
                if (h[i-1][j+1]) = 0
               a++;

                 
                if (a=3)
                h[i][j]= 1;
                else 
                h[i][j]=0;
return h[i][j];
}

Thanks!

Recommended Answers

All 4 Replies

I am new to C programs. I have tried to write the code for game of life. I started with just the first part. But its still not done. There are some errors. could you please point out where I am wrong?

Here are the first places where you are wrong:
1) Not using Code tags
2) Expecting us to find your errors when you already know what they are
3) Not reading the Forum Rules and the Sticky Posts.

I haven't gone through either the problem or the solution. But I have a query. Why are you printing out the address of the returned variable. In printf on line 24, shouldn't you be printing out life_test instead of &life_test.

There are some errors. could you please point out where I am wrong?

The compiler will gladly point them out for you.

Not an error, but a logical error in your program which won't be reported to you by your compiler is that you use the assignment operator = instead of the equals operator == in nearly every if-statement in your code.
If you want to check for equality, then this is wrong:

// Wrong code for comparing two values
int a = 0;
if ( a = 5 )
{
  cout << "This code should execute only when variable 'a' has value 5.\n";
}

The above code doesn't work correctly because you're assigning a value to variable a .
This code is a correct solution for comparing two values:

// Correct code for comparing two values
int a = 5;
if ( a == 4 )
{
  cout << "This code only executes when variable 'a' holds the value 4.\n";
}

Hope this helps.

Yes.. all these helped a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.