CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2009
    Posts
    27

    creating array with size determined by user input?

    So in MATLAB, I can create a matrix with a size determined by the user input, such as below:


    T=input('Number of data points for time variable? ');
    N = input('Number of grid points N? ');
    Temp = zeros(N,T);



    So that if I enter 5 for T and 4 for N, then Temp will be a 4 by 5 matrix. But I'm having trouble doing the same thing for C++. I defined T and N as variables, such as like this


    int T;
    cout<<"Number of data points for time variable? ";
    cin>>T;


    and similarly for N.. But then I tried writing


    float Temp[][];

    or

    float Temp[N][T]


    then I got errors. How do I fix this?


    On a side note, are there any computational physicists here? Are there any good websites that have relatively simple codes for comp physics problems? I'm been looking for websites that have codes with problems that I could work on but haven't found many. The code I wrote above was to solve the Heat Equation numerically.

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: creating array with size determined by user input?

    Code:
    float **Temp;
    
    Temp = new float*[N];
    for(int i = 0; i < N; ++i)
        Temp[i] = new float[T];
    
    for(int i = 0; i < N; ++i)
        delete[] Temp[i];
    delete[] Temp;
    You are probably better off with a vector of vectors or Boost.MultiArray

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: creating array with size determined by user input?

    Quote Originally Posted by larry burns View Post
    But I'm having trouble doing the same thing for C++. I defined T and N as variables, such as like this
    Code:
    #include <vector>
    typedef std::vector<float> Float1D;
    typedef std::vector<Float1D> Float2D;
    //...
    int main()
    {
        int T, N;
        //...
        Float2D myArray(N, Float1D(T,0));  // an N x T float array
        // now use myArray just like a 2D array
    }
    This also means that instead of arrays as arguments, you pass Float2D (by reference) to those functions.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 26th, 2011 at 04:25 AM.

  4. #4
    Join Date
    Nov 2009
    Posts
    27

    Re: creating array with size determined by user input?

    I didn't really get the above comments. Anyways, I tried this

    vector< vector<float> >Temp(N, vector<float>T);

    for (int i=0; i<N; i++)
    {
    Temp[i][0]=5;
    }


    for (int i=0; i<T; i++)
    {
    Temp[0][i]=0;
    }



    But I got these error messages:

    'std::vector<_Ty>' : illegal use of this type as an expression
    with
    [
    _Ty=float
    ]

    along with:

    syntax error : missing ')' before identifier 'T'
    syntax error : ')'


    What am I doing wrong here?

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: creating array with size determined by user input?

    Quote Originally Posted by larry burns View Post
    I didn't really get the above comments.
    Why not? They answer your question fully.
    Anyways, I tried this
    Please post the entire code you're compiling. If I were to take your code and compile it, I would be bombarded with errors due to missing #include file, no namespace specified, etc.

    And please use code tags when posting code.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: creating array with size determined by user input?

    This compiles correctly.
    Code:
    #include <vector>
    
    using namespace std;
    
    int main()
    {
       int N = 10;
       int T = 40;
       vector< vector<float> >Temp(N, vector<float>(T));
        for (int i=0; i<N; i++)
            Temp[i][0]=5;
    
        for (int i=0; i<T; i++)
            Temp[0][i]=0;
    }
    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured