private void PrintMatrix(int[,] A, int m, int n)
{
outputBox.Clear();
for (int r = 0; r < m; r++)
{
for (int c = 0; c < n; c++)
{
if (0 ==c )
outputBox.Text = outputBox.Text+A[r,c].ToString();
else
outputBox.Text = outputBox.Text + "," + A[r,c].
ToString();
}
outputBox.Text = outputBox.Text + ";" + "\n";
}
}
private int[,] GetMatrix(int m, int n)
{
int[,] returnVal=new int[m, n];
int r, c;
if (1==m && 1<=n)
{
for (c = 0; c < n; c++)
returnVal[0, c] = c + 1;
return returnVal;
}
else if (2 == m && 1 <= n)
{
for (c = 0; c < n; c++)
{
returnVal[0, c] = c + 1;
returnVal[1, c] = 2 * n - c;
}
return returnVal;
}
else if(1==n&&1<=m)
{
for (r = 0; r < m; r++)
returnVal[r, 0] = r + 1;
return returnVal;
}
else if (2 == n && 1 <= m)
{
returnVal[0, 0] = 1;
for (r = 0; r < m; r++)
returnVal[r, 1] = r + 2;
for (r = 1; r < m; r++)
returnVal[r, 0] = 2 * m+ 1 - r;
return returnVal;
}
int[,] t = GetMatrix(m-2,n - 2);
returnVal = new int[m, n];
for (r = 1; r < m - 1; r++)
for (c = 1; c < n - 1; c++)
returnVal[r, c] = t[r - 1, c - 1] + 2 * m + 2 * n - 4;
for (c = 0; c < n; c++)
{
returnVal[0, c] = c + 1;
returnVal[m - 1, c] = m+2*n - 2 - c;
}
for (r = 1; r < m - 1; r++)
{
returnVal[r, 0] = 2*m+2 * n - 3 - r;
returnVal[r, n - 1] = n + r;
}
return returnVal;
}
private void button2_Click(object sender, EventArgs e)
{
int m = Convert.ToInt16(inputM.Text);
int n = Convert.ToInt16(inputN.Text);
int[,] A= GetMatrix(m, n);
PrintMatrix(A, m, n);
}