Game of Life - C++ help

I'm in the first foundations of CS class in university so please be patient with me :)

I can't seem to figure out why this code is outputting gibberish symbols. The random array comes out fine but when the program runs through and starts to change things based on the rules I put in place it gives me weird symbols that are not '*' or ' '. I feel like I've verified that I'm not searching outside of my array but maybe I've missed something.

Also, we haven't learned how to make functions yet.

Hopefully someone can 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
#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{
	//NUMBER OF ROWS AND COLUMNS
	const char LIVE = '*', DEAD = ' ';
	const int ROWS = 7;
	const int COLS = 10;
	char life[ROWS][COLS];
	char life1[ROWS][COLS];
	int ans=0;

	//RANDOM NUMBER GENERATOR TO CREATE INITIAL GENERATION
	srand (time(NULL));
	int cell;

	for(int r=0; r<ROWS; r++)
	{
		for(int c =0; c<COLS;c++)
		{
			cell = rand() % 7;
			if(cell >= 5)
			{
				life[r][c] = LIVE;
			}
			else
			{
				life[r][c]=DEAD;
			}
		}
	}
	for(int r = 0; r < ROWS; r++)
	{
		for (int c = 0; c < COLS; c++)
		{
			cout << life[r][c] << " ";
		}
		cout << endl;
	}
for(int k = 0; k < 10; k++)
{
	for(int r = 0; r < ROWS; r++)
	{
		for (int c = 0; c < COLS; c++, ans=0)
		{
			if(life[r][c] == LIVE)
			{
				if((c-1)>=0 && (life[r][c-1] == LIVE))
					ans++;
				if(c<COLS && (life[r][c+1] == LIVE))
					ans++;
				if(c<COLS && r<ROWS && (life[r+1][c+1] == LIVE))
					ans++;
				if((r<ROWS) &&(life[r+1][c]) == LIVE)
					ans++;
				if(r<ROWS && c>=0 && (life[r+1][c-1]) == LIVE)
					ans++;
				if(r>=0 && c>=0 && (life[r-1][c-1] == LIVE))
					ans++;
				if(r>=0 && (life[r-1][c] == LIVE))
					ans++;
				if(r>=0 && c<COLS && (life[r-1][c+1] == LIVE))
					ans++;
				if(ans==2 || ans == 3)
					life1[r][c] = LIVE;
				if(ans>3)
					life1[r][c] = DEAD;
				if(ans<2)
					life1[r][c] = LIVE;
			}
			else
			{
				if (life[r][c] == DEAD)
				{
                if(c>=0 && (life[r][c-1] == DEAD))
                    ans++;
                if(c<COLS && (life[r][c+1] == DEAD))
                    ans++;
                if(r<ROWS && c<COLS && (life[r+1][c+1] == DEAD))
                    ans++;
                if(r<ROWS && (life[r][c] == life[r+1][c]))
                    ans++;
                if(r<ROWS && c>0 && (life[r][c] == life[r+1][c-1]))
                    ans++;
                if(r>=0 && c>=0 && (life[r][c] == life[r-1][c-1]))
                    ans++;
                if(r>=0 && (life[r][c] == life[r-1][c]))
                    ans++;
                if(r>=0 && c<COLS && (life[r][c] == life[r-1][c+1]))
                    ans++;
                if (ans==3)
                    life[r][c] = LIVE;
				}
			}
		}
	}
	for(int r = 0; r < ROWS; r++)
	{
		for (int c = 0; c < COLS; c++)
		{
			life[r][c]=life1[r][c];
		}
	}

	for(int r = 0; r < ROWS; r++)
	{
		for (int c = 0; c < COLS; c++)
		{
			cout << life[r][c] << " ";
		}
		cout << endl;
	}
}
	return 0;

}
I don't know whether this will help, but you could try setting the initial value of every cell in the array life1 to DEAD. I think the strange characters are uninitialised data.
Worked! ThanksSoMuch!
Topic archived. No new replies allowed.