### initialize a matrix in while-loop

Hi,
I have written a program, in which I create a NxN-matrix.
As I need some different samples (some different such NxN-matrices), I try to create a new matrix in each recall of a while-loop. The first recall of the while-loop works without any problems, but the second recall crashes, but I get any errors from the compiler.
Here is my quell code. The problem should be with line 44:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147`` ``````#include #include #include #include #include #include #include #include #include using namespace std; int main(){ int N=10; int timeStep=10; string species; string matrix[N][N]; int samples = 10; // repeating simulation and making an average over different samples int numberOfOutputs = 6; // The outputs are: AA_density AB_density BB_density AA_AB AA_BB AB_BB double averageMaker[timeStep][numberOfOutputs]; /* initialize the elements of averageMaker to zero */ for(int i=0; i

I will be very thankful, if you could help me.
Last edited on
Line 60:` for(int i=1; i<=timeStep; i++)`. I think it should be `for(int i=0; i<timeStep; i++)`. When i==timeStep the array index is out of bounds on line 136-141
I changed `for(int i=1; i<=timeStep; i++)` to `for(int i=1; i<timeStep; i++)` and now it works :-) Thanks alot :-)
the loop,
 ``12`` `````` while (sample! = 0) ``````

is a condition always true, because you have put

 `` `` ``int sample = 10;``

for it never stops, and there is no other means to get out.

 Line 60: for(int i=1; i<=timeStep; i++). I think it should be for(int i=0; i

only with this change does not work
Last edited on
`while (samples! = 0)` is not always true, because I change the value of samples each time by `samples = samples -1;` in line 143.
int N= 10;

apologizes. the long code, I hid the bottom.
however, "N" can not be a variable. must be constant.

made the change, the code works.
Topic archived. No new replies allowed.