# Triangular matrices in C#

Here is a way to create a triangular matrice in C++. is there a just as simple way to do this in C#?

``````int main(void)
{
int rows = 5, cols = 5;
int **arr;
// allocate and initialize the array
arr = new int * [rows];
for (int r = 0; r < rows; r++) {
arr[r] = new int[r + 1];
for(int c = 0; c <= r; c++)
arr[r][c] = (r + 1) * 10 + c + 1;
}
// print the array
for(int r = 0; r < rows; r++) {
for(int c = 0; c <= r; c++)
cout << arr[r][c] << " ";
cout << endl;
}
// free the array
for(int r = 0; r < rows; r++)
delete [] arr[r];
delete [] arr;
return 0;
}
``````
LVL 2
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RetiredCommented:
Hi itnifl;

See if this fits your needs.

Please have a look at this package, Math.NET Numerics, which has support for Matrices and Vectors. On the page there is a link on the right of the page Getting Started - Matrices and Vectors. This package can be loaded into your project using NuGet.
0
Commented:
I don't see why you can't convert it directly (assuming my C++ reading skills aren't terrible these days):

``````using System;

namespace ConsoleApplication58
{
class Program
{
static int Main(string[] args)
{
int rows = 5, cols = 5;
int[][] arr;

// allocate and initialize the array
arr = new int[rows][];

for (int r = 0; r < rows; r++)
{
arr[r] = new int[r + 1];

for (int c = 0; c <= r; c++)
arr[r][c] = (r + 1) * 10 + c + 1;
}

for (int r = 0; r < rows; r++)
{
for (int c = 0; c <= r; c++)
Console.Write(arr[r][c] + " ");

Console.WriteLine();
}

return 0;
}
}
}
``````
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.