Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cant initialize a 2d array/matrix to 0

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading