1 test case failed

code works completely fine in case of NN array like 33, but showing some 0 values in case of NM arrays like 34 or 4*3 . Can you tell what’s wrong in this code and what changes it need.
CODE:

#include
using namespace std;
int main(){

int a[100][100];
int m,n;
cin>>m>>n;
int i,j;

for( i=0; i<m; i++){
    for(j=0; j<n; j++){
        cin>>a[i][j];
        
    }
}
int k;
for(i=0;i<=m+n-2;i++){
    for(j=0; j<m; j++){

        for(k=0; k<n; k++){
            if(j+k==i){
                if(i%2==0){

                    cout<<a[k][j]<<" ";
                }
                else{
                    cout<<a[j][k]<<" ";
                }
        
            }
        }

    }
}


return 0;

}

hi @tanishqbatra0511
go through this -->

hi @tanishqbatra0511
is it clear now??

the solution given is clear to me, but still not able to find what’s wrong in my code.

@tanishqbatra0511
ur code will work fine only with N x N matrices… for N X M it wont work as it tries to access out of bounds indices at some places…

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

yes , its clear now, thanx.

1 Like