Guest User

Untitled

a guest
Jan 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. //given a 2-D matrix representing an image, a location of a pixel in the screen and a colour
  2. //replace the colour of the given pixel and all adjacent coloured pixels with the colour
  3.  
  4. //declare matrix
  5. string[,] matrix = new string[10, 10];
  6.  
  7. //populate matrix
  8. populate();
  9.  
  10. //get location
  11. Console.WriteLine("enter pixel coordinate eg 2,3");
  12. string Location = Console.ReadLine();
  13. int x =Convert.ToInt16(Location.Substring(0, 1));
  14. int y =Convert.ToInt16(Location.Substring(2, 1));
  15.  
  16. //get colour
  17. Console.WriteLine("enter pixel colour");
  18. string colour = Console.ReadLine();
  19.  
  20. //replace colours
  21. matrix[x, y] = colour;
  22.  
  23. if (x + 1 <= 10) { matrix[x + 1, y] = colour; }
  24. if (x - 1 >= 0) { matrix[x - 1, y] = colour; }
  25.  
  26. if (y + 1 <= 10) { matrix[x, y+1] = colour; }
  27. if (y - 1 >= 0) { matrix[x, y-1] = colour; }
  28.  
  29. //print matrix to check results
  30. print();
  31.  
  32. //populate matrix
  33. void populate()
  34. {
  35. Random rand = new Random();
  36. string[] alphabet = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
  37.  
  38. for (int i = 0; i < 10; i++)
  39. {
  40. for (int q = 0; q < 10; q++)
  41. {
  42. int select = rand.Next(0, 25);
  43. matrix[i, q] = alphabet[select];
  44. }
  45. }
  46. }
  47. //print matrix
  48. void print()
  49. {
  50. for (int i = 0; i < 10; i++)
  51. {
  52. for (int q = 0; q < 10; q++)
  53. {
  54. Console.Write(matrix[i, q] + " ");
  55. }
  56. Console.WriteLine();
  57. }
  58. }
Add Comment
Please, Sign In to add comment