# Thread: How to Pass Matrix in Function

1. ## How to Pass Matrix in Function

I am unsure how to pass a matrix in a function using codeblocks. I keep trying different things, but can't figure it out. Here is a part of my code. Where I have void getdata I keep getting errors on the stats, which is a matrix. Any help would be great. Thanks
Ryan

void getdata (double &stats, vector <string> &names, int num);
void file (double &stats, vector <string> &names, int num);
void display (int num);
int main ()
{
int num, ans;
cout<<"How many players are there: ";
cin>>num;
double stats[num][3];
vector <string> names (num);
int flag = 1;

2. 1) Please use code tags

2) I do not know a language called codeblocks but an IDE

3) please provide more clear code

3) you may want to define a maximum acceptable size for your input.

3. int num;
std::cin >> num;
std::vector<std::vector<double>> vec(num, std::vector<double>(3));
foo(vec);

You can't create a dynamically sized array on the stack.

4. ## Still Confused

It is the 2 dimensional array that I cannot pass. I use to use Borland C++ and I used apmatrix. I could just put apmatrix stats into the function parameters. In this case I am using Stats as a two dimensional array/vector and in codeblocks C++ I keep getting errors for my function with the variable stats. I just need to know how to write it in the parameters or if there is another way to declare a two dimensional array/vector so I can pass it in my function.
Elysia, thanks for your help, but I really don't understand what you are saying. Can you explain?
Thanks,
Ryan

5. The size of an array must be a compile time constant. Variable Length Arrays are not allowed in C++. So since you seem to know about vectors you should probably use a vector<vector<double>> for your stats variable.

Also if stats is an array then your function prototype is probably incorrect:
Code:
`void getdata (double &stats, vector <string> &names, int num);`
The above prototype expects you to pass a reference to a single double, a vector<string> reference, and an int. What are you trying to pass to this function?

Jim