1

Lets say I have a 2D array:

int[,] array = {
       {1,2,4}, 
       {5,6,7}
}; 

Now we want the user to enter a value into that array. Lets say 7. So now the array looks like this:

int[,] array = {
       {7,2,4}, 
       {5,6,7}
}; 

Now another user or even the same user tries to enter 7 into the same element. I want the program to deny this. So it would give the user a message something like: "Error that element contains a 7, no other values can be entered into this element. This is permanent, once a 7 is in any element of this array, it can never be changed unless the program is restarted.

I know this might seem simple, and I could have messed around with arrays to find a solution. But I want to know a good way of doing this instead of guessing. Or hacking it.

The List.IndexOf() method seems like something I might use, but this is an array and that is a list, should I change it to a list? I really don't know. I am not even sure if using IndexOf is a good way to do this.

So I ask the masters.

  • 2
    I am trying to understand, are you adding 7 to the array 'array[1]'? or are you adding it to the main array? I get what you want to do for the deny part, I'm just trying to figure out what the end structure of your array needs to look like. Basically, if I understand correctly, you want to avoid duplicate values in the same sub-element / sub-array. – Sirmyself Aug 01 '19 at 12:04
  • 1
    Why does 7 overwrite 1 in the first array? – MakePeaceGreatAgain Aug 01 '19 at 12:05
  • Possible duplicate of [Check if a value is in an array (C#)](https://stackoverflow.com/questions/13257458/check-if-a-value-is-in-an-array-c) – Sinatr Aug 01 '19 at 12:07
  • 1
    `if (array.Cast().Any(item => item == 7)) {/* 7 is in the array*/}` – Dmitry Bychenko Aug 01 '19 at 12:08
  • What's the use-case for this? – Moo-Juice Aug 01 '19 at 12:08
  • Do you mean you cant repeat a number in a particular row? – Musab Aug 01 '19 at 12:09
  • 1
    That seems like a bundle of weird constraints. First, why a 2D array? Is that important, or would it "work" the same for a simple 1D array? Second, are you simply looking for something like `someArray.Contains(someNumber)`? Third, so you have an array and apparently that array is allowed to be changed exactly once, is that it? Or is the value of an item at any index allowed to be changed exactly once? Or do you want to allow one specific value only once in the array? – Corak Aug 01 '19 at 12:10
  • if you want one "row" of a 2D array, you might want to look into: https://stackoverflow.com/questions/27427527/how-to-get-a-complete-row-or-column-from-2d-array-in-c-sharp – Corak Aug 01 '19 at 12:15

1 Answers1

1

Create a class MatrixElement.cs.

public class MatrixElement
{
    public int Value { get; set; }
    public bool isPermanentlyChanged { get; set; }
}

All of the elements in the matrix will have their property isPermanentlyChanged to false as default value. Then you can fill your matrix like this:

MatrixElement[,] array =
{
    {new MatrixElement { Value = 1 },new MatrixElement { Value = 2 },new MatrixElement { Value = 4 }},
    {new MatrixElement { Value = 6 },new MatrixElement { Value = 6 },new MatrixElement { Value = 7 }}
};

And just check for the value in the indexes you want to change. For instace:

var userValueToChange = 7;
if (array[0, 0].isPermanentlyChanged == false)
{
    array[0, 0].Value = userValueToChange;
    array[0, 0].isPermanentlyChanged = true;
}
else
{
    Console.WriteLine($"Error that element contains a {userValueToChange}, no other values can be entered into this element");
}

This should help you out if I understood you correctly. If not, comment below, so I can edit it out.

G.Dimov
  • 2,173
  • 3
  • 15
  • 40