I am trying to create a program which takes an array RXK and takes some numbers as its data. Each number represents a pixel. For example
represents a picture and each number is a pixel.Now what I want to do is to choose a specific pixel,I ask the user to give me the X, Y position, and than change that pixel to a desired value and also change all the pixels that have the same initial value to the chosen pixel, if these pixels "touch" each other. To give an example if I choose this configuration
and choose as the starting position the pixel in the left top and change its value to 5 I get this
and this is want I want to get. The 4 bottom pixels which have the same value as the chosen pixel are not changed because they are not touching any pixel which are touching the chosen pixel. This is what I want to get. Now I am facing some problem with the code I have written. I have created a class which has some functions:
- Has a static array of bool values of the size RXK and the initial values are all false. Each position of this array represents if I have checked its value before or not(because I use recursion and if I did not checked if I had been on this pixel before it would cause an infinite loop)
- A have the actual array that stores each value of this "photo"
- I have a static int value called N, which stores the value of the new value.
- I have a static int value called firstvalue which stores the initial value of the chosen pixel.
Now on this class I have the method which does the recursion. Before I create a new object I check if the neighbour pixel in the directions up,down,left,right are:
- inside the array, which means >-1 and
2.Have never been visited by checking if this position in the bool array is false
The value of this pixel is the same as the initial value. Now for some configurations the program works, but the starting point has to be 0,0 or it will not change all the pixels which need to be changed. For example if a choose this configuration
and chose the starting point 0,1 I get this
instead of this, which I get when I chose the initial position 0,0
Now for some configurations as a photo with 5x5 pixels the program does not work at all and gives me an error System.IndexOutOfRangeException
I can not find where the problem is and why sometimes it works perfectly and sometimes it does not work at all. All the problem and errors I have been getting have been error with IndexOutOfRangeException
. I have tried to see how the values change during the running of the problem, but I have not been able to find the problem. I would really appreciate any help. This is the code on the main method:
static void Main(string[] args)
{
int X_position;
int Y_position;
int N;
Console.WriteLine("Please write the number of rown and colums: ");
Arrays.K = int.Parse(Console.ReadLine());
Arrays.R = int.Parse(Console.ReadLine());
Arrays.photo = new int[Arrays.R, Arrays.K];
Arrays.validPositionHolder = new bool[Arrays.R, Arrays.K];
Console.WriteLine("Please Wirte the data of the array: ");
for (int i = 0; i < Arrays.R; i++)
{
for (int j = 0; j < Arrays.K; j++)
{
Arrays.photo[i, j] = int.Parse(Console.ReadLine());
}
}
for (int i = 0; i < Arrays.R; i++)
{
for (int j = 0; j < Arrays.K; j++)
{
Arrays.validPositionHolder[i, j] = false;
}
}
Console.WriteLine("Please write the coodrinate of the starting point: ");
X_position = int.Parse(Console.ReadLine());
Y_position = int.Parse(Console.ReadLine());
Arrays.firstvalue = Arrays.photo[X_position, Y_position];
Console.WriteLine("Please write the new value of the point:");
Arrays.N = int.Parse(Console.ReadLine());
Arrays a = new Arrays(X_position,Y_position);
for (int i = 0; i < Arrays.R; i++)
{
for (int j = 0; j < Arrays.K; j++)
{
Console.Write(Arrays.photo[i, j]);
}
Console.WriteLine();
}
Console.ReadKey();
}
and this is the code on the class i created
class Arrays
{
public static int firstvalue;
public static int N;
public static int[,] photo;
public static int K;
public static int R;
public static bool[,] validPositionHolder;
private int X_position01;
private int Y_position01;
object SubTree01;
object SubTree02;
object SubTree03;
object SubTree04;
public Arrays(int value01, int value02)
{
X_position01 = value01;
Y_position01 = value02;
ChangerOfValue();
}
private void ChangerOfValue()
{
if (X_position01 - 1 > -1)
{
if (Arrays.validPositionHolder[X_position01 - 1, Y_position01] == false)
{
if (Arrays.photo[X_position01 - 1, Y_position01] == firstvalue)
{
Arrays.validPositionHolder[X_position01 - 1, Y_position01] = true;
Arrays.photo[X_position01, Y_position01] = Arrays.N;
Arrays.photo[X_position01 - 1, Y_position01] = Arrays.N;
SubTree01 = new Arrays(X_position01 - 1, Y_position01);
}
}
}
if (Y_position01 - 1 > -1)
{
if (Arrays.validPositionHolder[X_position01, Y_position01 - 1] == false)
{
if (Arrays.photo[X_position01, Y_position01 - 1] == firstvalue)
{
Arrays.validPositionHolder[X_position01, Y_position01 - 1] = true;
Arrays.photo[X_position01, Y_position01] = Arrays.N;
Arrays.photo[X_position01, Y_position01 - 1] = Arrays.N;
SubTree02 = new Arrays(X_position01, Y_position01 + 1);
}
}
}
if (X_position01 + 1 < Arrays.R)
{
if (Arrays.validPositionHolder[X_position01 + 1, Y_position01] == false)
{
if (Arrays.photo[X_position01 + 1, Y_position01] == firstvalue)
{
Arrays.validPositionHolder[X_position01 + 1, Y_position01] = true;
Arrays.photo[X_position01, Y_position01] = Arrays.N;
Arrays.photo[X_position01 + 1, Y_position01] = Arrays.N;
SubTree03 = new Arrays(X_position01 + 1, Y_position01);
}
}
}
if (Y_position01 + 1 < Arrays.K)
{
if (Arrays.validPositionHolder[X_position01, Y_position01 + 1] == false)
{
if (Arrays.photo[X_position01, Y_position01 + 1] == firstvalue)
{
Arrays.validPositionHolder[X_position01, Y_position01 + 1] = true;
Arrays.photo[X_position01, Y_position01] = Arrays.N;
Arrays.photo[X_position01, Y_position01 + 1] = Arrays.N;
SubTree04 = new Arrays(X_position01, Y_position01 + 1);
}
}
}
}
}