Hello everyone,
I'm required to print a puzzle which in the form of row*col. And my code can do that except when row and col are the same number. How can I fix that? And another question is, how can I store these values into a 2D array??? Because later on, I need to modify the position inside the puzzle.
Please help me. Many thanks!!!!!!

Console.WriteLine("size:");
            int row = int.Parse(Console.ReadLine());
            int col = int.Parse(Console.ReadLine());
            int m = 1;

            for (int i = 0; i < row; i++)
                for (int j = 0; j < col; j++)
                {
                    if (j == row)
                        Console.WriteLine(m+" ");
                    else
                        Console.Write(m+" ");
                    m++;
                }
                Console.ReadLine();

my code can do that except when row and col are the same number. How can I fix that?

Line 9: if (j + 1 == col)

how can I store these values into a 2D array

Add declaration: int[,] puzzle = new int[row, col]; Then add a new line after line 12: puzzle[row, col] = m; HTH

"Then add a new line after line 12: puzzle[row, col] = m;"
This line causes "IndexOutOfRangeException". How can I fix this???

Yes, the puzzle matrix should be indexed with i and j, not row and col which are "static" values. My mistake :$

This article has been dead for over six months. Start a new discussion instead.