Creating a 3x3 matrix with user input numbers C# -
im trying create 3x3 matrix in c# language, know how create matrix need user input numbers. hope can me thank that.
i add while loop , use double.tryparse validate user's input. usin bwhazel's code:
const int matrix_rows = 3; const int matrix_columns = 3; double[,] matrix = new double[matrix_rows, matrix_columns]; (int = 0; < matrix_rows; i++) { (int j = 0; j < matrix_columns; j++) { double input; console.write("enter value ({0},{1}): ", i, j); while (!double.tryparse(console.readline(), out input) { console.write("enter correct value ({0},{1}): ", i, j); } matrix[i,j] = input } }
to totals rows can use following snippet:
for (int = 0; < matrix_rows; i++) { // each row double sum = 0.0; (int j = 0; j < matrix_columns; j++) { sum += matrix[i,j]; } console.writeline(string.format("the sum row {0} is: {1}", i, sum)); }
Comments
Post a Comment