Thread: How to Pass Matrix in Function

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    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. #2
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    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. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    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. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass a matrix/array from main to a function
    By Inukami in forum C Programming
    Replies: 7
    Last Post: 12-09-2009, 09:03 PM
  2. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  3. C function - Pass by value? Pass By Ref?
    By stevong in forum C Programming
    Replies: 4
    Last Post: 11-18-2005, 08:02 AM
  4. Replies: 3
    Last Post: 04-02-2002, 01:39 PM