Help with 2D Array

I am trying to do some math to values inside a 2D array. What I ultimately want to do is take the average of a "block" of the array, calculate average, and output to one of the registers within the array; this will be used to approximate voltage distribution within a grid.

The problem I am having is that my voltage [y][x] does not seem to register when doing any calculations. If I replace the y,x with specific numbers (coordinates within array) then the operations work fine for that location. See output below. I tried replacing voltage [y][x] = average with voltage [y][x] ++ for testing, and it seems to perform the action once only. It will increment all voltage [y][x] values by 1, but will not perform this action again on the next cycle.

Any advise?

Here is the 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
int x, y, cycle;
double voltage[11][21]={0}, sum, average;

report.open ("report1.txt");
if (report.fail())
{ 
	cerr << "fail to open reporting file" << endl;
	exit (1);
}
cout << "report opened successfully" << endl;

// Sets initial Condition

for (cycle=1; cycle<=10; cycle++)
{

for (y=1; y<=9; y++)
{
for (x=1; x<=19; x++)
{
voltage[y][x] = 5.0 *( (x>=8) && (x<=12) && (y>=4) && (y<=6) );

// Start calculating averages

sum = ((voltage [++y][--x]) + (voltage [y][++x]) + (voltage [y][++x]) + (voltage [--y][x]) + (voltage [--y][x]) + (voltage [y][--x])  + (voltage [y][--x]) + (voltage [++y][x]));
++x;
average = (sum/8);
voltage [y][x] = average;

}
}
}

Data is then outputed to a file... (code not included)

Output:

000000000000000000000
000000000000000000000
000000000000000000000
000000000000000000000
000000005555500000000
000000005555500000000
000000005555500000000
000000000000000000000
000000000000000000000
000000000000000000000
000000000000000000000
bump.
The post/pre increment/decrement (++x,--y,etc) also modify x and y, you should consider switching to x/y +/-1 just to make it less confusing.

Also, what's the point of running 10 cycles if voltage[y][x] = 5.0 *( (x>=8) && (x<=12) && (y>=4) && (y<=6) ); resets them to the same values everytime?
Topic archived. No new replies allowed.