0

i wanna fill a matrix in C# with user's inputs,but i have trouble with it.when i enter rows and cols equal with each other,it work; but when i enter rows and cols different with each other the program stop . the code is

        int row = 0;
        int col = 0;
        int[,] matrix1;

        row = Convert.ToInt16(Console.ReadLine());
        col = Convert.ToInt16(Console.ReadLine());
        matrix1=new int[row,col];
        Console.WriteLine("enter the numbers");
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {

                matrix1[i, j] = Convert.ToInt16(Console.ReadLine());// i have problem with this line,... plz show me the correct form

            }
        }
Navid
  • 11
  • 1
  • 6
  • Add matrix1=new int[row,col]; after getting the values of row and col from the user not before as you have done – Razack Jan 16 '16 at 05:53

4 Answers4

1

You allocate memory before you input array size. Correct code:

int row = 0;
int col = 0;
int[ , ] matrix1;

row = Convert.ToInt16( Console.ReadLine( ) );
col = Convert.ToInt16( Console.ReadLine( ) );
matrix1 = new int[ row, col ];
Console.WriteLine( "enter the numbers" );
for ( int i = 0; i < col; i++ )
{
    for ( int j = 0; j < row; j++ )
    {
        matrix1[ i, j ] = Convert.ToInt16( Console.ReadLine( ) );
    }
}
Ivan Zinovyev
  • 146
  • 1
  • 4
0

You initialized your matrix as a 0x0 matrix. Which is an empty matrix so you can't add or read anything from it.

Try this

   int row = 0; 
   int col = 0; 
   int[,] matrix1;
    row = Convert.ToInt16(Console.ReadLine()); 
  col = Convert.ToInt16(Console.ReadLine());

     matrix1=new int[row,col]; 
    Console.WriteLine("enter the numbers");
       for (int i = 0; i < col; i++) {
      for (int j = 0; j < row; j++) 
     {    
     matrix1[i, j] = Convert.ToInt16(Console.ReadLine());      } }
oziomajnr
  • 1,671
  • 1
  • 15
  • 39
0
        int row;
        int col;
        row = Convert.ToInt16(Console.ReadLine());
        col = Convert.ToInt16(Console.ReadLine());

        int[,] matrix1 = new int[row, col];
        Console.WriteLine("enter the numbers");
        for (int i = 0; i < col; i++)
        {
            for (int j = 0; j < row; j++)
            {
                matrix1[i, j] = Convert.ToInt16(Console.ReadLine());
            }
        }
  • 2
    Please explain why this solves the question. [This](https://stackoverflow.com/help/how-to-answer) can help improve your answers. – shapiro yaacov Jan 18 '21 at 19:00
  • First you declare the number of columns and the number of rows in the matrix then you declare the matrix with the previously entered variables. In the end, just make classic for loops that are one inside the other. In the second loop, you enter the elements of the matrix. – Nikola Simonovć Jan 18 '21 at 20:20
  • 1
    Great. Could you add that to the answer itself? Makes it easier than looking through comments. Cheers! – shapiro yaacov Jan 18 '21 at 21:13
0
int col = Convert.ToInt32(Console.ReadLine());
int row = Convert.ToInt32(Console.ReadLine());
int[,] matrix = new int[row, col];
for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++)
    {
        Console.Write($"enter row{i} and col{j} ");
        matrix[i, j] = Convert.ToInt16(Console.ReadLine());
    }

    Console.WriteLine();
}
for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++)
    {
        Console.Write(matrix[i, j]);
    }
    Console.WriteLine();
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75