1. ## Something is wrong.

Something is wrong with my programme but I'm stangling to find what it is. I've spent two whole days.

Code:
```int N,M,i,j,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill,a,b,position_i=0,position_j=0;
char **board;
char board_interaction;
printf("Give the two dimensions of the board.\n");
do
{
do
{
scanf("%d",&N);
}
while(N<4);
}
while(N>15);
do
{
do
{
scanf("%d",&M);
}
while(M<5);
}
while(M>15);
do
{
printf("Give the number of the difficulty of the game.\n1. Easy\n2. Medium\n3. Hard\n");
scanf("%d",&diff);
}
while((diff!=1)&&(diff!=2)&&(diff!=3));
if(diff==1)
{
number_of_enemies=((N*M)*5)/100;
number_of_obstacles=((N*M)*5)/100;
}
else if(diff==2)
{
number_of_enemies=((N*M)*10)/100;
number_of_obstacles=((N*M)*5)/100;
}
else
{
number_of_enemies=((N*M)*10)/100;
number_of_obstacles=((N*M)*10)/100;
}
enemies_to_kill=number_of_enemies;
board=(char**)malloc(N*sizeof(char*));
for(i=0;i<N;i++)
{
board=(char*)malloc(M*sizeof(char));
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
board[i][j]='.';
}
}
board[0][0]='\$';
while((number_of_obstacles!=0)&&(board[rand()%N][rand()%M]!='\$'))
{
board[rand()%N][rand()%M]='#';
number_of_obstacles=number_of_obstacles-1;
}
do
{
a=rand()%N;
b=rand()%M;
board[a][b]='1';
}
while((board[rand()%N][rand()%M]!='\$')&&(board[rand()%N][rand()%M]!='#'));
while((number_of_enemies!=0)&&(board[rand()%N][rand()%M]!='\$')&&(board[rand()%N][rand()%M]!='#')&&((a!=N)||(b!=M)))
{
if(board[a+1][b]=='.')
{
board[a+1][b]='2';
number_of_enemies=number_of_enemies-1;
}
else if(board[a-1][b]=='.')
{
board[a-1][b]='2';
number_of_enemies=number_of_enemies-1;
}
if(board[a][b+1]=='.')
{
board[a][b+1]='3';
number_of_enemies=number_of_enemies-1;
}
else if(board[a][b-1]=='.')
{
board[a][b-1]='3';
number_of_enemies=number_of_enemies-1;
}
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
printf(" %c ",board[i][i]);
}
}
printf("\nLevel money spent:%d \n",money);
printf("Game money spent:%d \n",total_money);
scanf("%c",board_interaction);
for(i=0;i<M;i++)
{
free(board[i]);
}
free(board);
return 0;```

2. Was it really worth trimming off a couple of lines that prevent people from simply copy/pasting your code to try it?

Fix all these warnings would be the first thing.
Code:
```\$ gcc -Wall -Wextra foo.c
foo.c: In function ‘main’:
foo.c:51:14: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
board=(char*)malloc(M*sizeof(char));
^
foo.c:106:11: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%c",board_interaction);
^
foo.c:4:115: warning: unused variable ‘position_j’ [-Wunused-variable]
int N,M,i,j,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill,a,b,position_i=0,position_j=0;
^
foo.c:4:102: warning: unused variable ‘position_i’ [-Wunused-variable]
int N,M,i,j,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill,a,b,position_i=0,position_j=0;
^
foo.c:4:82: warning: variable ‘enemies_to_kill’ set but not used [-Wunused-but-set-variable]
int N,M,i,j,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill,a,b,position_i=0,position_j=0;
^
foo.c:106:5: warning: ‘board_interaction’ is used uninitialized in this function [-Wuninitialized]
scanf("%c",board_interaction);
^```

Next would be breaking your 100+ line main down into some functions.

3. That is the problem. I cannot fix them and find a solution by myself. That is why I posted my code here.

4. Well you ignored my previous advice about just using statically sized arrays while you get the bulk of the code working.

Instead, you went for broke, tried to do it all in one big hit, and missed.

> board=(char*)malloc(M*sizeof(char));
1. There's no need to cast the return result of malloc in a C program.
2. Study how you call free, which somehow you've managed to do successfully.

> scanf("%c",board_interaction);
Don't forget the &

5. It is not that I left out your advice but the moving of the \$ is not showing.

6. Restart then, and this time develop incrementally. Write a small piece of code, compile and test, then debug and fix as needed. Write some more code, compile and test, then debug and fix as needed. When a function starts getting a little long (say longer than 50 to 70 lines), break it up into smaller functions that each does one thing and does it well. This way, you will have a much easier time finding out your mistakes than trying to hunt for them after writing a large chunk of code. You will even be able to write automated unit tests to help you be more certain that your program is working as expected.

7. I uploaded the program because the terminal said Segmentation fault (core dumped). That is why I cannot find any problems.

8. And I've already shown you two places in your code where trouble like segmentation faults will appear.

Have you fixed them?

9. Both of them. Now without the warnings but still Segmentation fault.

10. Now without the warnings but still Segmentation fault.
Post your modified code. And note without the complete program we will probably be guessing about where the problem lies because we can't see the problem.

Did you run the program with your debugger? The debugger should tell you exactly where it detects the problem and you should also be able to view the contents of your variables at the time of the crash.

