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();

Recommended Answers

All 4 Replies

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???

He meant to say puzzle[i, j] = m. He was probably tired.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.