i want to print 2 dimmension arrey
( mat = new double[imax, jmax] )
in matrix mod,what is the syntex in c#?
thank you
i want to print 2 dimmension arrey
( mat = new double[imax, jmax] )
in matrix mod,what is the syntex in c#?
thank you
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
AlanRank #669 · 3.6K reputationPosted Nov 25, 2007, 5:02 AM EST
Code like I've used in the following console application will give you a display similar to what you might get if you were drawing the matrix on a piece of paper:
using System;
using System.Text;
class Test
{
static void Main()
{
int imax = 3; // say
int jmax = 4; // say
double[,] mat = new double[imax, jmax];
// fill matrix with some arbitrary data
for (int i = 0; i < imax; i++)
for (int j = 0; j < jmax; j++)
mat[i,j] = ((i + 1) * (j + 1)) / 4.0;
// build matrix string representation
// field width of 5 and 2 decimal places assumed
const char Vertical = '\u2502';
const char UpperLeftCorner = '\u250c';
const char UpperRightCorner = '\u2510';
const char LowerLeftCorner = '\u2514';
const char LowerRightCorner = '\u2518';
StringBuilder sb = new StringBuilder();
sb = sb.Append(UpperLeftCorner);
sb = sb.Append(new string(' ', jmax * 7));
sb = sb.Append(UpperRightCorner);
sb = sb.Append(Environment.NewLine);
for (int i = 0; i < imax; i++)
{
for (int j = 0; j < jmax; j++)
{
if (j==0)
{
sb = sb.Append(Vertical);
sb = sb.Append(" ");
}
sb = sb.Append(String.Format("{0,5:F2}", mat[i,j]));
if (j < jmax - 1)
sb = sb.Append(", ");
else
{
sb = sb.Append(" ");
sb = sb.Append(Vertical);
sb = sb.Append(Environment.NewLine);
}
}
}
sb = sb.Append(LowerLeftCorner);
sb = sb.Append(new string(' ', jmax * 7));
sb = sb.Append(LowerRightCorner);
string matrixString = sb.ToString();
Console.WriteLine(matrixString);
}
}