debug assertion failed
Nov 12, 2016 at 1:59pm UTC
Hello everyone,
I have been learning c++ for a week now and I'm doing an assignment where I have to make a cellular automat where each cell updates depending on its two neigbors.
Each cells state is contained in a vector with either state 1 or 0.
however when I try to run this code it says debug assertion failed. I know this is either some kind of logic error or I'm using elements of the vector that don't exist, but I can't seem to find it.
I have been messing around with the vector lengths and the maximum i and k values. Is there something I'm missing not seeing/understanding?
I would greatly appreciate if someone could point out where my error is
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
int main()
{
std::vector<int > vect(250);
vect[0] = 1;
for (int i = 1; i <= 249; ++i) {
vect[i] = 0;
}
for (int i = 0; i <= 249; ++i) {
std::cout << vect[i];
}
std::cout<< std::endl;
for (int t = 0; t < 1; ++t){
for (int k = 0; k < 250; ++k){
if (0 < k < 249){
if (vect[k - 1] == 1 && vect[k] == 1 && vect[k + 1] == 1)
vect[k] = 0;
else {if (vect[k - 1] == 1 && vect[k] == 1 && vect[k + 1] == 0)
vect[k] = 0;
else {if (vect[k - 1] == 1 && vect[k] == 0 && vect[k + 1] == 1)
vect[k] = 0;
else {if (vect[k - 1] == 1 && vect[k] == 0 && vect[k + 1] == 0)
vect[k] = 1;
else {if (vect[k - 1] == 0 && vect[k] == 1 && vect[k + 1] == 1)
vect[k] = 1;
else {if (vect[k - 1] == 0 && vect[k] == 1 && vect[k + 1] == 0)
vect[k] = 1;
else {if (vect[k - 1] == 0 && vect[k] == 0 && vect[k + 1] == 1)
vect[k] = 1;
else {if (vect[k - 1] == 0 && vect[k] == 0 && vect[k + 1] == 0)
vect[k] = 0;
}}}}}}}}else {
if (k == 0) {
if (vect[249] == 1 && vect[k] == 1 && vect[k + 1] == 1)
vect[k] = 0;
else {if (vect[249] == 1 && vect[k] == 1 && vect[k + 1] == 0)
vect[k] = 0;
else {if (vect[249] == 1 && vect[k] == 0 && vect[k + 1] == 1)
vect[k] = 0;
else {if (vect[249] == 1 && vect[k] == 0 && vect[k + 1] == 0)
vect[k] = 1;
else {if (vect[249] == 0 && vect[k] == 1 && vect[k + 1] == 1)
vect[k] = 1;
else {if (vect[249] == 0 && vect[k] == 1 && vect[k + 1] == 0)
vect[k] = 1;
else {if (vect[249] == 0 && vect[k] == 0 && vect[k + 1] == 1)
vect[k] = 1;
else {if (vect[249] == 0 && vect[k] == 0 && vect[k + 1] == 0)
vect[k] = 0;
}}}}}}}}
else {
if (k == 249) {
if (vect[k - 1] == 1 && vect[k] == 1 && vect[0] == 1)
vect[k] = 0;
else {if (vect[k - 1] == 1 && vect[k] == 1 && vect[0] == 0)
vect[k] = 0;
else {if (vect[k - 1] == 1 && vect[k] == 0 && vect[0] == 1)
vect[k] = 0;
else {if (vect[k - 1] == 1 && vect[k] == 0 && vect[0] == 0)
vect[k] = 1;
else {if (vect[k - 1] == 0 && vect[k] == 1 && vect[0] == 1)
vect[k] = 1;
else {if (vect[k - 1] == 0 && vect[k] == 1 && vect[0] == 0)
vect[k] = 1;
else {if (vect[k - 1] == 0 && vect[k] == 0 && vect[0] == 1)
vect[k] = 1;
else {if (vect[k - 1] == 0 && vect[k] == 0 && vect[0] == 0)
vect[k] = 0;
}}}}}}}}
} } } } }
Nov 12, 2016 at 3:02pm UTC
One definite problem is here at line 14:
It should be written with two separate conditions:
The original version would evaluate as
which would always be
true
.
Nov 13, 2016 at 6:29pm UTC
I see, thanks for pointing that out
however this still does not solve the problem with the assertion failure
Nov 13, 2016 at 7:11pm UTC
Well, can you give any more information - do you know at which line the problem occurs, or any more information at all about the error you are getting.
Nov 13, 2016 at 7:12pm UTC
After I applied Chervil's fix the programs runs fine on VS2015.
Output:
1 2 3 4
100000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000
Nov 13, 2016 at 7:32pm UTC
Perhaps my previous post was confusing.
This is the fix for line 14:
Topic archived. No new replies allowed.