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;
}

Open in new window

LVL 2
itniflAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Fernando SotoRetiredCommented:
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.
käµfm³d 👽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();
            }

            Console.ReadKey();

            return 0;
        }
    }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
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.