0

Hi,
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.

Thanks!

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by maddav
0

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;
 vecOfArray2D.resize(size);

The Array2D is a class that you should implement.

0

Thanks!

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!

0

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

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

using namespace std;

class layerinfo {
public:
	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;
	layer.resize(N);
			
}

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 {
public:
	double matrix[2][2];
	double rho;
	double k;
	double r;
...

Would this be ok?

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.