Put this outside your “main” method to get make sure the user gives a number.
private static int GetNumber(string request)
{
bool succeeded = false;
Console.WriteLine(request);
string reply="";
while(!succeeded)
{
reply = Console.ReadLine();
try
{
int.Parse(reply);//Attempt to convert "reply" into an integer.
succeeded = true;
}
catch
{
Console.WriteLine(request+" (make it a number)");
}
}
return int.Parse(reply);
}
And then use this for your main method.
public static void Main()
{
int rows = GetNumber("Enter no of Rows:");
int columns = GetNumber("Enter no of Columns:");
int[,] matrix = new int[rows,columns];
for(int row = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
{
matrix[row,column] = GetNumber("Enter value for sector Row: "+(row+1)+" Column: "+(column+1));
}
}
int smallestnumber = matrix[0,0];
for(int row = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
{
if(matrix[row,column] < smallestnumber)
{
smallestnumber = matrix[row,column];
}
}
}
Console.WriteLine("Lowest number is: "+smallestnumber);
}
I took a liberty here as your example says “Enter row value 1…Enter column value 2” however each individual value in a matrix would be represented by both a row and a column.
2
solved C# Console Application program to Create a user defined matrix and find Lowest number [closed]