Arrays in numerical computing

Hi,

To date, I have done a fair amount of numerical computing (mostly numerical solutions to ODEs/PDEs) in Matlab. I'm now taking a course where the professor would prefer us to do our coding in C++. I've taken an introductory course to programming in C++, so I know the very basics of the language, but there are many subtleties that I'm not yet comfortable with.

So far, the thing I miss most about Matlab is the ability to use arrays whose size you don't know yet, and to change the size of those arrays later if necessary.

For example, let's say I want to discretize a domain [0,1] in intervals of h = 0.1. Then I'd like to make a spacial array of points, x, which would end up containing 11 coordinate points on [0,1]. Now I run my code again, but I'd like to discretize using h = 0.01, so the array x now contains 101 coordinate points.

Ideally, I'd like to do something like:

h = 0.1 // set discretization level
double x[(1/h)+1] // initialize array with correct number of points

But I know that's not allowed in C++. Is there a way to work around this? I know a simple solution would be to declare arrays of sizes much larger than necessary and then just use the space that you need, but that seems messy and wasteful. Are there any clean solutions to this question?

Thank you.
Read up on Vectors:

http://www.cplusplus.com/reference/stl/vector/

"unlike regular arrays, storage in vectors is handled automatically, allowing it to be expanded and contracted as needed."
Topic archived. No new replies allowed.