Assigning values to a two dimensional array in C#

This program illustrates how to use random values.  This program also assigns values to a two dimensional array.  In the first dimension you will have the random variable and in the next it’s square.  Also, this program makes use of the string variable and adds values to the string variable every time during the loop.  The code for the string variable below will also show you how to format values within a string.   This will avoid making you loop the Console.Write Statement and instead at the end of it all, you just output the string value on the screen.

 

using System;

using System.Collections;

using System.Collections.Generic;

using System.Text;

 

namespace csMultiDimension

{

    class Squared

    {

        static void Main(string[] args)

        {

            int[,] twoDim = new int[10,2];

            Random randInt = new Random();

            int valueX;

            int valueY;

            string msgOut = "The random squared values table created is: \n \n";

 

            for (int x = 0; x < twoDim.GetLength(0); x++)

            {

                valueX = randInt.Next(30);

                valueY = valueX * valueX;

                twoDim[x,0] = valueX;

                twoDim[x, 1] = valueY;

                msgOut += String.Format("Random value {0,2}. Squared value: {1,4}. \n", twoDim[x, 0], twoDim[x, 1]);

            }

 

            Console.Write(msgOut);

            Console.ReadLine();

        }

    }

}