11. Code:
```int N,M,i,j,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill,a,b;
char **board;
char board_interaction;
printf("Give the two dimensions of the board.\n");
do
{
do
{
scanf("%d",&N);
}
while(N<4);
}
while(N>15);
do
{
do
{
scanf("%d",&M);
}
while(M<5);
}
while(M>15);
do
{
printf("Give the number of the difficulty of the game.\n1. Easy\n2. Medium\n3. Hard\n");
scanf("%d",&diff);
}
while((diff!=1)&&(diff!=2)&&(diff!=3));
if(diff==1)
{
number_of_enemies=((N*M)*5)/100;
number_of_obstacles=((N*M)*5)/100;
}
else if(diff==2)
{
number_of_enemies=((N*M)*10)/100;
number_of_obstacles=((N*M)*5)/100;
}
else if(diff==3)
{
number_of_enemies=((N*M)*10)/100;
number_of_obstacles=((N*M)*10)/100;
}
enemies_to_kill=number_of_enemies;
board=(char**)malloc(N*sizeof(char*));
for(i=0;i<N;i++)
{
board=malloc(M*sizeof(char));
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
board[i][j]='.';
}
}
board[0][0]='\$';
while((number_of_obstacles!=0)&&(board[rand()%N][rand()%M]!='\$'))
{
board[rand()%N][rand()%M]='#';
number_of_obstacles=number_of_obstacles-1;
}
do
{
a=rand()%N;
b=rand()%M;
board[a][b]='1';
}
while((board[rand()%N][rand()%M]!='\$')&&(board[rand()%N][rand()%M]!='#'));
while((number_of_enemies!=0)&&(board[rand()%N][rand()%M]!='\$')&&(board[rand()%N][rand()%M]!='#')&&((a!=N)||(b!=M)))
{
if((board[a+1][b]=='.')&&((a!=N)||(b!=M)))
{
board[a+1][b]='2';
number_of_enemies=number_of_enemies-1;
}
else if((board[a-1][b]=='.')&&((a!=N)||(b!=M)))
{
board[a-1][b]='2';
number_of_enemies=number_of_enemies-1;
}
if((board[a][b+1]=='.')&&((a!=N)||(b!=M)))
{
board[a][b+1]='3';
number_of_enemies=number_of_enemies-1;
}
else if((board[a][b-1]=='.')&&((a!=N)||(b!=M)))
{
board[a][b-1]='3';
number_of_enemies=number_of_enemies-1;
}
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
printf(" %c ",board[i][i]);
}
}
printf("\nLevel money spent:%d \n",money);
printf("Game money spent:%d \n",total_money);
scanf("%c",&board_interaction);
for(i=0;i<M;i++)
{
free(board[i]);
}
free(board);```
I am working on the debugger now.

12. So where is the complete code?

13. Code:
```#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int N,M,i,j,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill,a,b;
char **board;
char board_interaction;
printf("Give the two dimensions of the board.\n");
do
{
do
{
scanf("%d",&N);
}
while(N<4);
}
while(N>15);
do
{
do
{
scanf("%d",&M);
}
while(M<5);
}
while(M>15);
do
{
printf("Give the number of the difficulty of the game.\n1. Easy\n2. Medium\n3. Hard\n");
scanf("%d",&diff);
}
while((diff!=1)&&(diff!=2)&&(diff!=3));
if(diff==1)
{
number_of_enemies=((N*M)*5)/100;
number_of_obstacles=((N*M)*5)/100;
}
else if(diff==2)
{
number_of_enemies=((N*M)*10)/100;
number_of_obstacles=((N*M)*5)/100;
}
else if(diff==3)
{
number_of_enemies=((N*M)*10)/100;
number_of_obstacles=((N*M)*10)/100;
}
enemies_to_kill=number_of_enemies;
board=(char**)malloc(N*sizeof(char*));
for(i=0;i<N;i++)
{
board=malloc(M*sizeof(char));
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
board[i][j]='.';
}
}
board[0][0]='\$';
while((number_of_obstacles!=0)&&(board[rand()%N][rand()%M]!='\$'))
{
board[rand()%N][rand()%M]='#';
number_of_obstacles=number_of_obstacles-1;
}
do
{
a=rand()%N;
b=rand()%M;
board[a][b]='1';
}
while((board[rand()%N][rand()%M]!='\$')&&(board[rand()%N][rand()%M]!='#'));
while((number_of_enemies!=0)&&(board[rand()%N][rand()%M]!='\$')&&(board[rand()%N][rand()%M]!='#')&&((a!=N)||(b!=M)))
{
if((board[a+1][b]=='.')&&((a!=N)||(b!=M)))
{
board[a+1][b]='2';
number_of_enemies=number_of_enemies-1;
}
else if((board[a-1][b]=='.')&&((a!=N)||(b!=M)))
{
board[a-1][b]='2';
number_of_enemies=number_of_enemies-1;
}
if((board[a][b+1]=='.')&&((a!=N)||(b!=M)))
{
board[a][b+1]='3';
number_of_enemies=number_of_enemies-1;
}
else if((board[a][b-1]=='.')&&((a!=N)||(b!=M)))
{
board[a][b-1]='3';
number_of_enemies=number_of_enemies-1;
}
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
printf(" %c ",board[i][i]);
}
}
printf("\nLevel money spent:%d \n",money);
printf("Game money spent:%d \n",total_money);
scanf("%c",&board_interaction);
for(i=0;i<M;i++)
{
free(board[i]);
}
free(board);
return 0;
}```
The complete program I have a problem with is here.

14. > board=malloc(M*sizeof(char));
You didn't fix this.

board[i]=malloc(M*sizeof(char));

15. That solved the Sengmetation problem but now the first row is all filled with \$, the second row is filled with 2, the third with 3, the forth with 1 and the 7th,9th,10th,12th row with 1. Also, I give it different dimensions other than 15 and 15 but it continues to ask me the difficulty without ending.