-
April 21st, 2011, 09:46 PM
#1
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.
-
April 21st, 2011, 10:36 PM
#2
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
-
April 21st, 2011, 11:44 PM
#3
Re: creating array with size determined by user input?
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
Last edited by Paul McKenzie; April 26th, 2011 at 04:25 AM.
-
April 26th, 2011, 12:52 AM
#4
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?
-
April 26th, 2011, 04:22 AM
#5
Re: creating array with size determined by user input?
Originally Posted by larry burns
I didn't really get the above comments.
Why not? They answer your question fully.
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
-
April 26th, 2011, 04:28 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|