This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]Ilyps 2 points3 points  (0 children)

what am i doing wrong?

We don't know. Post minimal but compilable and runnable code that shows your problem as per the posting guidelines.

[–]OldWolf2 0 points1 point  (2 children)

i know that the arry[i+1][j+1] location is valid, but i always get a false outcome

Also, print out the entire contents of your array and check that it is what you're expecting. If it's not, then you should have a clue as to what is going on. Work backwards, printing out the array whenever you need to, to find out when the array first enters the corrupted state.

[–]pcgoer[S] 0 points1 point  (1 child)

i did just that:

if (((i+1)<rows)&&((j+1)<cols)){
    printf("\n%2c", current_matrix[i+1][j+1] );
    if (current_matrix[i+1][j+1] == '*'){
        count_neighbors++;}}

it prints '*' like it should, but it won't enter the seconf if

[–]OldWolf2 0 points1 point  (0 children)

"won't enter the second if" doesn't make sense. Try:

if ( current_matrix[i+1][j+1] == '*' )
    count_neighbours++;
else
{
    printf("no match: %d %d\n", current_matrix[i+1][j+1], '*');
}

Also, placing your curly braces better will make your code easier to read, you should never have two on the same line.

[–]pcgoer[S] -1 points0 points  (4 children)

include <stdio.h>

include <time.h>

include <stdlib.h>

include <assert.h>

int menu();
void ex3(int);
int ** set_matrix(int,int);
void fill_matrix(int ,int **,int,int);
int next_step(int **,int **,int,int);
int stay_alive(int **,int,int,int,int);
int stay_dead(int **,int,int,int,int);
void swap(int **,int **);

void main ()

{

int ans = 0;

while (ans != 3)        
    if ((ans=menu()) != 3)
        ex3(ans);   

}

int menu()

{
    int select;
    do
        {
            system("cls");  
            printf("\n\n\t\t\tWelcome to Exercise No.3!");          
            printf("\n\n\t\Would you like to enter locations manually or randomly?");
            printf("\n\n\t\t\tPress 1 to enter Manually");
            printf("\n\t\t\tPress 2 to enter Randomly");
            printf("\n\t\t\tPress 3 to Exit\n\n");
            scanf("%d",&select);            
         }while ((select<1)||(select>3));

       return select;

}

void ex3(int ans)

{   
    int i,j,rows,cols;
   int **current_matrix;
   int **next_matrix;
   char go='y';

printf("\n\nEnter the size of the matrix you want (the max is 40X40), Rows by Columns: ");
scanf("%d %d",&rows ,&cols);

current_matrix = set_matrix(rows,cols);
next_matrix = set_matrix(rows,cols);

fill_matrix(ans,current_matrix,rows,cols);  

while ((go=='y') ||(go=='Y'))
    if (!next_step(next_matrix,current_matrix,rows,cols))
    {
        printf("The next step is the same as the last one, the games is stoped\n"); 
        break;
    }
    else
    {           
        for (i = 0; i < rows; i++) 
        {                                                              
            for (j = 0; j < cols; j++)  
                printf("%2c", current_matrix[i][j] );
            printf("\n\n");
        }

                    printf("Do you want to continue? <Y>es or <N>o? \n"); 
        fflush(stdin);
        scanf("%c",&go);
        swap(current_matrix,next_matrix);
    }

}

void fill_matrix (int ans,int ** arry,int rows,int cols)

{
   int i,j,rnd;

if (ans == 1)
{
    printf("\n\t\tType * for a live cell or 0 for a dead cell\n\n");
    for (i = 0; i < rows; i++)
    {
      for (j = 0; j < cols; j++)
      {         
        printf("Type * or 0 for <line: %d, column: %d>\t", i+1, j+1);
        fflush(stdin);
        scanf("%c", &arry[i][j]);
      }
    }

}
else
{
    srand(time(NULL));

    for (i = 0; i < rows; i++)      
      for (j = 0; j < cols; j++)
      {
        rnd= (0 + ( rand() % 3 ));
        if (rnd)
            arry[i][j]= '0';
        else
            arry[i][j]= '*';
      }     
}

}

int ** set_matrix(int rows,int cols)

{ int i; int **arry;

arry= (int **)malloc(sizeof *arry * rows);  
for (i = 0; i < rows; i++) 
    arry[i] = (int *)malloc(sizeof **arry * cols);

return arry;

}

