Help me pls.can't find the error in this code

#include
using namespace std;
bool isSafe(int x,int y,int n,int arr[n][n])
{
if(x<n&&y<n&&(arr[x][y]==1))
{
return true;
}

return false;
}

bool ratinMaze(int x,int y,int n,int solArr[n][n],int arr[n][n])
{
if(x==n-1&&y==n-1)
{
solArr[x][y]==true;
return true;
}
if(isSafe(x,y,n,arr))
{
solArr[x][y]=1;

    if(ratinMaze(x+1,y,n,solArr,arr))
    return true;

    if(ratinMaze(x,y+1,n,solArr,arr))
    return true;

    solArr[x][y]==0   
    
    return false;
}
return false;

}
int main()
{
int n;
cin>>n;
int arr[n][n],solArr[n][n];
cout<<“enter the elemets”<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
solArr[i][j]=0;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
cout<<endl;
}
cout<<ratinMaze(0,0,n,solArr,arr);
return 0;
}

Will you please provide problem link …

i tried to solve this problem without dynamic memory allocation because I have not studied that topic yet

First of all you should don’t; copy and paste code directly you should enclose your code in backticks.LIke this ie ``` xyz ``

my code int a ....

The second thing you cannot pass 2d array in a function like this. You need to first clear the basics, don’t worry I will make you understand that.
The third thing is that you need to provide the problem link also .

To pass 2d array in the function you can refer to Link:How to pass a 2D array as a parameter in C? - GeeksforGeeks

Link :Passing a 2D array to a C++ function - Stack Overflow
or make initialize it globally.

Corrected Code :

void helper(int maze[][20],int n,int **ans ,int i,int j)
{

    if(i==n-1 && j==n-1)
    {
        ans[i][j]=1;
        
    for(int k=0;k<n;k++)// printing one possible solution
    {
        for(int p=0;p<n;p++)
        {
            cout<<ans[k][p]<<" ";
        }
    }
    cout<<endl;
        
    ans[i][j]=0;
        return ;
    }
    
    
    
    if(i<0 || j>=n||j<0 ||i>=n || maze[i][j]==0  || ans[i][j]==1)
    {
        return;
    }
    
    ans[i][j]=1;
    
    helper( maze, n,ans , i+1, j);
    helper( maze, n,ans , i-1, j);   
    helper( maze, n,ans , i, j+1);
    helper( maze, n,ans , i, j-1);
    
    ans[i][j]=0;
    
    return ;
}

void ratInAMaze(int maze[][20], int n)
{
int **ans =new int *[n];
for(int i=0;i<n;i++)
{
    ans[i]=new int [n];
    for(int j=0;j<n;j++)
    {
        ans[i][j]=0;
    }
}
    
  helper( maze, n,ans , 0, 0);  
    
}

#include<iostream>
using namespace std;


int main(){

  int n; 
  cin >> n ;
  int maze[20][20];
  for(int i = 0; i < n ;i++){
	for(int j = 0; j < n; j++){
        	cin >> maze[i][j];
        }		
  }
  ratInAMaze(maze, n);
}
1 Like

If you have a problem in DMA then initialize all the 2d arrays globally.
Like for example

#include<iostream>
using namespace std;

    int maze[1000][1000];
    int ans [1000][1000];

fun(int A ,int B)
{
maze[A][B]= 2;
ans[A][B]=3;
do whatever you want to do with 2d array anywhere it would be changed in the global 2d array .

}

int main{

for(int i=0;i<1000;i++) // this will put zero in both global 2d array .
{
for(int j=0;j<1000;j++)
{
maze[i][j]=0;
ans[i][j]=0;
}
}


fun(0,3);

}
1 Like

@ayush29azad can you pls specify what i did wrong in passing 2d array.
i read that link and I am not able to figure it out. i am not able to understand why It is giving errors even when I have passed the dimensions of arrays in each function

#bro first thing initialise all 2d array globally . without dma you cannot write it inside the function directly.

#Second check that in 2 places you have written == in place of = you need to understand that also == is used in if condition and = is assignment operator .

#third thing now compare the corrected code below with your previous code line by line.
and try to visualize the difference that’s the way you will learn and become a good coder.
Here is your corrected code

#include <iostream>
using namespace std;
#include<bits/stdc++.h>

int arr[1000][1000];
int solArr[1000][1000];
bool isSafe(int x,int y,int n)
{
if(x<n &&y<n && (arr[x][y]==1) )
{
return true;
}

return false;
}

bool ratinMaze(int x,int y,int n)
{
if(x==n-1 && y==n-1)
{
solArr[x][y]=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{

cout<<solArr[i][j]<<" "; // to print one solution 
}
cout<<endl;
}
return true;

    
}


if(isSafe(x,y,n))
{
solArr[x][y]=1;

    if(ratinMaze(x+1,y,n))
    return true;

    if(ratinMaze(x,y+1,n))
    return true;

    solArr[x][y]=0 ;  
    
    return false;
}


return false;
}



int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];

solArr[i][j]=0;
}


}


cout<<ratinMaze(0,0,n)<<endl;


return 0;
}
1 Like

thanks now it is clear :grinning: :grinning:

1 Like