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:

1
2
3
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

1
2
3
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.
Last edited on
Size of ordinary (C-like) arrays needs to be known at compile time. You should use std::vector instead: http://www.cplusplus.com/reference/stl/vector/

BTW, you should post this in the beginners section.
Last edited on
I didn't really get the above comments. Anyways, I tried this

1
2
3
4
5
6
7
8
9
10
11
12
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:

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


along with:

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



What am I doing wrong here?
ignoring everthing other than your question posted as the topic:

heres how you would create an "array with size determined by user input"
considering its an array of ints:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <limits>  // needed for pause
using namespace std;

int main()
{
    cout << "enter in size of array" << endl;
    // answer to your question
    int size;
    cin >> size;
    int array[size];
    // end of answer to your question
    cout << "array[" << size << "] has been created" << endl;
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); //pause
    return 0;
}


let me know if this is not what your looking for
Last edited on
@metl wolf
Your code is not a valid C++. You're using C99's variable length array.

@larry burns
I would suggest to do something like this:

1
2
3
vector<float> Temp(N * T);
...
Temp[n * T + t] = 0;

Vector of vector is not really good way of creating a 2-dimensional array.
To clear things up:

Yes, metl wolf's code is not correct. You cannot declare a variable-sized array like that.
As for Abramus' suggestion, sounds good to me.

Finally, this is how you create an array based on user's input:

1
2
3
4
5
//Assuming you got the user to input the data and you stored it in N and T...
float *arrMatrix = new float[N * T];
//Use the matrix as Abramus suggests.
//When you are done using the matrix, you must delete it to avoid memory leaks.
delete[] arrMatrix;
I'm confused as to what what "n" means. Did you intend for that to be the same as "i" that I used in my For loops? I also have no clue why you have "T + t". And what goes in the "..."? I'm completely new to vectors, so I would appreciate it if you could provide detailed explanations for why you wrote those suggestions. I've also only used arrays a few times for the past few years and haven't used pointers in years. Thanks
Here is my solution, and I apologize that it diverge slightly from the conversation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

using namespace std;

int main()
{
	//initialize the matrix
	int** Matrix;						//A pointer to pointers to an int.
	int rows,columns;
	cout<<"Enter number of rows: ";
	cin>>rows;
	cout<<"Enter number of columns: ";
	cin>>columns;
	Matrix=new int*[rows];				//Matrix is now a pointer to an array of 'rows' pointers.
	
	//define the matrix
	for(int i=0;i<rows;i++)
	{
		Matrix[i]=new int[columns];		//the ith array is initialized
		for(int j=0;j<columns;j++)		//the i,jth element is defined
		{
			cout<<"Enter element in row "<<(i+1)<<" and column "<<(j+1)<<": ";
			cin>>Matrix[i][j];
		}
	}
	
	//Print the matrix
	cout<<"The matrix you have input is:\n";
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
			cout<<Matrix[i][j]<<"\t";	//tab between each element
		cout<<"\n";						//new row
	}
	
	//now, we have to free up the memory we took by releasing each vector:
	for(int i=0;i<rows;i++)
		delete[] Matrix[i];				//c++ can delete up to 1 dimensional vectors
}

This method for me has been the most intuitive. I appreciate any corrections or improvements.
Last edited on
If I'm not mistaken, the n and t in Abramus and webJose's suggestions are the column and row variables (i and j in my example). Note that 0 <= t < T and 0 <= n < N. So each element is stored at the memory address n*T+t; that is, Temp[n * T + t] refers to the element in column t and row n (of course, row/column are arbitrary, but it seems more intuitive to make arrays of rows than columns). This is because c++ has to store your two-dimensional array in a one-dimensional memory address space, so it flattens it into n T-sized arrays that follow each other (or are scattered and pointed to in memory, as I believe is the case in my example).
Then the "..." is probably where the elements of your matrix are defined. I think webJose has made the array one-dimensional (so the memory is connected) by declaring one T*N memory space as a vector that can be referenced as above (arrMatrix[n*T+t]), whereas I made a bunch of arrays connected by a double-pointer.
Topic archived. No new replies allowed.