Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //given a 2-D matrix representing an image, a location of a pixel in the screen and a colour
- //replace the colour of the given pixel and all adjacent coloured pixels with the colour
- //declare matrix
- string[,] matrix = new string[10, 10];
- //populate matrix
- populate();
- //get location
- Console.WriteLine("enter pixel coordinate eg 2,3");
- string Location = Console.ReadLine();
- int x =Convert.ToInt16(Location.Substring(0, 1));
- int y =Convert.ToInt16(Location.Substring(2, 1));
- //get colour
- Console.WriteLine("enter pixel colour");
- string colour = Console.ReadLine();
- //replace colours
- matrix[x, y] = colour;
- if (x + 1 <= 10) { matrix[x + 1, y] = colour; }
- if (x - 1 >= 0) { matrix[x - 1, y] = colour; }
- if (y + 1 <= 10) { matrix[x, y+1] = colour; }
- if (y - 1 >= 0) { matrix[x, y-1] = colour; }
- //print matrix to check results
- print();
- //populate matrix
- void populate()
- {
- Random rand = new Random();
- 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" };
- for (int i = 0; i < 10; i++)
- {
- for (int q = 0; q < 10; q++)
- {
- int select = rand.Next(0, 25);
- matrix[i, q] = alphabet[select];
- }
- }
- }
- //print matrix
- void print()
- {
- for (int i = 0; i < 10; i++)
- {
- for (int q = 0; q < 10; q++)
- {
- Console.Write(matrix[i, q] + " ");
- }
- Console.WriteLine();
- }
- }
Add Comment
Please, Sign In to add comment