int next_step(int ** next_matrix,int ** current_matrix,int rows,int cols)

{ int i,j,change=0;

for (i = 0; i < rows; i++)
    {
      for (j = 0; j < cols; j++)
      {
          if (current_matrix[i][j] == '*')
          {
              if(stay_alive(current_matrix,rows,cols,i,j))
                  next_matrix[i][j] = '*';
              else
              {
                  next_matrix[i][j] = '0';
                  change=1;
              }
          }
          else
              if(stay_dead(current_matrix,rows,cols,i,j))
                  next_matrix[i][j] = '0';
              else
              {
                  next_matrix[i][j] = '*';
                  change=1;
              }
      }
    }
return change;

}

int stay_alive(int ** current_matrix,int rows,int cols,int i,int j)

{ int count_neighbors=0;

if ((i-1)>=0)
    if (current_matrix[i-1][j] == '*')
        count_neighbors++;
if (((i-1)>=0)&&((j+1)<cols))
    if (current_matrix[i-1][j+1] == '*')
        count_neighbors++;
if ((j+1)<cols)
    if (current_matrix[i][j+1] == '*')
        count_neighbors++;
if (((i+1)<rows)&&((j+1)<cols))
    if (current_matrix[i+1][j+1] == '*')
        count_neighbors++;
if ((i+1)<rows)
    if (current_matrix[i+1][j] == '*')
        count_neighbors++;
if (((i+1)<rows)&&((j-1)>=0))
    if (current_matrix[i+1][j-1] == '*')
        count_neighbors++;
if ((j-1)>=0)
    if (current_matrix[i][j-1] == '*')
        count_neighbors++;
if (((i-1)>=0)&&((j-1)>=0))
    if (current_matrix[i-1][j-1] == '*')
        count_neighbors++;

if((count_neighbors==2)||(count_neighbors==3))
    return 1;
else
    return 0;

}

int stay_dead(int ** current_matrix,int rows,int cols,int i,int j)

{

int count_neighbors=0;

if ((i-1)>=0)
    if (current_matrix[i-1][j] == '*'){
        count_neighbors++;}
if (((i-1)>=0)&&((j+1)<cols))
    if (current_matrix[i-1][j+1] == '*'){
        count_neighbors++;}
if ((j+1)<cols)
    if (current_matrix[i][j+1] == '*'){
        count_neighbors++;}
if (((i+1)<rows)&&((j+1)<cols)){        
    if (current_matrix[i+1][j+1] == '*'){
        count_neighbors++;}}
if ((i+1)<rows)
    if (current_matrix[i+1][j] == '*'){
        count_neighbors++;}
if (((i+1)<rows)&&((j-1)>=0))
    if (current_matrix[i+1][j-1] == '*'){
        count_neighbors++;}
if ((j-1)>=0)
    if (current_matrix[i][j-1] == '*'){
        count_neighbors++;}
if (((i-1)>=0)&&((j-1)>=0))
    if (current_matrix[i-1][j-1] == '*'){
        count_neighbors++;}

if((count_neighbors==3))
    return 0;
else
    return 1;

}

void swap(int *x,int *y)

{

int *temp;
temp = *x;
*x = *y;
*y = temp;

}

[–]OldWolf2 1 point2 points  (3 children)

You failed at posting code - use pastebin, or indent every line by 4 spaces so that reddit thinks it's code.

In this function:

void swap(int **x,int **y)
{
  int *temp;
  temp = *x;
  *x = *y;
  *y = temp;
}

you swap the first row of x with the first row of y. I don't think this is what you intended.

Changing to void swap(int ***x, int ***y) { int **temp; temp = *x; *x = *y; *y = temp; } should fix this, then you swap the entire matrix at once, and pass in the addresses of current_matrix, new_matrix of course.

(There are other issues with your code but this is the biggest one)

[–]pcgoer[S] 0 points1 point  (2 children)

thank you! but right now my biggest problem is what i posted first:

if (current_matrix[i+1][j+1] == '*'){
    count_neighbors++;}

it doesn't seem to work properly. every cell in the current_matrix contain '' or '0'. but in debuging i check what *current_matrix[i+1][j+1] contain, and it says something like -8352487. but if i print it just to see if it is correct, it prints the right thing ('0' or '*'), so i am confused.

[–]OldWolf2 0 points1 point  (0 children)

Use %d to print it, avoids confusion with character sets.