Im not getting the correct ans is this the same way sir taught na?

int main(){

//inputs
int n,m,sum=0,i,j;
cin>>n>>m;
int a[n][m];
for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        cin>>a[i][j];
    }
}   

//calculating prefix matrix
for(j=0;j<m;j++){
    for(i=1;i<m;i++){
        a[i][j] += a[i-1][j];
    }
}

for(i=0;i<n;i++){
    for(j=1;j<m;j++){
        a[i][j] += a[i][j-1];
    }
}

for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        cout<<a[i][j]<<" ";
    }cout<<endl;
}


// finding all sum tlx = top Left X .....similarly
int tlx,tly,brx,bry;

for(tlx=0;tlx<n;tlx++){
    for(tly=0;tly<m;tly++){
        for(brx=tlx;brx<n;brx++){
            for(bry=tly;bry<m;bry++){
                sum += a[brx][bry] - a[brx][tly-1] - a[tlx-1][bry] + a[tlx-1][tly-1];
            }
        }
    }
}

cout<<sum;

}

Hello @talhashamim001 please share your code by saving it on ide.codingblocks.com .

@talhashamim001 this is the correct code :


check this code and tell me if you find it helpful.
if you have any doubt you can ask here:
Happy Learning!!

oh i got my mistake , I was not checking if the index are insite the array or not before adding or subtracting

thank you …

@talhashamim001 yes.
if your doubt is cleared you can mark this as resolved.
Happy Learning!!

sir Ive raised a doubt regarding a question named FINQUEST please can you check

@talhashamim001 there i have asked you to explain your appraoch?
please exlain there only and mark this as resolved.
Happy Learning!!

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.