Friday 8 February 2013

Two Dimentional Array


//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TD_array
{
    class td_array
    {
        static void Main(string[] args)
        {
            int[,] arr=new int[3,5];
            // Can also initialize the values at the time of declaration
            /*int[,] arr = {{11,12,13,14,15},
                         {21,22,23,24,25},
                         {31,32,33,34,35},
                         };*/

            int x = 0;

            //printing values of 2D array using foreach loop
            foreach (int i in arr)
                Console.Write(i + "  ");
            Console.WriteLine("\n");

            //assigning values to 2D array using nested for
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    x = x + 5;
                    arr[i, j] = x;
                }

            }
            //printing values of 2D array using nested for loop
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                    Console.Write(arr[i, j] + "  ");
                Console.WriteLine();
                Console.ReadKey();

            }
        }
    }
}

No comments:

Post a Comment