0

I'm trying to fill a 2D char array with characters form a char array

I have tried this code but not finding the problem

    public void FillArray(char[,] array, char[] values)
    {
      
        for (int i = 0; i < array.GetLength(0); i++)
        {
            for (int j = 0; j < array.GetLength(1); j++)
            {
                for (int k = 0; k < values.Length; k++)
                {
                    array[i, j] = values[k];
                }

            }
        }
    }
10
  • What's the problem with it? Commented Apr 20, 2022 at 20:12
  • is looping array more times and not filling array in right way, i think problem is to this nested for loops Commented Apr 20, 2022 at 20:14
  • I think you need to describe what you're trying to get out of this. What does "filling array in right way" mean? The most inner loop is iterating over the values for each i/j pair; is that what you're wanting? Commented Apr 20, 2022 at 20:16
  • Could you please provide the exception or the problem you have and what have you tried Commented Apr 20, 2022 at 20:17
  • Show how you're calling it. Are you passing it an empty array? Commented Apr 20, 2022 at 20:21

2 Answers 2

1

Your first for loop is responsible for iteration over first dimension of array (i) It's okay. Your second for loop is responsible for iteration over second dimension of array (j). It's okay. Your third loop is responsible for iteration over char values array (k) Here's your bug.

For a given set of values of i and j which represents dimensions indexes of array, your function iterates through all positions of values array. So for each k value i and j values remain unchanged. Therefore you sequentially put all the values of values array (k+1)times into the same cell of two dimension array, ultimately leaving it with value of values[values.Length] as it is the highest possible value of k in the most nested loop.

I'd suggest solution similar to what @adv12 has proposed with slight modification as I am not sure if the k value would be 0 during first iteration of the nested for loop. It is also more readable IMO.

int k = 0;
public void FillArray(char[,] array, char[] values)
{
    for (int i = 0; i < array.GetLength(0); i++)
    {
        for (int j = 0; j < array.GetLength(1); j++)
        {
            array[i, j] = values[k];
            k++
            if (k >= values.Length)
            {
                k = 0;
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would also like to point out that this bug could have been spotted easily if you had debugged it with breakpoints. You would have simply observed how i, j and k values change with each iteration and at some point it would be perfectly clear where the bug is. If you are not familiar with breakpoints I'd suggest that you watch a video tutorial. There are many tutorials on youtube covering this subject and this is essential programming knowledge.
0

Here's my best guess at what you want based on your comments:

int k = 0;
public void FillArray(char[,] array, char[] values)
{
    for (int i = 0; i < array.GetLength(0); i++)
    {
        for (int j = 0; j < array.GetLength(1); j++)
        {
            array[i, j] = values[k++];
            if (k >= values.Length)
            {
                k = 0;
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.