tags:

views:

519

answers:

8

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.

A: 

The problem is in line

k = LCS(x[100], y[100], m, n);

change it to

k = LCS(x, y, m, n);
Yogesh Arora
A: 

LCS accepts an array of 100 ints. You pass it a single int. This is invalid in C++. This is just one of many problems in this code, by the way. Just as an example, this:

int c[m][n]

Is also invalid in C++.

If you're not familiar with a language, it's a good practice to start with small code snippets and build-up iteratively, step by step. It's a better way to gain understanding than to translate a large chunk of code from another language and just cross your fingers for it to compile and work.

Eli Bendersky
+5  A: 

Just pass the array name.

This

LCS(x, y, m, n);

not this

LCS(x[100], y[100], m, n);
Daniel A. White
+3  A: 

Change k = LCS(x[100], y[100], m, n); to k = LCS(x, y, m, n);.

Kyle Lutz
A: 
McAden
A: 

Your code to call the function is mixed up:

k = LCS(x[100], y[100], m, n);

This passes the 101st entry of x in the first argument, and the 101st entry of y in the second. Presumably the function wants you to pass the address of the array, so it should be LCS(x, y, m, n).

Andy Ross
A: 
int LCS(int a[100], int b[100], int m, int n)
{
 int c[m][n], i, j;
...

You cannot declare variable-length arrays on the stack (or anywhere) in C/C++ like that. You ether need to allocate them on the heap or use constant-length arrays.

Nikolai N Fetissov
A: 

At least part of the problem is this line:

int c[m][n], i, j;

You can't use a variable to declare the size of an array. You will need to do something like:

int **c = new int[m][n];

Then at the end,

int ret = c[m][n];
delete [][] c;
return ret;
Graeme Perrow
The C99 standard allows for variable length arrays, so that first line is valid.
Kyle Lutz
I didn't know that - turns out MS Visual C++ doesn't support them. http://msdn.microsoft.com/en-us/library/zb1574zs.aspx
Graeme Perrow