Can't find my error

here is my code

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
radius = 0;
q = High + 1 + f;
t = Long + 1 + f;
a = 0;
b = 0;
h = 0;
if (q <= height && t <= length)
{	
  do{
    e = High + 1 + h;
    a = 1 + h;
    i = 0;
    do{
      g = Long + 1 + i;
      b = 1 + i;
      alpha = pow (a,2.0);
      beta = pow (b,2.0);
      if (alpha > 0 && beta > 0)
      {
	add = alpha + beta;
	radius = sqrt(add);
	if (radius <= hop)
	{
          e++;
	  g++;
	  if (e <= height || g <= length)
	  {
	    if ( grid[High][Long] == s && grid[e][g] == s)
	    {
	      cout << "ur(" << e << "," << g << "), ";
	    }
	  }
	}
      }
      i = i + 1;
    }while (radius <= hop); 
    h = h + 1;
  }while (h < hop);
}


the error is

Debug assertion fail
vector subscription out of range
I think the error is on line 28: if ( grid[High][Long] == s && grid[e][g] == s) check 'grid' sizes because it seems you are passing too high values to the [ ] operators
1
2
3
4
5
6
7
grid.resize(height);

for (int i = 0; i < height; ++i)
grid[i].resize(length);

height = height - 1;
length = length - 1;


ps High and Long never exceed height and length
1
2
3
if (e <= height || g <= length)
 {
    if ( grid[High][Long] == s && grid[e][g] == s) // guess what happens if e == height || g == length 


Last edited on
)c:

I tried that it gives the same error at the same place.
do you want to see the entire code it's really big but it might help?
closed account (z05DSL3A)
When you have a container of n elements you index it from 0 to n-1.

So if height is the size of the container either use
e < height or e <= height-1
tried both of those

here's my code maybe it will help

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <iostream>
#include <vector>
#include "string"
#include <cmath>

using namespace std;
using std::vector;

