Advertisement
plamen27

4 7 Matrix Generator

Sep 6th, 2016
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 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 _4_7_Exercize_Matrix_Generator
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. // task with jagged arrays
  14. string[] input = Console.ReadLine().Split(' ').ToArray();
  15. string a = input[0];
  16. string b = input[1];
  17. string c = input[2];
  18.  
  19. int rows = Int32.Parse(b); // converts String to int
  20. int cols = Int32.Parse(c); // converts String to int
  21. int[][] matrix = new int[rows][];
  22. for (int row = 0; row < rows; row++)
  23. {
  24. matrix[row] = new int[cols];
  25. for (int col = 0; col < cols; col++)
  26. {
  27. matrix[row][col] = 0;
  28. }
  29. }
  30.  
  31. if (a == "A")
  32. {
  33. GetTypeA(rows, cols, matrix);
  34. }
  35. if (a == "B")
  36. {
  37. GetTypeB(rows, cols, matrix);
  38. }
  39. if (a == "C")
  40. {
  41. GetTypeC(rows, cols, matrix);
  42. }
  43. if (a == "D")
  44. {
  45. GetTypeD(rows, cols, matrix);
  46. }
  47. }
  48. private static void GetTypeA(int rows, int cols, int[][]matrix)
  49. {
  50. int d = 1;
  51. for (int col = 0; col < cols; col++)
  52. {
  53. for (int row = 0; row < rows; row++)
  54. {
  55. matrix[row][col] = d;
  56. d++;
  57. }
  58. }
  59. for (int row = 0; row < rows; row++)
  60. Console.WriteLine(string.Join(" ", matrix[row]));
  61. }
  62. private static void GetTypeB(int rows, int cols, int[][] matrix)
  63. {
  64. int d = 1;
  65. for (int col = 0; col < cols; col++)
  66. {
  67. if(col==0)
  68. {
  69. for (int row = 0; row < rows; row++)
  70. {
  71. matrix[row][col] = d;
  72. d++;
  73. }
  74. continue;
  75. }
  76. if (col % 2 != 0)
  77. {
  78. for (int row = rows-1; row >=0; row--)
  79. {
  80. matrix[row][col] = d;
  81. d++;
  82. }
  83. }
  84. if (col%2==0)
  85. {
  86. for (int row = 0; row < rows; row++)
  87. {
  88. matrix[row][col] = d;
  89. d++;
  90. }
  91. }
  92. /// Print the matrix
  93. }
  94. for (int row = 0; row < rows; row++)
  95. Console.WriteLine(string.Join(" ", matrix[row]));
  96. }
  97.  
  98. static void GetTypeC(int rows, int cols, int[][] matrix)
  99. {
  100. // the first down left part of the matrix(jagged array)
  101. int number = 1;
  102. for (int row = rows - 1; row >= 0; row--)
  103. {
  104. int startRow = row;
  105. for (int col = 0; col < rows - startRow; col++)
  106. {
  107. if (col > cols - 1) { break; }
  108. matrix[startRow + col][col] = number;
  109. number++;
  110. }
  111. }
  112. // the second up right part of the matrix (jagged array)
  113. for (int col = 1; col < cols; col++)
  114. {
  115. int startCol = col;
  116. for (int row = 0; row < cols - startCol; row++)
  117. {
  118. if (row > rows - 1) { break; }
  119. matrix[row][startCol + row] = number;
  120. number++;
  121. }
  122. }
  123. // Print matrix (jagged array)
  124. for (int row = 0; row < rows; row++)
  125. Console.WriteLine(string.Join(" ", matrix[row]));
  126. }
  127. static void GetTypeD(int rows, int cols, int[][] matrix)
  128. {
  129. int number = 1;
  130. int indexRow = 0;
  131. int indexCol = 0;
  132.  
  133. while (number <= rows * cols)
  134. {
  135. matrix[indexRow][indexCol] = number;
  136. number++;
  137.  
  138. bool canGoDown = (indexRow + 1) < rows && matrix[indexRow + 1][indexCol] == 0 && !((indexCol - 1) >= 0 && matrix[indexRow][indexCol - 1] == 0);
  139. if (canGoDown) { indexRow++; continue; }
  140. bool canGoRight = (indexCol + 1) < cols && matrix[indexRow][indexCol + 1] == 0;
  141. if (canGoRight) { indexCol++; continue; }
  142. bool canGoUp = (indexRow - 1) >= 0 && matrix[indexRow - 1][indexCol] == 0;
  143. if (canGoUp) { indexRow--; continue; }
  144. bool canGoLeft = (indexCol - 1) >= 0 && matrix[indexRow][indexCol - 1] == 0;
  145. if (canGoLeft) { indexCol--; continue; }
  146. }
  147.  
  148. // Print matrix (jagged array)
  149. for (int row = 0; row < rows; row++)
  150. Console.WriteLine(string.Join(" ", matrix[row]));
  151. }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement