I am trying to initialize a 2d array (matrix) to 0 but, cant do it:
int longestCommonSubsequence(string text1, string text2) {
int len1=0;
len1=text1.length()+1;
int len2=0;
len2=text2.length()+1;
int dp[len1][len2]={0};
Error:
Line 8: Char 16: error: variable-sized object may not be initialized
int dp[len1][len2]={0};
^~~~
1 error generated.
I want initialize the matrix while declaring it. I do not want to use for loop.
Variable sized arrays are not standard C++, for arrays of constant length you just use value-initialisation syntax:
int arr[4][4]{};
Alternatively, use C++ collections of variable length:
int rows = 4;
int columns = 4;
std::vector<std::vector<int>> vec(rows, std::vector<int>(columns));