Johnny Johnny - 5 days ago 7
C# Question

How to add a column of 0s at the end of an array C#

the console prompts the user to enter his array, i was able to put a col of 0s before the user enters his inputs, what i need is to put also a col of 0s at the end of the array after the user finishes.

Thank you

Answer

I am sure that you are using this code to insert 0's before your matrix. How to add a column to the an array C#

The changes required for this code is

//c++; //remove this line
c = c + 2; //add two extra column for adding 0's, One for beginning one for end
int[,] matrix = new int[r, c];   

Other changes are

    for (int row = 0; row < r; row++)
    {
      for (int col = 0; col < c - 1; col++) // reduce column loop by one
      {
        if (col == 0)
        {
          matrix[row, col] = 0;
        }
        else
        {
          Console.Write("Enter value for matrix[{0},{1}] = ", row, col - 1);
          matrix[row, col] = (int)Convert.ToInt32(Console.ReadLine());
        }
     }
   }
Comments