ellapt

T8.1.PrintMatrix

Jan 17th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. using System;
  2.  
  3. class PrintmatrixToPrint
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("Fill and print a matrix of size (n, n) in 4 ways");
  8. Console.Write("Enter size of the matrix to be printed: ");
  9. int mSize = int.Parse(Console.ReadLine());
  10. int[,] matrixToPrint = new int[ mSize, mSize];
  11.  
  12. int element = 1;
  13.  
  14. Console.WriteLine("\nTask a:");
  15. for (int col = 0; col < mSize; col++)
  16. {
  17. for (int row = 0; row < mSize; row++)
  18. {
  19. matrixToPrint[row, col] = element;
  20. element++;
  21. }
  22. }
  23. for (int row = 0; row < mSize; row++)
  24. {
  25. for (int col = 0; col < mSize; col++)
  26. {
  27. Console.Write("{0,4}", matrixToPrint[row, col]);
  28. }
  29. Console.WriteLine();
  30. }
  31.  
  32. element = 1;
  33. Console.WriteLine("\nTask b: ");
  34. for (int col = 0; col < mSize; col++)
  35. {
  36. if (col % 2 == 0)
  37. {
  38. for (int row = 0; row < mSize; row++)
  39. {
  40. matrixToPrint[row, col] = element;
  41. element++;
  42. }
  43. }
  44. else
  45. {
  46. for (int row = mSize - 1; row >= 0; row--)
  47. {
  48. matrixToPrint[row, col] = element;
  49. element++;
  50. }
  51. }
  52. }
  53.  
  54. for (int row = 0; row < mSize; row++)
  55. {
  56. for (int col = 0; col < mSize; col++)
  57. {
  58. Console.Write("{0,4}", matrixToPrint[row, col]);
  59. }
  60. Console.WriteLine();
  61. }
  62. Console.WriteLine();
  63.  
  64. element = 1;
  65. Console.WriteLine("\nTask c: ");
  66. for (int row = 0; row <= mSize - 1; row++)
  67. {
  68. for (int col = 0; col <= row; col++)
  69. {
  70. matrixToPrint[ mSize - row + col - 1, col] = element;
  71. element++;
  72. }
  73. }
  74.  
  75. for (int row = mSize - 2; row >= 0; row--)
  76. {
  77. for (int col = row; col >= 0; col--)
  78. {
  79. matrixToPrint[row - col, mSize - col - 1] = element;
  80. element++;
  81. }
  82. }
  83.  
  84. for (int row = 0; row < mSize; row++)
  85. {
  86. for (int col = 0; col < mSize; col++)
  87. {
  88. Console.Write("{0,4}", matrixToPrint[row, col]);
  89. }
  90. Console.WriteLine();
  91. }
  92.  
  93. element = 1;
  94. int end= mSize;
  95. int start=0;
  96. Console.WriteLine("\nTask d:");
  97. do
  98. {
  99. for (int row = start; row < end; row++)
  100. {
  101. matrixToPrint[row, start] = element;
  102. element++;
  103. }
  104. for (int col = start + 1; col < end; col++)
  105. {
  106. matrixToPrint[end-1, col] = element;
  107. element++;
  108. }
  109. for (int row = end - 2; row >= start; row--)
  110. {
  111. matrixToPrint[row, end - 1] = element;
  112. element++;
  113. }
  114.  
  115. for (int col = end - 2; col >= start + 1; col--)
  116. {
  117. matrixToPrint[start, col] = element;
  118. element++;
  119. }
  120. start++;
  121. end--;
  122. }
  123. while (end - start > 0);
  124. for (int row = 0; row < mSize; row++)
  125. {
  126. for (int col = 0; col < mSize; col++)
  127. {
  128. Console.Write("{0,4}", matrixToPrint[row, col]);
  129. }
  130. Console.WriteLine();
  131. }
  132. Console.WriteLine();
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment