Advertisement
Guest User

Fill the Matrix

a guest
Nov 23rd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MultiDimensionalArrays
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int inputNum = int.Parse(Console.ReadLine());
  14.             string inputChar = Console.ReadLine();
  15.  
  16.             int[,] array = new int[inputNum, inputNum];
  17.  
  18.             switch (inputChar)
  19.             {
  20.                 case "a": VerticalMatrix(array, inputNum); break;
  21.                 case "b": HorizontalMatrix(array, inputNum); break;
  22.                 case "c": DiagonalMatrix(array, inputNum); break;
  23.                 case "d": break;
  24.                 default: Console.WriteLine("Invalid input!"); break;
  25.             }
  26.             PrintMatrix(inputNum, array);
  27.            
  28.         }
  29.         public static void VerticalMatrix(int[,] array, int num)
  30.             {
  31.            
  32.             int counter = 1;
  33.             for (int cols = 0; cols < num; cols++)
  34.             {
  35.                 for (int rows = 0; rows < num; rows++)
  36.                 {
  37.                     array[rows, cols] = counter;
  38.                    
  39.                     counter++;
  40.                 }                
  41.             }            
  42.             }
  43.  
  44.         public static void HorizontalMatrix(int[,] array, int num)
  45.         {
  46.             int counter = 1;
  47.  
  48.             for (int cols = 0; cols < num; cols++)
  49.             {
  50.                 if (cols % 2 == 0)
  51.                 {
  52.                     for (int rows = 0; rows < num; rows++)
  53.                     {
  54.                         array[rows, cols] = counter;
  55.                         counter++;
  56.                     }
  57.                 }
  58.                 else
  59.                 {
  60.                     for (int rows = num - 1; rows >= 0; rows--)
  61.                     {
  62.                         array[rows, cols] = counter;
  63.                         counter++;
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.         public static void DiagonalMatrix(int[,] array, int num)
  69.         {
  70.             int counter = 1;
  71.             int rows = 0;
  72.             int cols = 0;
  73.             int currentElements = 1;
  74.  
  75.             //first part very good
  76.             for (int diagonal = num - 1; diagonal > 0; diagonal--)
  77.             {
  78.                 rows = diagonal;
  79.                 cols = 0;
  80.                 for (int elements = 0; elements < currentElements; elements++)
  81.                 {
  82.                     array[rows, cols] = counter;
  83.                     counter++;
  84.                     rows++;
  85.                     cols++;
  86.                 }
  87.                 currentElements++;
  88.             }
  89.             //second part i no like
  90.             for (int diagonal = 0; diagonal < num; diagonal++)
  91.             {
  92.                 rows = 0;
  93.                 cols = diagonal;
  94.                 for (int elements = 0; elements < currentElements; elements++)
  95.                 {
  96.                     array[rows, cols] = counter;
  97.                     counter++;
  98.                     rows++;
  99.                     cols++;
  100.                 }
  101.                 currentElements--;
  102.             }
  103.         }
  104.  
  105.         public static void PrintMatrix(int num, int[,] array)
  106.         {
  107.             for (int rows = 0; rows < num; rows++)
  108.             {
  109.                 for (int cols = 0; cols < num; cols++)
  110.                 {
  111.                     if (cols == num - 1)
  112.                     {
  113.                         Console.Write("{0}", array[rows, cols]);
  114.                     }
  115.                     else
  116.                     {
  117.                         Console.Write("{0} ", array[rows,cols]);
  118.                     }
  119.                 }
  120.                 Console.WriteLine();
  121.             }
  122.         }        
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement