# LCS (how can i improve this code)

I created this code using dynamic programming approach but the code is not display the result. the code is as follows:

``````#include <iostream>
using namespace std;

void LCS(int input1[], int input2[], int n, int m) {
int L[n + 1][m + 1];  /*This matrix stores the length of common subsequences*/
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (input1[i - 1] == input2[j - 1])
L[i][j] = 1 + L[i - 1][j - 1];
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
int index = L[n][m];
int lcs[index];
int i = n, j = m;
while (i > 0 && j > 0) {
if (input1[i - 1] == input2[j - 1]) {
lcs[index - 1] = input1[i - 1];
i--;
j--;
index--;
} else if (L[i - 1][j] > L[i][j - 1])
i--;
else
j--;

}
for (int i = 0; i < index; i++){
cout << lcs[i];
}

}
int main() {
int n, m;
cin >> n >> m;
int input1[n], input2[m]; /*two arrays from which longest subsequnce is to be found*/
for (int i = 0; i < n; i++)
cin >> input1[i];
for (int i = 0; i < m; i++)
cin >> input2[i];
LCS(input1, input2, n, m);
return 0;
}``````
``` The code terminates without showing any result! I even switched to a different IDE but its the same. What is wrong with this? Please help.```
``` ```
``` cpp algorithms dynamic-programming asked Sep 26, 2016 by nrb ```
``` ```
``` ```
``` 2 Answers In c++, Variable length arrays are not legal such as input1[n] intput2[m];  if you want to use variable length array  then you will have to allocate memory dynamically for an arrays.for example double* input[n]=new double[n]; Don't forget to delete when you're done .   or you can use standard container std::vector<double> intput(n); by including <vector.h> library.   answered Sep 28, 2016 by 4nkush for (int i = 0; i < index; i++){ cout << lcs[i]; In last loop of your function index value reached at 0 and you are using i < index this will i < 0 so there will nothing in output. Just relace index = L[n][m] you will get answer. // corrected loop will be index = L[n][m];   for ( int i = 0 ; i < index ; i++) cout << lcs[i]; Hope it will help you if not then let me know. answered Sep 29, 2016 by Hariom Singh Bais ```