Dynamic 2D array

This is a discussion on Dynamic 2D array within the C++ Programming forums, part of the General Programming Boards category; Hi I am trying to make a Monte-Carlo simulation in C++, and in that respect I need to store two ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    Dynamic 2D array

    Hi

    I am trying to make a Monte-Carlo simulation in C++, and in that respect I need to store two values x and y in a 2D array.

    Here is the situation: I have a loop running until some condition is satisfied. For ever iteration I want to write a pair of values (x, y) to my array, but the problem is that I don't know how many iterations there will be until the loop stops.

    So my question is how I can make a 2D-array dynamically?

    Thanks for any help.

    Best,
    Niels.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    Have you considered using std::vector, this will grow as required.

    Jim

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Are the ranges of x and y somehow dependent on your number of iterations?

    I thought the general idea of MC was "throwing random darts at a dartboard" and then seeing how many meet some criterion (distance from centre) to give you some approximate answer to some value (say PI).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by jimblumberg View Post
    Have you considered using std::vector, this will grow as required.

    Jim
    Thanks, they did the trick.

    Quote Originally Posted by Salem View Post
    Are the ranges of x and y somehow dependent on your number of iterations?

    I thought the general idea of MC was "throwing random darts at a dartboard" and then seeing how many meet some criterion (distance from centre) to give you some approximate answer to some value (say PI).
    They do depend on the number of iterations (on average) in my case. Yes, MC can be used for stuff like that (evaluating integrals numerically, estimating Pi, etc..), but those are merely simple examples of what stochastic processes can be used for.


    Thanks for the help.

    Best,
    Niels.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-03-2011, 01:26 PM
  2. Copy array into a Dynamic array
    By pantherman34 in forum C Programming
    Replies: 15
    Last Post: 05-01-2010, 10:58 PM
  3. Dynamic Array
    By Zuko in forum C Programming
    Replies: 3
    Last Post: 11-09-2007, 01:55 PM
  4. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  5. dynamic array
    By sweets in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 11:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21