Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using static System.Math;
  7.  
  8.  
  9. namespace MyProgram
  10. {
  11. public class Program
  12. {
  13. public static void Main(string[] args)
  14. {
  15. Console.Write("Enter number of rows:");
  16. int n = int.Parse(Console.ReadLine());
  17. Console.Write("Enter number of columns:");
  18. int m = int.Parse(Console.ReadLine());
  19. int[] arr1 = new int[n * m];
  20. int[,] arr2 = new int[n, m];
  21. int choice;
  22. do
  23. {
  24. Console.WriteLine("Select option:");
  25. Console.WriteLine("t1: Diagonal");
  26. Console.WriteLine("t2: Spiral");
  27. Console.WriteLine("t3: Exit");
  28. Console.Write("Your selection: ");
  29. choice = int.Parse(Console.ReadLine());
  30. switch (choice)
  31. {
  32. case 1:
  33. {
  34. SetArray(arr2);
  35. PrintArray(arr2);
  36. Diagonal(arr2, arr1);
  37. PrintArray(arr1);
  38. break;
  39. }
  40. case 2:
  41. {
  42. SetArray(arr2);
  43. PrintArray(arr2);
  44. Spiral(arr2, arr1);
  45. PrintArray(arr1);
  46. break;
  47. }
  48. }
  49. Console.WriteLine();
  50. Console.WriteLine();
  51. } while (choice != 5);
  52. }
  53. static void Diagonal(int[,] array2, int[] array1)
  54. {
  55. int k = 0;
  56. int row = 0;
  57. int col = 0;
  58. while (k < array1.Length)
  59. {
  60.  
  61. array1[k] = array2[row, col];
  62. if ((row + col) % 2 == 0)
  63. {
  64. if ((row == 0) && (col != array2.GetLength(1) - 1)) { col++; }
  65. else
  66. {
  67. if (col == array2.GetLength(1) - 1) { row++; }
  68. else { row--; col++; }
  69. }
  70. }
  71. else
  72. {
  73. if ((col == 0) && (row != array2.GetLength(0) - 1)) { row++; }
  74. else
  75. {
  76. if (row == array2.GetLength(0) - 1) { col++; }
  77. else { row++; col--; }
  78. }
  79.  
  80. }
  81. k += 1;
  82. }
  83. }
  84.  
  85. private static void Spiral(int[,] array2, int[] array1)
  86. {
  87. int lengthX = array2.GetLength(0);
  88. int lengthY = array2.GetLength(1);
  89. int Product = lengthX * lengthY;
  90. int CorrectY = 0;
  91. int CorrectX = 0;
  92. int Count = 0;
  93. while (lengthX > 0 && lengthY > 0)
  94. {
  95. for (int j = CorrectY; j < lengthY && Count < Product; j++)
  96. {
  97. array1[Count] = array2[CorrectX, j];
  98. Count++ ;
  99. }
  100. CorrectX++;
  101. for (int i = CorrectX; i < lengthX && Count < Product; i++)
  102. {
  103. array1[Count] = array2[i, lengthY - 1];
  104. Count++ ;
  105. }
  106. if (lengthY > 0 && lengthX > 0) lengthY-- ;
  107. else break;
  108. for (int j = lengthY - 1; j >= CorrectY && Count < Product; j--)
  109. {
  110. array1[Count] = array2[lengthX - 1, j];
  111. Count++ ;
  112. }
  113. if (lengthY > 0 && lengthX > 0) lengthX-- ;
  114. else break;
  115. for (int i = lengthX - 1; i >= CorrectX && Count < Product; i--)
  116. {
  117. array1[Count] = array2[i, CorrectY];
  118. Count++ ;
  119. }
  120.  
  121. CorrectY++;
  122. }
  123.  
  124. }
  125.  
  126. public static void SetArray(int[,] arr)
  127. {
  128. Random r = new Random();
  129. for (int i = 0; i < arr.GetLength(0); i++)
  130. {
  131. for (int j = 0; j < arr.GetLength(1); j++)
  132. {
  133. arr[i, j] = r.Next(11, 99);
  134. }
  135. }
  136. }
  137.  
  138. public static void SetArray(int[] arr)
  139. {
  140. Random r = new Random();
  141. for (int i = 0; i < arr.Length; i++)
  142. {
  143.  
  144. arr[i] = r.Next(11, 99);
  145.  
  146. }
  147. }
  148.  
  149. public static void PrintArray(int[] arr)
  150. {
  151. Console.Write("print 1d array:");
  152. for (int i = 0; i < arr.Length; i++)
  153. {
  154. Console.Write(arr[i] + " ");
  155. }
  156. Console.WriteLine();
  157. }
  158.  
  159. public static void PrintArray(int[,] arr)
  160. {
  161. Console.WriteLine("print 2d array:");
  162. for (int i = 0; i < arr.GetLength(0); i++)
  163. {
  164. for (int j = 0; j < arr.GetLength(1); j++)
  165. {
  166. Console.Write(arr[i, j] + " ");
  167. }
  168. Console.WriteLine();
  169. }
  170. }
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement