I was writing a program which takes input from the keyboard and prints a square matrix in the following spiral manner
1 2 3 
8 9 4 
7 6 5 
I managed to write the program but I encountered a weird error. in line 26 it gives me an index out of bound exception
while (matrix[row, col] == 0 && col < matrix.GetLength(0) )
However if I switch the order of the two statements inside the loop the exception is gone ? Does this mean that the order of the two statements in the while loop is important ? And if yes , why ? Shouldn't it be if both statements are true to execute the loop, and if one of them is false, no matter which one, to stop executing it.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpiralMatrixN
{
    class Program
    {
        static void Main(string[] args)
        {
            //prompt the user to enter n
            Console.WriteLine("Enter the value of n");
            int n = int.Parse(Console.ReadLine());
            int[,] matrix = new int[n,n];
            Console.Clear();
            System.Console.SetWindowSize(100, 30);
            int value = 1;
            int col = 0;
            int row = 0;
            if (n>0 && n<21)
            {
                while(value <= n*n)
                {
                    while (matrix[row, col] == 0 && col < matrix.GetLength(0) )
                    {
                        matrix[row, col++] = value;
                        value++;
                    }
                    col--;
                    row++;
                    while (row < matrix.GetLength(1) && matrix[row, col] == 0)
                    {
                        matrix[row++, col] = value;
                        value++;
                    }
                    row--;
                    col--;
                    while (col >= 0 && matrix[row, col] == 0 )
                    {
                        matrix[row, col--] = value;
                        value++;
                    }
                    col++;
                    row--;
                    while (matrix[row, col] == 0 && row >= 0)
                    {
                        matrix[row--, col] = value;
                        value++;
                    }
                    col++;
                    row++;
                }
                for (int i = 0; i < matrix.GetLength(0); i++)
                {
                    for (int j = 0; j < matrix.GetLength(1); j++)
                    {
                        Console.SetCursorPosition(j * 5, i * 2);
                        Console.Write(matrix[i, j] + " ");
                    }
                    Console.WriteLine();
                }
            }
        }
    }
}
                Yes, order is important. Conditions in the && clause are executed in the order of precedence, and if one fails another is not executed. Currently what you have fails because matrix[row, col] == 0 is executed first, and col comes out of bounds. So your check for col (which is absolutely correct btw) should come first:
while (col < matrix.GetLength(0) && matrix[row, col] == 0)
If it fails, second statement won't be executed and you won't have an error. This is called "short-circuit evaluation".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With