trouble summing parts of a 2D array

I am having issues summing up the bottom parts of a 24 x 24 array.
The issue is with wards 3 and 4
as well as towns 7, 8, and 9.

this is my output to file.

Census results for Squaresville:

Ward 1: 665397
Ward 2: 641032
Ward 3: -858323989
Ward 4: -1717371729

Township 1: 288885
Township 2: 266309
Township 3: 302098
Township 4: 346256
Township 5: 295756
Township 6: 289329
Township 7: 1718305072
Township 8: 1718270537
Township 9: 1718259915

Thank you for any help you can provide.

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
  
#include <iostream>
#include <fstream>
using namespace std;

void loadData(int census [24][24]);
void calcWards(int census [24][24], int wards [2][2]);
void calcTowns(int census [24][24], int towns [3][3]);
void outputData(int wards [2][2], int towns[3][3]);

int main()
{
	int census[24][24];
	int wards[2][2];
	int towns[3][3];

	loadData(census);
	calcWards(census, wards);
	calcTowns(census, towns);
	outputData(wards, towns);

	return 0;
}

void loadData(int census[24][24])
{
	
	ifstream inFile;
	inFile.open ("census_data.txt");
	for (int r = 0; r < 25; r++)
	{
		for (int c = 0; c < 25; c++)
		{
			inFile >> census[r][c];
		}
	}

	inFile.close();
}

void calcWards(int census[24][24], int wards[2][2])
{
	
	int sum1 = 0;
	int sum2 = 0;
	int sum3 = 0;
	int sum4 = 0;

	for (int r = 0; r < 12; r++)
	{
		for (int c = 0; c < 12; c++)
		{
			sum1 = sum1 + census[r][c];
		}
	}

	for (int r = 0; r < 12; r++)
	{
		for (int c = 12; c < 24; c++)
		{
			sum2 = sum2 + census[r][c];
		}
	}

	for (int r = 12; r < 24; r++)
	{
		for (int c = 0; c < 12; c++)
		{
			sum3 = sum3 + census[r][c];
		}
	}

	for (int r = 12; r < 24; r++)
	{
		for (int c = 12; c < 24; c++)
		{
			sum4 = sum4 + census[r][c];
		}
	}

	wards[0][0] = sum1;
	wards[0][1] = sum2;
	wards[1][0] = sum3;
	wards[1][1] = sum4;
}


void calcTowns(int census[24][24], int towns[3][3])
{
	int sum1 = 0;
	int sum2 = 0;
	int sum3 = 0;
	int sum4 = 0;
	int sum5 = 0;
	int sum6 = 0;
	int sum7 = 0;
	int sum8 = 0;
	int sum9 = 0;

	for (int r = 0; r < 8; r++)
	{
		for (int c = 0; c < 8; c++)
		{
			sum1 = sum1 + census[r][c];
		}
	}

	for (int r = 0; r < 8; r++)
	{
		for (int c = 8; c < 16; c++)
		{
			sum2 = sum2 + census[r][c];
		}
	}

	for (int r = 0; r < 8; r++)
	{
		for (int c = 16; c < 24; c++)
		{
			sum3 = sum3 + census[r][c];
		}
	}

	for (int r = 8; r < 16; r++)
	{
		for (int c = 0; c < 9; c++)
		{
			sum4 = sum4 + census[r][c];
		}
	}

	for (int r = 8; r < 16; r++)
	{
		for (int c = 8; c < 16; c++)
		{
			sum5 = sum5 + census[r][c];
		}
	}
	for (int r = 8; r < 16; r++)
	{
		for (int c = 16; c < 24; c++)
		{
			sum6 = sum6 + census[r][c];
		}
	}
	for (int r = 16; r < 24; r++)
	{
		for (int c = 0; c < 9; c++)
		{
			sum7 = sum7 + census[r][c];
		}
	}
	for (int r = 16; r < 24; r++)
	{
		for (int c = 8; c < 16; c++)
		{
			sum8 = sum8 + census[r][c];
		}
	}
	for (int r = 16; r < 24; r++)
	{
		for (int c = 16; c < 24; c++)
		{
			sum9 = sum9 + census[r][c];
		}
	}
	
	towns[0][0] = sum1;
	towns[0][1] = sum2;
	towns[0][2] = sum3;
	towns[1][0] = sum4;
	towns[1][1] = sum5;
	towns[1][2] = sum6;
	towns[2][0] = sum7;
	towns[2][1] = sum8;
	towns[2][2] = sum9;
}


void outputData(int wards[2][2], int towns[3][3])
{
	ofstream outFile;
	outFile.open ("a1.txt");

	outFile << " Census results for Squaresville:" << endl;
	outFile << endl;

	outFile << " Ward 1: " << wards[0][0] << endl;
	outFile << " Ward 2: " << wards[0][1] << endl;
	outFile << " Ward 3: " << wards[1][0] << endl;
	outFile << " Ward 4: " << wards[1][1] << endl;

	outFile << endl;

	outFile << " Township 1: " << towns[0][0] << endl;
	outFile << " Township 2: " << towns[0][1] << endl;
	outFile << " Township 3: " << towns[0][2] << endl;
	outFile << " Township 4: " << towns[1][0] << endl;
	outFile << " Township 5: " << towns[1][1] << endl;
	outFile << " Township 6: " << towns[1][2] << endl;
	outFile << " Township 7: " << towns[2][0] << endl;
	outFile << " Township 8: " << towns[2][1] << endl;
	outFile << " Township 9: " << towns[2][2] << endl;

	outFile.close();
}Put the code you need help with here.
I would direct your attention to lines 30 and 32.

What are valid indexes for an array of size 24? You are attempting to read 625 values from a file that probably only contains 576, as well as store many of those values in memory that is outside the bounds of your array.
Thank you so much.

It works great now

Thanks again.
Topic archived. No new replies allowed.