Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a program that fills and prints a matrix of size (n, n) as shown below:
- //Example for n=4:
- // a) b) c) d)*
- // 1 5 9 13 1 8 9 16 7 11 14 16 1 12 11 10
- // 2 6 10 14 2 7 10 15 4 8 12 15 2 13 16 9
- // 3 7 11 15 3 6 11 14 2 5 9 13 3 14 15 8
- // 4 8 12 16 4 5 12 13 1 3 6 10 4 5 6 7
- using System;
- namespace FillTheMatrix
- {
- class FillTheMatrix
- {
- static void Main()
- {
- Console.WriteLine("Please enter n.");
- int n = Int32.Parse(Console.ReadLine());
- int[,] filledMatrix = FillMatrixA(n);
- Console.WriteLine("Option A:");
- Console.WriteLine();
- PrintMatrix(filledMatrix);
- Console.WriteLine("Option B:");
- Console.WriteLine();
- filledMatrix = FillMatrixB(n);
- PrintMatrix(filledMatrix);
- Console.WriteLine("Option D:");
- Console.WriteLine();
- filledMatrix = FillMatrixD(n);
- PrintMatrix(filledMatrix);
- }
- static int[,] FillMatrixA(int n)
- {
- int[,] matrixA = new int[n, n];
- int counter = 1;
- for (int col = 0; col < n; col++)
- {
- for (int row = 0; row < n; row++)
- {
- matrixA[col, row] = counter;
- counter++;
- }
- }
- return matrixA;
- }
- static int[,] FillMatrixB(int n)
- {
- int[,] matrixB = new int[n, n];
- int counterEven = 0;
- int multiplyNBy = 1;
- int counterOdd = n * 2;
- for (int col = 0; col < n; col++)
- {
- if (col % 2 != 0)
- {
- counterEven = counterOdd;
- }
- for (int row = 0; row < n; row++)
- {
- if ((col == 0) || (col % 2 == 0))
- {
- counterEven++;
- matrixB[col, row] = counterEven;
- }
- else
- {
- matrixB[col, row] = counterOdd;
- counterOdd--;
- }
- }
- multiplyNBy++;
- counterOdd = n * multiplyNBy;
- }
- return matrixB;
- }
- static int[,] FillMatrixD(int n)
- {
- int[,] matrixD = new int[n, n];
- int[,] beenThereDoneThat = new int[n, n];
- string direction = "down";
- int directionCounter = 1;
- int counter = 2;
- int row = 0;
- int col = 0;
- matrixD[row, col] = 1;
- row++;
- while (counter <= n * n)
- {
- switch (directionCounter)
- {
- case 1: direction = "down";
- break;
- case 2: direction = "right";
- break;
- case 3: direction = "up";
- break;
- case 4: direction = "left";
- break;
- default: directionCounter = 0;
- break;
- }
- if (direction == "down")
- {
- if (matrixD[row, col] == 0)
- {
- matrixD[row, col] = counter;
- counter++;
- }
- if (row < n -1)
- {
- row++;
- }
- else
- {
- directionCounter = 2;
- col++;
- }
- }
- else if (direction == "right")
- {
- if (matrixD[row, col] == 0)
- {
- matrixD[row, col] = counter;
- counter++;
- }
- if (col < n -1)
- {
- col++;
- }
- else
- {
- directionCounter = 3;
- row = col;
- }
- }
- else if (direction == "up")
- {
- if (matrixD[row, col] == 0)
- {
- matrixD[row, col] = counter;
- counter++;
- }
- if (row > 0)
- {
- row--;
- }
- else
- {
- directionCounter = 4;
- }
- }
- else if (direction == "left")
- {
- if (matrixD[row, col] == 0)
- {
- matrixD[row, col] = counter;
- counter++;
- }
- if (col > 0)
- {
- col--;
- }
- else
- {
- directionCounter = 1;
- }
- }
- }
- return matrixD;
- }
- static void PrintMatrix(int[,] filledMatrix)
- {
- for (int row = 0; row < filledMatrix.GetLength(1); row++)
- {
- for (int col = 0; col < filledMatrix.GetLength(0); col++)
- {
- Console.Write(filledMatrix[col, row] + " ");
- }
- Console.Write(Environment.NewLine + Environment.NewLine);
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement