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:
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>
#include <ctype.h>
#include <string>
#include <iostream>
#include <fstream>
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<timeStep; i++){
		for(int j=0; j< numberOfOutputs; j++){
			averageMaker[i][j]= 0;
		}
	}

	srand(time(NULL));
	double speciesAA, speciesBB, speciesAB; // number of species
	double AA_density, AB_density, BB_density;
	while(samples!=0){
		speciesAA=0; speciesBB=0; speciesAB=0; // number of species
		/*initialize the lattice and counting the number of different species*/
		for(int i=0; i<N; i++){
			for(int j=0; j<N; j++){
				int r = rand()%2;
				if(r==0){
					species = "AA";
					speciesAA = speciesAA + 1;
				}
				else{
					species = "BB";
					speciesBB = speciesBB + 1;
				}
				matrix[i][j]=species;
				cout<<"Here is the Problem !!!"<<endl;
			}
		}

		/*density of each species*/
		AA_density = speciesAA/(N*N);
		AB_density = speciesAB/(N*N);
		BB_density = speciesBB/(N*N);

			averageMaker[0][0] = averageMaker[0][0] + AA_density;
			averageMaker[0][1] = averageMaker[0][1] + AB_density;
			averageMaker[0][2] = averageMaker[0][2] + BB_density;
			averageMaker[0][3] = averageMaker[0][3] + 1;
			averageMaker[0][4] = averageMaker[0][4] + 1;
			averageMaker[0][5] = averageMaker[0][5] + 1;

			for(int i=1; i<=timeStep; i++){ // Monte Carlo timeStep
				for(int l=0; l<N*N; l++){//in each timeStep we have N*N updates
					std::string site = "CC" ;
					std::string neighbour = "CC";
					/*choosing a site randomly*/
					int siteRow = rand()%N;
					int siteColumn = rand()%N;
					site = matrix[siteRow][siteColumn];
					/*choosing a neighbor randomly for periodic boundary condition */
					int rowOrColumn = rand()%2;
					int moreOrLess = rand()%2;
					if(rowOrColumn == 0){ //changing row
						if(moreOrLess == 0 && siteRow != N-1)
							neighbour= matrix[siteRow + 1][siteColumn]; //choosing lower neighbour
						if(moreOrLess == 1 && siteRow != 0)
							neighbour= matrix[siteRow - 1][siteColumn]; //choosing upper neighbour
						// if the site is a border-site:
						if(moreOrLess == 0 && siteRow == N-1)
							neighbour= matrix[0][siteColumn];
						if(moreOrLess == 1 && siteRow == 0)
							neighbour= matrix[N-1][siteColumn];
					}
					else{ //choosing column
						if(moreOrLess == 0 && siteColumn != N-1)	neighbour= matrix[siteRow][siteColumn + 1]; //choosing right hand side neighbour
						if(moreOrLess == 1 && siteColumn != 0)	neighbour= matrix[siteRow][siteColumn - 1]; //choosing left hand side neighbour
						// if the site is a border-site:
						if(moreOrLess == 0 && siteColumn == N-1)	neighbour= matrix[siteRow][0];
						if(moreOrLess == 1 && siteColumn == 0)	neighbour= matrix[siteRow][N - 1];
					}

					/* defining site's neighbors */
					std::string siteNeighbor_up, siteNeighbor_down, siteNeighbor_left, siteNeighbor_right;
					int upSite, downSite, leftSite, rightSite;
					upSite = siteRow -1;
					if(upSite == -1) upSite = N-1;
					downSite = siteRow +1;
					if(downSite == N) downSite = 0;
					leftSite = siteColumn -1;
					if(leftSite == -1) leftSite = N-1;
					rightSite = siteColumn +1;
					if(rightSite == N) rightSite = 0;
					siteNeighbor_up = matrix[upSite][siteColumn];
					siteNeighbor_down = matrix[downSite][siteColumn];
					siteNeighbor_left = matrix[siteRow][leftSite];
					siteNeighbor_right = matrix[siteRow][rightSite];

					
					/*interaction*/
					if((site == "AA" && neighbour == "BB") || (site == "BB" && neighbour == "AA")){
						matrix[siteRow][siteColumn] = "AB";
						speciesAB = speciesAB + 1;
						if(site == "AA") speciesAA = speciesAA - 1; // number of species
						else speciesBB = speciesBB - 1;
					}
					if((site == "AA" && neighbour == "AB") || (site == "AB" && neighbour == "AA")){
						matrix[siteRow][siteColumn] = "AA";
						if(site == "AB"){
							speciesAA = speciesAA + 1;
							speciesAB = speciesAB - 1;
						}
					}
					if((site == "AB" && neighbour == "BB") || (site == "BB" && neighbour == "AB")){
						matrix[siteRow][siteColumn] = "BB";
						if(site == "AB"){
							speciesBB = speciesBB + 1;
							speciesAB = speciesAB - 1;
						}
					}

					
				}
				/*density of each species*/
				AA_density = speciesAA/(N*N);
				AB_density = speciesAB/(N*N);
				BB_density = speciesBB/(N*N);

				averageMaker[i][0] = averageMaker[i][0] + AA_density;
				averageMaker[i][1] = averageMaker[i][1] + AB_density;
				averageMaker[i][2] = averageMaker[i][2] + BB_density;
				averageMaker[i][3] = averageMaker[i][3] + 1;
				averageMaker[i][4] = averageMaker[i][4] + 1;
				averageMaker[i][5] = averageMaker[i][5] + 1;
			}
			samples = samples -1;
	}
}


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,
1
2
 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<timeStep; i++). When i==timeStep the array index is out of bounds on line 136-141

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.