arrays - filling matrix with user's input in C# -


i wanna fill matrix in c# user's inputs,but have trouble it.when enter rows , cols equal each other,it work; when enter rows , cols different each other program stop . 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 numbers");         (int = 0; < row; i++)         {             (int j = 0; j < col; j++)             {                  matrix1[i, j] = convert.toint16(console.readline());// have problem line,... plz show me correct form              }         } 

you allocate memory before 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 numbers" ); ( int = 0; < col; i++ ) {     ( int j = 0; j < row; j++ )     {         matrix1[ i, j ] = convert.toint16( console.readline( ) );     } } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -