Dinamic size of a matrix

Hi everyone,
My purpose is to fill a matrix with random numbers. As you can see in the code, I defined a matrix called sim_matrix (which should be filled), but the dimensions of such matrix is given by the parameters that we introduce in the constructor. Because we don't know the size of such matrix until we give these parameters through the constructor. I'd like to know how to declare such matrix, or how to modify the size,... Does anyone of you know how to do that?

The header of my file MonteCarlo.h is:
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
/* MonteCarlo.h */

#ifndef MONTECARLO_H_
#define MONTECARLO_H_

using namespace std;

class MonteCarlo {
public:
	MonteCarlo(int n_paths, int n_simulations);
	~MonteCarlo();

	
	int np,nt;
	double sim_matrix[?][?];

	//methods
	void 	set_np(int a);
	void 	set_nt(int b);
	int  	get_np();
	int  	get_nt();
	void 	simulate(int a, int b);
	double 	get_matrix(int row, int col);
};

#endif /* MONTECARLO_H_ */ 


In the other hand, the cpp code called as MonteCarlo.cpp is
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * MonteCarlo.cpp
 *
 *  Created on: 17/04/2013
 *      Author: carlos
 */
#include <iostream>
#include<fstream>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>
#include "MonteCarlo.h"
#include "BlackScholes.h"

using namespace std;

MonteCarlo::MonteCarlo(int n_paths, int n_simulations)
{
	//number paths
	set_np(n_paths);
	//number steps
	set_nt(n_simulations);

	//now we simulate and save the random normal number in the matrix sim_matrix[][]
	//the two elements get_np and get_nt is setting the matrix size
	simulate(get_np(),get_nt());
}


MonteCarlo::~MonteCarlo() { }

//setters and getters methods
void 	MonteCarlo::set_np(int a){np=a;}
int  	MonteCarlo::get_np(){return np;}
void 	MonteCarlo::set_nt(int b){nt=b;}
int  	MonteCarlo::get_nt(){return nt;}
double 	MonteCarlo::get_matrix(int row, int col){return sim_matrix[row][col];}

//we fill the matrix sim_matrix with random numbers (normal distributed)
void MonteCarlo::simulate(int a, int b)
{
	//It will generate the same values...not problematic for the moment
	boost::random::mt19937 gen;
	//setting the boost for a normal distribution de ~N(0,1)
	boost::random::normal_distribution<> dist(0.0, 1.0);
	for (int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			//setting the random number to the matrix, indexes i for row and j for colum
			sim_matrix[i][j] = dist(gen);
		}
	}
}


I'll be very thankful if someone of you try to help me with this topic.
Operator new []

Reserve heap memory with that. Member variable is a pointer type that is set to point to the allocated memory.

Indexing: you either have an array of pointers to beginning of each row, or implement operator[](size_t) cleverly.


Actually, scratch all that; use std::vector<std::vector<double> >
Oh, thanks! I didnt know such dynamic creation object...I'm gonna look for more information
Thanks a lot!!
Topic archived. No new replies allowed.