Matricies

Hello, I'd like to start by saying I am very new to programming. I am taking Computer Simulations in the physical sciences. So far it is going well. For one of our assignments we had to take the determinate of a 4x4 matrix. This went well but made me curious to do more. I would like to develop a program that will take the determinate of any size matrix. However, I am stuck on how to make a function/program that will set up some matrix that is nxn? Any thoughts or places to look would be great! Thanks

Multidimensional arrays
You'll need to create and allocate the array at run time. Because of this, I suggest that you use vector<> instead of built in arrays. Warning: the rest of this is off the top of my head without enough coffee this morning....

To use vectors you first need to include the header file that declares them. Also, to use the name vector directly instead of having to say std::vector all the time you have to tell the compiler:
1
2
#include <vector>
using std::vector;


A 2D array is a vector of vector of numbers. Let's call this a Matrix:
typedef vector<vector<double> > Matrix;

Now let's make a square NxN matrix:

1
2
3
int N;
// code to set N
vector<double> dummyRow(N);   // A vector that looks like 1 row 

// Now create a Matrix whose first dimension is N. Initialize each of the N items with
// a copy of dummyRow, which is a vector of N doubles. The result is an NxN matrix
Matrix myMatrix(N, dummyRow);
Here is the reference for vector:
http://www.cplusplus.com/reference/vector/vector/
Topic archived. No new replies allowed.