I was looking for solving a LCS problem (Longest common subsequence) and I tried to make my own code in c++ by referring to the explanation and the pascal code given at wikipedia.
My final result was this:
#include <iostream>
#include <algorithm>
using namespace std;
int LCS(int a[100], int b[100], int m, int n);
int main()
{
int n, m, i, k, x[100], y[100];
cout << "n i m: " << endl;
cin >> n >> m;
cout << "n array: " << endl;
for(i=1;i<=n;i++)
cin >> x[i];
cout << "m array: " << endl;
for(i=1;i<=m;i++)
cin >> y[i];
k = LCS(x[100], y[100], m, n);
cout << k << endl;
return 0;
}
int LCS(int a[100], int b[100], int m, int n)
{
int c[m][n], i, j;
for(i=0;i<=m;i++)
c[i][0] = 0;
for(i=0;i<=n;i++)
c[0][i] = 0;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(a[i] == b[j])
{
c[i][j] = c[i-1][j-1]+1;
}
else
c[i][j] = max(c[i][j-1], c[i-1][j]);
}
}
return c[m][n];
}
I tried to compile it via g++ and i got an error:
3.cpp: In function 'int main()':
3.cpp:19: error: invalid conversion from 'int' to 'int*'
3.cpp:19: error: initializing argument 1 of 'int LCS(int*, int*, int, int)'
3.cpp:19: error: invalid conversion from 'int' to 'int*'
3.cpp:19: error: initializing argument 2 of 'int LCS(int*, int*, int, int)'
I’m not really into c/c++ programming, and i want to know where is my mistake, why it happens and how to fix it. Thank you.
Just pass the array name.
This
not this