« first day (44 days earlier)      last day (3033 days later) » 

21:59
@nwp
int findpath(int a,int m,int n)
{
if(m==0 || n==0)
return 1;

return findpath(a,m-1,n)+findpath(a,m,n-1);
}
if a is a 2d array
then why is it wrong
user406009
@jayantsingh What's your question about this?
nwp
nwp
looks like a is an int
user406009
It should work. (Might not give you anything useful, but whatever.)
actually i am sendding a 2 d array
and giving error
user406009
@jayantsingh Can you post your entire code snippet?
22:01
int findpath(int a[][],int m,int n)
not workin
user406009
To gist.github.com or something.
user406009
@jayantsingh Unfortunately, that's not how you do two dimensional arrays in C++.
user406009
Or at least that's not how you pass them as arguments.
but for single dimension it works
int findpath(int a[],int m,int n)
user406009
Yep. The problem with C (and C++) is that you can only leave off the last dimension of an array.
user406009
int a[10][] would work. But of course that limits you to that size.
user406009
The "real" solution is to use templates.
user406009
But those suck. I would recommend just using a 1d array and manually computing the indices.
user406009
Or std::vector<std::vector<int>>
nwp
nwp
you should also consider std::array
user406009
22:03
Also, that's not correct C++.
user406009
You can't do dynamically sized arrays like that in C++.
user406009
Let's switch to std::vector instead.
user406009
That will make things more clear.
ok sure
user406009
user406009
22:08
The other solution is to just have one std::vector of size n*m.
user406009
And then manually index it.
user406009
Sometimes that is simpler than messing around with std::vector<std::vector<int>>
in func why & a?
only a
?
user406009
I pass the vector by reference because it's a large structure to pass along.
user406009
You should look into the C++ reference feature.
user406009
22:11
The other int n and int m arguments are small and cheap to copy, so there is no point.
using array makes it pass by reference auto
?
user406009
Also, it would get annoying quite fast.
user406009
@jayantsingh Yes. Arrays have a lot of special casing around them.
user406009
They are annoying.
ok not vector
user406009
22:12
Yeah. In fact there is a new type std::array simply to no longer have to deal with all the B.S surrounding arrays.
user406009
user406009
Oops wrong link
user406009
^ That's std::array.
user406009
One of these days I will stop messing up when copying and pasting.
22:17
with this bydefault its call by value
or call by reference
?
user406009
By value. Like everything else.
ok
@lalaland
thanku very much
user406009
No problem. It's why this room exists.
true :)
23:01
What is wrong with this DP matrix sum. It keeps giving the wrong answer:

int solve(int i, int j) {
    if (i < 0 || j < 0) return 0;
    if (i == 0) return mat[0][j-1] + solve(0, j-1);
    if (j == 0) return mat[i-1][0] + solve(i-1, 0);
    return mat[i-1][j-1] + solve(i-1, j) + solve(i, j-1);
}
nwp
nwp
what is a DP matrix sum?
also you probably want to pass the matrix by reference and not use a global variable
@nwp "Dynamic programming"
nwp
nwp
ok, so the goal is to get the sum of all the elements of mat?
where mat is a 2D-matrix with size i*j?
@nwp Yes
nwp
nwp
if (i == 0) return mat[0][j-1] + solve(0, j-1); seems wrong because if the width of the matrix is 0 then there are no elements in the matrix
maybe you meant i==1, or 0 actually means 1 element
23:11
@nwp Changing it to i==1 and j==1 gets me closer to the actual answer but it's still the wrong answer.
nwp
nwp
if you have the matrix [1,2][3,4] I think it counts the 4 twice, because it does 1 + [3,4] + [2][4]
user406009
@templateboy You should remove all the -1s when you get values from the matrix.
user406009
int solve(int i, int j) {
    if (i < 0 || j < 0) return 0;
    if (i == 0) return mat[0][j] + solve(0, j-1);
    if (j == 0) return mat[i][0] + solve(i-1, 0);
    return mat[i][j] + solve(i-1, j) + solve(i, j-1);
}
@Lalaland The problem says (1, 1) is the top left corner, and (i, j) is the bottom right corner. Your edit still doesn't give the right answer.
user406009
@templateboy But in C++ 0,0 is the top left and i-1 and j-1 is the bottom right.
23:22
@Lalaland For a 3x3 matrix, the indices will be [0..2][0..2] but in the program i and j are passed in as, for example, solve(3, 3). So in your example you will be accessing an out of bounds area. That's why I was doing i-1 and j-1.

« first day (44 days earlier)      last day (3033 days later) »