Click here to Skip to main content
15,921,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Is it wrong?
If not why is there an error in the last line?
error:Index was outside the bounds of the array.

C#
const int n = 5;
        int[,] next=new int[n,n];

            for(int i=0;i<=n;i++)
                for (int j = 0; j <= n; j++)
                    next[i,j]= -1;
Posted
Comments
Sugato Pal 21-Jul-11 9:33am    
Hi
If your problem is solved then please accept the solution.
Cheers

You are creating a two dimensional array, with the first dimension holding an array of 5 integers, but if you use i = 0; i <= 5 you actually create 6 integers (starts with 0), and so basically, you are trying to fit 6 integers into an array that was declared to hold only 5. Either declare n = 6, or use i = 0; i < 5 in your loops. Hope this helps.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jul-11 22:33pm    
Correct, a 5.
--SA
I think
C#
for(int i=0;i<=n;i++)
                for (int j = 0; j <= n; j++)

will be
C#
for(int i=0;i<n;i++)
                for (int j = 0; j < n; j++)


For 0 to 5 means its taking value for 6 times .But array is declared with max size of 5.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 21-Jul-11 9:49am    
My 5!
Proposed as solution!
Sugato Pal 22-Jul-11 4:44am    
--Thanks MRB.
Sergey Alexandrovich Kryukov 22-Jul-11 22:22pm    
Correct, a 5.
--SA
Sugato Pal 28-Jul-11 8:58am    
Thanks SA.

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