Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
Iam facing this problem Index outside the bounds of array.
Even if i increase the size of array, this problem still persist.

C#
public partial class corelation_test : Form
   {
       public corelation_test()
       {
           InitializeComponent();
       }
       int[] x_prev = new int[2000000];
       int[] y_prev = new int[2000000];

       public corelation_test(int [] x,int [] y)
       {
           InitializeComponent();

           x.CopyTo(x_prev,0);
           y.CopyTo(y_prev,0);

       }
              int count = 0;      
              double[] min_x = new double[20000000];
              double[] min_y = new double[20000000];
                int k = 0;
               double [] min = new double [20000000];
do
 {
  for (double i = (x_prev[count] - 10.5); i<(x_prev[count] + 10.5); i++)
  {
  for (double j = (y_prev[count] - 10.5); j<(y_prev[count] + 10.5); j++)
  {
  min[k]=Math.Pow (((x_prev [count]+11)-(x_prev [count ]+i+21))+((y_prev [count ]+11)-(y_prev [count ]+j+21)),2);        
k++;
   }
   }

Error is in array min[k].
While project gets build it stops at line :
  min[k]=Math.Pow (((x_prev [count]+11)-(x_prev [count ]+i+21))+((y_prev [count ]+11)-(y_prev [count ]+j+21)),2);
Posted
Comments
OriginalGriff 18-Jan-11 4:18am    
Don't use the "Add an Answer" buutton to reply - use the "Add Comment" feature instead.
That way, the person concerned gets an email and may respond more quickly. Adding an answer emails nobody except yourself!
I have moved your comment and deleted your answer.
Arpita1086 18-Jan-11 4:29am    
ok.. Thanks.

Have a look at k by adding watch on that variable or you should also be able to see it when the error is given and you go over it with the mouse. The value is probably out of range of the array so you should have another look at how that value is determined.

By the way.You have a nested for loop that probably is producing a lot more than you expect. Determine how much data is generated instead of guessing a fixed size. Another option would be to use a container class that can grow dynamically.

Good luck!
 
Share this answer
 
v2
Comments
OriginalGriff 18-Jan-11 4:16am    
The OP wrote:
"Thanks for you reply..

Can i store the values in a file and then culculate the minimum values, instead of using an array.
K value is going out of bound.But i need to store all values for calculating minimum.Can i use some other data structure instaed of array?"
E.F. Nijboer 18-Jan-11 4:27am    
Could you explain what you mean by calculating minimum? If you would like to find the 10 lowest values for example it isn't necessary to store all the values at all. The size of the "min" array can then simply be 10. At the start you fill it with the first 10 values and a value in the "min" array is replaced if the current value is smaller that the value in the array. That would save an enormous amount of memory consumption.
Otherwise you could use a List: http://dotnetperls.com/list
A file or even a temporary database table could maybe also be used.
Without actually running this through the debugger it is difficult to tell you what the problem is - and I can't run it throughthe debugger because it won't compile: your do loop is not terminated with a while and is outside all methods anyway.

Replace your line with several.
Create temporary values for each array access and put them on separate lines.
Then when the error occurs , you can tell which array access is giving the problem, and can look at the value, and how it got to be that large or negative.
 
Share this answer
 
The value of k became large than or equal 20000000. You can set a condition so see this.

for (double i = (x_prev[count] - 10.5); i<(x_prev[count] + 10.5); i++)
{
for (double j = (y_prev[count] - 10.5); j<(y_prev[count] + 10.5); j++)
{
min[k]=Math.Pow (((x_prev [count]+11)-(x_prev [count ]+i+21))+((y_prev [count ]+11)-(y_prev [count ]+j+21)),2);
k++;
if(k>=20000000)
break;
}
if(k>=20000000)
break;
}

by doing this, your program will not crash but your demands will not full fill.
You also should give the value of x_prev[count] & full do while loop. Then i can understard why the value of k is increasing so much.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900