2D array/vector

Hi,

I want to build a 2D array (or vector), which I can use as function input.

So far, I have the following code, but it does not work. It gives the following error:
2d.cpp: In function ‘int main()’:
2d.cpp:16: error: variable-sized object ‘M’ may not be initialized

But it works if I make it like : M[10][10] = {{0}}, but the problem is that the size is dependent on the length of strings seq1 and seq2.

I used the C++ array, but solution with <vector> are also ok (and perhaps better?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;


int main() {
  int L1,L2;
  string seq1,seq2;
  seq1 = "actgc";
  seq2 = "agtgt";
  L1 = seq1.size()+1;
  L2 = seq2.size()+1;
  int M[L1][L1] = {{0}};
  

  return 0;
}

Last edited on
Arrays must have constant length.
See http://www.cplusplus.com/forum/articles/17108/
It will both introduce you to the types of 2d arrays and tell you why you shouldn't use them!
Topic archived. No new replies allowed.