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.
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
Re: creating array with size determined by user input?
Quote:
Originally Posted by
larry burns
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
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?
Re: creating array with size determined by user input?
Quote:
Originally Posted by
larry burns
I didn't really get the above comments.
Why not? They answer your question fully.
Quote:
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
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