I'm very much a beginner to C++, but I've been assigned a project outside of my programming class that is starting to get tricky.

I'm modelling a layered optical system and I need to create N two-dimensional arrays (2x2 matrices to be used in matrix multiplication), where N is determined at runtime (entered through keyboard).

I know how to create 2D arrays dynamically, but can't think how to create N. I have a feeling it's going to consist of a struct array but I really can't think where to begin.

Any advice/hints/tips would be greatly appreciated.


What you should do is create a 2D array class. Then use a vector to hold it. Here is what I mean :

std::vector< Array2D > vecOfArray2D;
 int size = 0;
 cin >> size;

The Array2D is a class that you should implement.


We haven't really covered classes or vectors yet in our C++ lectures yet, but I'll do some reading today and give this a go.

Thanks again!

thanks, cplusplus is pretty much my "go to" site at the moment!

#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

class layerinfo {
	double matrix[2][2];

int main(void)
	vector <layerinfo> layer;
	int size = 0;
	cout << "Enter number of layers:" << endl;
	cin >> size;
	int N = size+1;

From the post and doing a bit of reading I've gathered that this (code above) essentially creates a container (vector) of types "layerinfo", which themselves contain the characteristic matrix of each layer (+1 required for boundary).

Since I have other parameters (rho, k, r) associated with each layer, that I currently have rather crudely strewn together using arrays, I'm assuming i could include them in the layerinfo class, i.e.

class layerinfo {
	double matrix[2][2];
	double rho;
	double k;
	double r;

Would this be ok?

just worked through it and now defined all the layer info in the class, so problem solved!

Thanks for everyone's help!

This question has already been answered. Start a new discussion instead.