int main()
{
	  vector<vector<string> > grid;

	int length;
	int height;
	int Long;
	int High;
	int hop;

	int a;
	int b;
	int c;
	int d;
	int e;
	int f;
	int g;
	int h;
	int i;
	int j;
	int k;
	int q;
	int t;
	
	double alpha;
	double beta;
	double per;
	double radius;
	double win;
	double up;
	double pin;
	double add;

	char r;
	string s;

	cout << "How long do you want your grid? "; //set up length of grid
	cin  >> length;

	cout << "How tall do you want your grid? "; //set up height of grid
	cin  >> height;

	cout << endl;


	//##########################################find out how many nodes you need


	do{
		cout << "What percentage of nodes do you want? ";
		cin  >> per;
		cout << endl;

		if (per <= 0)
		{
			cout << "INVALID ENTRY" << endl;
		}
	}while (per <= 0);

	win = length*height;

	cout << "Out of " << win << " grid markers, you have ";
	up = (per/100);
	pin = win * up;
	
	int rounded = static_cast<int>(pin+.5);

	cout << rounded << " nodes." << endl;  

	grid.resize(height);

	for (int i = 0; i < height; ++i)
	grid[i].resize(length);

	height = height - 1;
	length = length - 1;


	// ##################################################assign grid


	High = 0;
	
	if (per < 100)
	{
		do{
			Long = 0;
			do{
				if (rounded > 0)
				{
					c = High + 1 ;
					d = Long + 1 ;
					cout << "Grid location " << d << ", " << c 
						<< " would you like to place a node here? Y/N" << endl;
					cin  >> r;

					if (r == 'y' || r == 'Y')
					{
						grid[High][Long] = "*";
						Long = Long + 1;
						rounded = rounded - 1;

						cout << rounded << " nodes left." << endl << endl;
					}
	
					else if (r == 'n' || r == 'N')
					{
						grid[High][Long] = ".";
						Long = Long + 1;
					}
					else
					{
						grid[High][Long] = ".";
						Long = Long + 1;
					}
				}
				else if (rounded == 0)
				{
					grid[High][Long] = ".";
					Long = Long + 1;
				}
			}while( Long <= length);

			High = High + 1;
		}while(High <= height);
	}
	
	

	// ############################################for 100 percent


	else					
	{
		High = height;
		do{
			Long = 0;
			do{
				grid[High][Long] = "*";
				Long = Long + 1;
			}while (Long <= length);
			High = High - 1;
		}while(High > -1);
	cout << endl;
	}


	//####################################################display grid


	High = height;
	do{
		Long = 0;
		do{
			cout << grid[High][Long];
			Long = Long + 1;
		}while (Long <= length);

		High = High - 1;
		cout << endl;
	}while(High > -1);

	cout << endl;


	//#########################################communication between nodes


	j = k = 0;
	s = "*";
	cout << "What is the transmition range?" << endl;
	cin  >> hop;

	do{
		if (hop < 1)
		{
			cout << "Invalid entery, no node can communicate." << endl 
				<< "What is the transmition range?" << endl;
			cin  >> hop;
		}
	}while (hop < 0);

	cout << "Displayed in (x,y) form." << endl << endl;

	Long = 0;
	do{
		High = 0;
		do{
			f = 0;
			c = High + 1 ;
			d = Long + 1 ;
			
			if (grid[High][Long] == s)
			{
				cout << "Node at point (" << d << "," << c 
						<< ") can communicate with " << endl;
				do{
					// beneath subject node
					q = High - 1 - f;
					if (q >= 0)
					{
						if (grid[High][Long] == s && grid[q][Long] == s)
						{
							q = High - f;
							cout << "d(" << d << "," << q << "), ";
						}
					}
					// above subject node
					q = High + 1 + f;
					if (q <= height)
					{
						if (grid[High][Long] == s && grid[q][Long] == s)
						{
							q = High + 2 + f;
							cout << "u(" << d << "," << q << "), ";
						}
					}
					// right of subject node
					q = Long + 1 + f;
					if (q <= length)
					{
						if (grid[High][Long] == s && grid[High][q] == s)
						{
							q = Long + 2 + f;
							cout << "r(" << q << "," << c << "), ";
						}
					}
					// left of subject node
					q = Long - 1 - f;
					if (q >= 0)
					{
						if (grid[High][Long] == s && grid[High][q] == s)
						{
							q = Long - f;
							cout << "l(" << q << "," << c << "), ";
						}
					}
//#####Here is the problem######### upper right corner of subject node
					radius = 0;
					q = High + 1 + f;
					t = Long + 1 + f;
					a = 0;
					b = 0;
					h = 0;
					if (q <= height && t <= length)
					{
						do{
							e = High + 1 + h;
							a = 1 + h;
							i = 0;
							do{
								g = Long + 1 + i;
								b = 1 + i;
								alpha = pow (a,2.0);
								beta = pow (b,2.0);
								if (alpha > 0 && beta > 0)
								{
									add = alpha + beta;
									radius = sqrt(add);
									if (radius <= hop)
									{
										e++;
										g++;
										if (e < height || g < length)
										{
											if ( grid[High][Long] == s && grid[e][g] == s)
											{
												cout << "ur(" << e << "," << g << "), ";
											}
										}
									}
								}
								i = i + 1;
							}while (radius <= hop); 
							h = h + 1;
						}while (h < hop);
					}
					// lower right corner of subject node
					// upper left corner of subject node
					// lower left corner of subject node
					f = f + 1;
				}while (f < hop);
				cout << "no other nodes." << endl << endl;
			}
			High = High + 1;
		}while (High <= height);
		Long = Long + 1;
	}while(Long <= length);
		
	cout << endl;

	return 0;
}
Last edited on
if (e < height || g < length) If e or g are OK => if one of them is wrong, the next block will be executed
if (e < height && g < length) if both e and g are OK => if one of them is wrong, the next block won't be executed
Topic archived. No new replies allowed.