C++/help me plz
Expert: vijayan - 11/30/2008
Questionhi
i have a problem whit the code below,in the rows that i have signed the code i want the program to search trough the mark[i][j] to find that if we have any array with i row and j column that contain '0' but in it's first search it just search for i=m/2 & j=n/2 that i initialaized in the bug function,but i want it to compare all of the arrays and it just compare '0' whit i=m/2 &j=n/2 can you help me about this problem?
#include <stdlib.h>
#include <time.h>
#include<string.h>
#include<stdio.h>
void bug(int x,int y);
int mark[10][10],m,n;
void main()
{
int j=0,i=0,w=0,x=0,y=0;
printf("Enter m&n:1<m<41,1<n<21\n");
scanf("%d%d",&m,&n);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
mark[i][j]=0;
m=m+1;
n=n+1;
i=0;
for(j=0;j<=n+1;j++)
mark[i][j]='#';
i=n+1;
for(j=0;j<=n+1;j++)
mark[i][j]='#';
j=0;
for(i=0;i<=m+1;i++)
mark[i][j]='#';
j=m+1;
for(i=0;i<=m+1;i++)
mark[i][j]='#';
do{
for(i=1;i<=m-1;i++)//*<-------------------my problem
for(j=1;j<=n-1;j++){//*<-------------------my problem
if(mark[i][j]==0)//*<-------------------my problem
w=0;
else
w=1;
}
printf("w=%d\n",w);
bug(x,y);
}while(w!=1);
}
void bug(int x,int y){
int count=0,stor1[50],stor2[50],a=0,b=0;
int k = 1 + int( 9.0 * ( rand() / ( RAND_MAX + 1.0 ) ) ) ;
x=m/2;
y=n/2;
mark[x][y]=1;
printf("k=%d\n",k);
if(k==1){
x=x-1;
y=y+1;
count++;
}
else if(k==2){
y=y+1;
count++;
}
else if(k==3){
x=x+1;
y=y+1;
count++;
}
else if(k==4){
x=x+1;
count++;
}
else if(k==5){
x=x+1;
y=y-1;
count++;
}
else if(k==6){
y=y-1;
count++;
}
else if(k==7){
x=x-1;
y=y-1;
count++;
}
else if(k==8){
x=x-1;
count++;
}
stor1[a]=x;
stor2[b]=y;
a++;
b++;
if(mark[x][y]=='#'){
x=stor1[a-2];
y=stor2[b-2];
}
mark[x][y]=1;
printf("x=%d,y=%d,count=%d\n",x,y,count);
}
thanx
Bita
Answeryou are causing a buffer overflow for the array mark
int mark[10][10],m,n;
void main()
{
int j=0,i=0,w=0,x=0,y=0;
printf("Enter m&n:1<m<41,1<n<21\n");
scanf("%d%d",&m,&n);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
mark[i][j]=0; // i, j could be greater than 9 here
you need mark to be at least
int mark[40][20],m,n;
m, n could have been corrupted because of the buffer overflow.
otherwise, i cannot see the problem
"i want the program to search trough the mark[i][j] to find that if we have any array with i row and j column that contain '0' but in it's first search it just search for i=m/2 & j=n/2 that i initialaized in the bug function,but i want it to compare all of the arrays and it just compare '0' whit i=m/2 &j=n/2"
i modified mark as suggested above and ran the program.
it seems to go through the entire array in the loop.
just to check out, i added a printf in the loop:
do{
for(i=1;i<=m-1;i++)//*<-------my problem
for(j=1;j<=n-1;j++){//*<-------my problem
{
printf( "*** %d %d\n", i, j ) ; // *** added ***
if(mark[i][j]==0)//*<-------my problem
w=0;
else
w=1;
}
}
printf("w=%d\n",w);
bug(x,y);
}while(w!=1);
and i tested it for some cominations of m and n. eg:
>./a.out
Enter m&n:1<m<41,1<n<21
4 6
*** 1 1
*** 1 2
*** 1 3
*** 1 4
*** 1 5
*** 1 6
*** 2 1
*** 2 2
*** 2 3
*** 2 4
*** 2 5
*** 2 6
*** 3 1
*** 3 2
*** 3 3
*** 3 4
*** 3 5
*** 3 6
*** 4 1
*** 4 2
*** 4 3
*** 4 4
*** 4 5
*** 4 6
w=1
k=1
x=1,y=4,count=1