6

Full disclosure: This is for a homework assignment.

This is driving me nuts. I'm writing a Discrete Cosine Transform function and have it basically complete, but I'm running into an IndexOutOfRange exception.

Code is 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 is:

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}
            };
1
  • 2
    What line of code is throwing the exception? Mar 5, 2010 at 3:46

3 Answers 3

15

This line

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

should read

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

                      ^
3
  • +1 ... D'oh! Well, these things happen. Thanks for helping me fix my silly mistake!
    – Riddari
    Mar 5, 2010 at 3:56
  • 4
    @MachinationX: Don't just fix the mistake. Ask yourself "what factor caused me to make this mistake and how can I never, ever do it again?" The factor that led to the mistake was the use of meaningless and visually almost-identical variable names i and j. Can you come up with names for i, j, x and y which are descriptive and less likely to be easily confused for each other? Mar 5, 2010 at 7:43
  • I agree with Eric. Variables called i,j,x and y make it so much more difficult to spot a problem like this, especially when i and j look very similiar in fixed-width fonts like courier. If you can give them short, relevent names you'd have found this issue before needing to post :) Best of luck with your project/hw however.
    – dooburt
    Mar 5, 2010 at 11:28
3

You did:

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

And most likely meant:

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

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

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.