0

I have a 10x10 matrix that I am filling with random chars. My problem is that I can't figure out how to insert a string "HOUSE" into the first row of the table.

Random rchar = new Random();
string word = "HOUSE";
char[] wordChars = word.ToCharArray();
char[,] arr = new char[10, 10];

//Size of Rows and Cols
var rowLength = arr.GetLength(0);
var colLength = arr.GetLength(1);

for (int x = 0; x < rowLength; x++)
{
    for (int y = 0; y < colLength; y++)
    {
        arr[0, y] = wordChars[1];
        arr[x, y] = (char)(rchar.Next(65, 91));
        Console.Write(arr[x, y] + " ");
    }
    Console.WriteLine();
}

I was trying to place a new value with the SetValue property but it doesn't work for me because I have a two-dimensional array.

Astrid E.
  • 2,280
  • 2
  • 6
  • 17
Dima
  • 1
  • 2
  • 1
    `arr[0, y] = chars[1];` <-- what is `chars`? It's defined nowhere – Cid Nov 15 '22 at 09:12
  • string word = "HOUSE"; char[] wordChars = word.ToCharArray(); – Dima Nov 15 '22 at 09:15
  • 2
    What do you man by "insert" and "new letters"? You cannot *insert* in an array, only *set* a specific value. You already show example of setting values, so your question is unclear. – JonasH Nov 15 '22 at 09:16
  • 1
    You are already setting a value in line `arr[0, y] = chars[1];`. – Good Night Nerd Pride Nov 15 '22 at 09:18
  • I want the letters of the word House to appear in the table i was tryinig to do this one **arr[x, y] = wordChars[y];** but i got a error IndexOutOfRangeException: – Dima Nov 15 '22 at 09:21
  • `y` goes from 0 to 9, and `"HOUSE"` has only 5 characters – Cid Nov 15 '22 at 09:34
  • 1
    the code in the question and what you're saying in comment have little in common. Please edit your question with the **actual** code along with its issue – Cid Nov 15 '22 at 09:37
  • and how to bypass this error ? – Dima Nov 15 '22 at 10:03
  • You need to make sure `y` is in fact a valid index of `wordChars`, before trying to access the value at that very index (`wordChars[y]`). Something like [this](https://dotnetfiddle.net/CLBKfy) – Astrid E. Nov 15 '22 at 10:06
  • `var ch = printLetterInWord ? wordChars[y] : (char)(rchar.Next(65, 91));` i cant undrestund what its mean ? :, can u rewrite _ – Dima Nov 15 '22 at 11:02
  • The `var a = b ? c : d` notation means: _If `b` is `true`, set `var a = c`; else (if `b` is `false`), set `var a = d`_. So: If `printLetterInWord` is `true`, set `ch = wordChars[y]`; else (if `printLetterInWord` is `false`), set `ch = (char)(rchar.Next(65, 91))`. – Astrid E. Nov 15 '22 at 11:09
  • (More about the ternary conditional operator `?:` [here](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator)) – Astrid E. Nov 15 '22 at 11:10
  • Thank you so much. I'm starting to get it. Will it work if you take for example a random index and try to insert a word into it? – Dima Nov 15 '22 at 11:28
  • I have added a comment to your (at the time of posting this) closed [question post regarding a scrabble game](https://stackoverflow.com/questions/74115276/how-to-add-a-word-of-letters-to-the-table-for-a-scrabble-game), containing a link to a fiddle I made. You can play around with indices there, and hopefully gain some better understanding. – Astrid E. Nov 16 '22 at 10:00

2 Answers2

0

Just set to your arr using two dimentional indices like [1,1] or [x,y]

Example:

    public static void Main()
    {
        Random rchar = new Random();
        char[,] arr = new char[10, 10];

        //Size of Rows and Cols
        var rowLength = arr.GetLength(0);
        var colLength = arr.GetLength(1);

        for (int x = 0; x < rowLength; x++)
        {
          for (int y = 0; y < colLength; y++)
          {
            arr[x,y] = (char)(rchar.Next(65, 91));
            Console.Write(arr[x, y] + " ");
          }
          Console.WriteLine();
       }
    }
IKomarovLeonid
  • 352
  • 1
  • 9
  • and if I just want to insert a new value by y (colLength), how do I write it correctly? – Dima Nov 15 '22 at 09:18
0

Try running this piece of code, I think it covers your question of setting values in a multidimensional array. Also I see a "chars[1]" in your code, this variable does not exist, did you mean to get the random char here? You might want to look up how to get a random char and put it in that spot.

char[,] arr = new char[10, 10];

arr[0, 0] = 'a';
arr[0, 1] = 'b';
arr[0, 2] = 'c';

arr[1, 0] = 'd';
arr[1, 1] = 'e';
arr[1, 2] = 'f';

for (int i = 0; i < 3; i++)
{
    Console.WriteLine(arr[0, i]);
}

for (int i = 0; i < 3; i++)
{
    Console.WriteLine(arr[1, i]);   
}