Game Of life Error _DEBUG_ERROR("vector subscript out of range");

Problem was fixed
Last edited on
You will go outside array bounds on lines
83
87
91
95
100
104
108
113
(at least)
Yeah I know that, but how to fix it?
if ( x >=1 && y >= 1 && inputArray[x - 1][y - 1] == 1)
etc.

If either of the first two tests evaluates to false then it won't need to look at the non-existent element inputArray[x - 1][y - 1]


Yeah I know that,
Well, if so, why then
Hello I just cant spot a mistake here

Last edited on
Thank you for help. I did spot it a bit later while doing test by using stop mark, but thank you again.
Please do not remove your question when the problem is fixed, other people can't learn from this if they don't know the question.
Please don't do that, @KomRex. As @goldenchicken says, it removes the educative purposes of this site.


Here, for reference is your original code (and original comment!)



Hello I just cant spot a mistake here need a bit of help thank you in advance.
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
#include "GridWindow.h"
#include <iostream>
#include <tbb\tbb.h>
#include <random>
#include <chrono>

using namespace std;
using namespace tbb;


void initialise(Grid& gridArray);
void update(Grid& inputArray, Grid& outputArray);


// Main start point for the programme.  This sets up a 2D array (Grid) and creates the window to display the grid.
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
{
	// *** Create a 2D array (Grid) and call the initialise function to set it's elements
	// Example array size (n x n)
	const int n = 20;

	Grid myArray = Grid(n);
	initialise(myArray);


	// Create and show a window that draws the 2D array.  This example creates a window (400 x 400) pixels in size.
	GridWindow mainWindow = GridWindow(400, 400, myArray, hInstance, iCmdShow);

	// Run the main event loop.  This calls the update function defined below to modify the 2D array elements
	mainWindow.mainLoop(update);

	return 0;
}


// Function called to initialise the 2D grid array.  This uses TBB's parallel_for to setup an example checker-board pattern.
// *** ADD YOUR OWN INITIALISATION CODE TO SET THE APPROPRIATE SQUARES FOR YOUR "GAME OF LIFE" IMPLEMENTATION ***
void initialise(Grid& gridArray) {

	const int n = gridArray.getNumElements();

	parallel_for(

		blocked_range2d<int, int>(0, n, 0, n),

		[&](blocked_range2d<int, int>& range) {

		int yStart = range.rows().begin();
		int yEnd = range.rows().end();

		for (int y = yStart; y < yEnd; y++) {

			int xStart = range.cols().begin();
			int xEnd = range.cols().end();

			for (int x = xStart; x < xEnd; x++) {

				// Set array element to 1 (on) or 0 (off) to create a checker-board pattern.
				gridArray[x][y] = rand() % 2;
			}
		}
	}
	);
}


// Update function - this is called automatically from the window's event loop.  This takes an input array (inputArray) and returns the result of any processing in 'outputArray'.  Here you can set the output array values.

void update(Grid& inputArray, Grid& outputArray) {

	// *** ADD YOUR "GAME OF LIFE" RULES HERE!

	// For now, this example just copies the current (input) array to the output array using a normal serial loop
	for (int y = 0; y < inputArray.getNumElements(); ++y) {

		for (int x = 0; x < inputArray.getNumElements(); ++x) {
			int life = 0;
			//check through Elements 
			
			int Dead = 0;
			int Alive = 1;
			
			if (inputArray[x - 1][y - 1] == 1)
			{
				life++;
			}
			if (inputArray[x - 1][y] == 1)
			{
				life++;
			}
			if (inputArray[x - 1][y + 1] == 1)
			{
				life++;
			}
			if (inputArray[x][y - 1] == 1)
			{
				life++;
			}

			if (inputArray[x][y + 1] == 1)
			{
				life++;
			}
			if (inputArray[x + 1][y - 1] == 1)
			{
				life++;
			}
			if (inputArray[x + 1][y + 1] == 1)
			{
				life++;
			}

			if (inputArray[x+1][y] == 1)
			{
				life++;
			}

			//RULES
			if (inputArray[x][y] == 1 && life < 2) //rule 1
			{

				inputArray[x][y] = Dead;
			}
			else if (inputArray[x][y] == 1 && (life == 2 || life == 3))//rule 2
			{

				inputArray[x][y] = Alive;
			}
			else if (inputArray[x][y] == 1 && life > 3) //rule 3
			{

				inputArray[x][y] = Dead;
			}
			else if (inputArray[x][y] == 0 && life == 3) //rule 4
			{

				inputArray[x][y] = Alive;
			}

			
		}
	}
		
	
			int life ;
		//Updating function
	for (int y = 0; y < inputArray.getNumElements(); ++y) {

		for (int x = 0; x < inputArray.getNumElements(); ++x) {

			outputArray[x][y] = inputArray[x][y];
		}
	}
	}
Last edited on
Topic archived. No new replies allowed.