C # Array index error outside borders

Full disclosure . This is for homework.

It drives me crazy. I am writing a discrete cosine transform function and filling it completely, but I am throwing an IndexOutOfRange exception.

Code below:

static int[][] DiscreteCosineTransform(int[][] pIn) { int[][] cosP = pIn; double SumCosP = 0; double Cx = 0; double Cy = 0; Console.WriteLine("Discrete Cosine Transformed Array:"); for(int i = 0; i < 8; i++) { if (i == 0) Cx = 1 / Math.Sqrt(2); else Cx = 1; for(int j = 0; j < 8; i++) { if (j == 0) Cy = 1 / Math.Sqrt(2); else Cy = 1; for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { SumCosP += cosP[x][y] * Math.Cos(((2 * x + 1) * i * Math.PI) / 16) * Math.Cos(((2 * y + 1) * j * Math.PI) / 16); } } pIn[i][j] = (int)(0.25 * Cx * Cy * SumCosP); Console.Write(" " + pIn[i][j] + " "); } Console.WriteLine(); } Console.WriteLine(); return pIn; } 

Where pIn:

 int[][] P = new int[][] { new int[]{10,10,10,10,10,10,10,10}, new int[]{10,20,20,20,20,20,20,10}, new int[]{10,20,30,30,30,30,20,10}, new int[]{10,20,30,40,40,30,20,10}, new int[]{10,20,30,40,40,30,20,10}, new int[]{10,20,30,30,30,30,20,10}, new int[]{10,20,20,20,20,20,20,10}, new int[]{10,10,10,10,10,10,10,10} }; 
+6
c #
source share
3 answers

This line

 for(int j = 0; j < 8; i++) 

must read

 for(int j = 0; j < 8; j++) ^ 
+15
source share

You did it:

 for(int j = 0; j < 8; i++) 

And, most likely, it meant:

 for(int j = 0; j < 8; j++) 

(You did i ++, not j ++.)

+3
source share

change i to j in this for (int j = 0; j < 8; i++) line for (int j = 0; j < 8; i++)

+1
source share

All Articles