c# - Read matrix elements in row wise -
i writing c# program matrix.when enter matrix inputs console, each element coming in separate row.but, want read row elements in single line.
this code
console.writeline("enter matrix"); int n= convert.toint32(console.readline()); int[ , ] matrix=new int[n,n]; for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ matrix[i,j]=convert.toint32(console.readline()); // console.write("\t"); } }
present getting like
1
2
3
4
but, want
1 2
3 4
help me.
if want read 1 row in 1 line, can ask user enter space-separated values 1 2
, 3 4
, read this
console.writeline("enter matrix size"); int n = convert.toint32(console.readline()); //add size , other validation if required int[,] matrix = new int[n, n]; console.writeline("enter values separated space."); (int = 0; < n; i++) { var values = (console.readline().split(' ')); (int j = 0; j < n; j++) { matrix[i, j] = int.parse(values[j]); } } //to write (int = 0; < n; i++) { (int j = 0; j < n; j++) { console.write(matrix[i, j] + " "); } console.writeline(); }
Comments
Post a Comment