Advertisement
Guest User

Untitled

a guest
Nov 13th, 2012
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. /*
  2.  * Unit 6. Loops
  3.  * Exercise 6.14.
  4.  *
  5.  * Write a program that reads a positive integer
  6.  * number N (N < 20) from console and outputs in the
  7.  * console the numbers 1 ... N numbers arranged as a spiral.
  8.  *
  9.  * Author: Pavel Petrov (AN: 9001944)
  10.  *
  11.  */
  12.  
  13. using System;
  14. using System.Collections.Generic;
  15.  
  16. class SpiralMatrix
  17. {
  18.     static void Main()
  19.     {
  20.         // The matrix size
  21.         int n = 0;
  22.  
  23.         int currentValue = 1;  
  24.         int pos = -1;           // Direction  1: ascending, -1: descending
  25.         int row = 0, col = 0;   // Current row, col
  26.  
  27.         // User choice of matrix size
  28.         Console.Write("Enter the matrix size: ");
  29.         bool isInteger = Int32.TryParse(Console.ReadLine(), out n);
  30.         Console.WriteLine();
  31.  
  32.         if (isInteger)
  33.         {
  34.             int[,] matrix = new int[n, n];      // The matrix
  35.             int maxDirections = (n * 2) - 1;    // Total number of moves
  36.             int size = n;                       // Size of current move
  37.             string[] directions = { "up", "right", "down", "left" };   // Directions (for clarity)
  38.  
  39.            
  40.  
  41.             for (int currentDirection = 1; currentDirection <= maxDirections; currentDirection++)
  42.             {
  43.                 if (currentDirection % 2 == 0)
  44.                 {
  45.                     size--;     // Decrease size of next move
  46.                 }
  47.                 else
  48.                 {
  49.                     pos *= -1;  // Change direction of move
  50.                 }
  51.  
  52.                 // Fill the matrix
  53.                 for (int i = 0; i < size; i++)
  54.                 {
  55.                     switch (directions[currentDirection % 4])
  56.                     {
  57.                         case "right":
  58.                         case "left":
  59.                             col += pos;
  60.                             matrix[row, col - 1] = currentValue;
  61.                             break;
  62.                         case "down":
  63.                         case "up":
  64.                             row += pos;
  65.                             matrix[row, col - 1] = currentValue;
  66.                             break;
  67.                     }
  68.  
  69.                     // Next member
  70.                     currentValue++;
  71.                 }
  72.             }
  73.  
  74.             // Print the matrix
  75.             for (int i = 0; i < n; i++)
  76.             {
  77.                 Console.WriteLine();
  78.                 for (int j = 0; j < n; j++)
  79.                 {
  80.                     Console.Write("{0,4}", matrix[i, j]);
  81.  
  82.                 }
  83.                 Console.WriteLine();
  84.             }
  85.  
  86.             Console.WriteLine();
  87.         }
  88.         else
  89.         {
  90.             Console.WriteLine("The matrix size must be a integer number only!!!");
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement