"An Access Violation error"

Ok, so i have read about it on the web before posting this and i have changed it time and time and it still does not work. I am using Dev-C++ (as my instructor insisted) and it complies correctly but does not run. Here is the part of my code that does not work:

int main(){

float plotPoints [20][20];

float value = 0.0;

for ( int y = 0; y <= 20; y++ ){
for ( int x = 0; x <= 20; x++ ){

x = x - 10;
y = y - 10;

value = ( sqrt( pow( x, 2.0 ) + pow( y, 2.0 ) ) + 0.001 );

plotPoints [x][y] = ( (sin(value)) / value ); // THE ERROR OCCURS HERE
}
}

for ( int y = 0; y <= 20; y++ ){
for ( int x = 0; x <= 20; x++ ){
cout << plotPoints [x][y];
}
}

return 0;
}

i know it is a issue with the pointer and memory, i just dont understand how to fix it.

Thanks
Use the code wrapper please.

You cannot address negative array values. If you need to use your indices for calculation, do not overwrite them!

1
2
3
4
5
6
7
8
for ( int y = 0; y <= 20; y++ ){
  for ( int x = 0; x <= 20; x++ ){
    value = ( sqrt( pow( x-10, 2.0 ) + pow( y-10, 2.0 ) ) + 0.001 );

...
  }
}
It works now.
Thank you so much turbozedd.
Btw for future reference:

x = x -10
can be shortened using the shortcut operator -=
x -= 10

This works for +,-,*,/, and %
Even if the problem with accessing arrays with negative values didn't exist in the program, which turbo pointed out you'd still have a continuous for loop with no chance of exit. The x variable used for the for loop is initialized to 0, and immediately changed to -10. If the program didn't crash, how would the loop ever exit?
And if x and y are not manipulated within the body of the for loop, the indices would attempt to access beyond the boundaries of plotPoints.
Topic archived. No new replies allowed.