Skip to content

#12 Array

Arrays stores, a fixed-length collection of the same type of data in contiguous memory locations.

Let’s look at a simple example of an array and understand the declaration, initialization, and consumption of an array.

using System;

namespace Array
{

    class Program
    {
        
        static void Main(string[] args)
        {
            Console.WriteLine("Understanding Arrays");

            int[] arrayvariable = new int[3]; //Declaration of array

            //int[] arrayvariable = new int[3] { 10,20,30 }; //Declaration and initialization of array

            for (int i=0; i<3; i++)
            {
                Console.WriteLine("please enter {0} value", i);
                arrayvariable[i] = Convert.ToInt32(Console.ReadLine()); //Initialization of array
            }

            Console.WriteLine("You have entered the following numbers");
            foreach (var item in arrayvariable)
            {
                Console.WriteLine(item); //Consumption of array
            }

            Console.ReadKey();
        }
    }
}

Some Important Array implementations:

Multi Dimensional Array:

We also call it as Rectangular arrays. The simplest of all these is the 2-dimensional array. If a normal array is considered as a line of fixed length, then the 2-dimensional array can be pictured like a table or rectangle with a length and breadth.

A 3-dimensional array can be seen as a cube and so on.

using System;

namespace Array
{

    class Program
    {
        
        static void Main(string[] args)
        {
            Console.WriteLine("Understanding Arrays");

            //int[,] arrayvariable = new int[rows, columns]; //Syntax

            int[ , ] arrayvariable = new int[3,2]; //Declaration of array
            
            for (int i=0; i<3; i++)
            {
                for(int j=0; j<2; j++)
                { 
                Console.WriteLine("please enter {0} value of row {1}", j, i);
                arrayvariable[i,j] = Convert.ToInt32(Console.ReadLine()); //Initialization of array
                }
            }

            Console.WriteLine("You have entered the following numbers");
            foreach (var item in arrayvariable)
            {
                Console.WriteLine(item); //Consumption of array
            }

            Console.WriteLine("Same output different way of printing");
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Console.WriteLine("position of value a[{0},{1}]: {2}", j, i,arrayvariable[i,j]);
                }
            }
            Console.ReadKey();
        }
    }
}

Jagged Arrays:

A Jagged array is nothing but an array of arrays. When a variable is declared, space is created for it, but with jagged arrays declaration must be followed by a definition of the size of all the arrays in the array for space creation.

using System;

namespace Array
{

    class Program
    {
        
        static void Main(string[] args)
        {
            Console.WriteLine("Understanding Arrays");

            Console.WriteLine("Please enter the no of arrays you would want to create");
            int arraycount = Convert.ToInt32(Console.ReadLine());
            int arraysize;

            int[][] arrayvariable = new int[arraycount][]; //Declaration of array
            for(int i=0; i<arraycount; i++)
            {
                Console.WriteLine("Please enter the {0} array size", i);
                arraysize = Convert.ToInt32(Console.ReadLine());

                arrayvariable[i] = new int[arraysize]; //defining size of each array
                for (int j = 0; j < arraysize; j++)
                {
                    Console.WriteLine("Please enter the {0} value of {1} array", j,i);
                    arrayvariable[i][j] = Convert.ToInt32(Console.ReadLine());
                }
            }
            
            Console.WriteLine("You have entered the following numbers");
            //foreach (var item in arrayvariable) //this method will not work
            //{
            //    Console.WriteLine(item); //Consumption of array
            //}

            foreach (var numberofarray in arrayvariable) //this method will work because the first forloop returns no of arrays
            {
                foreach (var values in numberofarray) //while the second foreach returns the values 
                {
                    Console.WriteLine(values); //Consumption of array
                }
            }

            for (int i = 0; i < arrayvariable.Length; i++)
            {
                for (int j = 0; j < arrayvariable[i].Length; j++)
                {
                    Console.WriteLine("a[{0}][{1}] contains value: {2}", i, j, arrayvariable[i][j]);
                }
            }
            Console.ReadKey();
        }
    }
}

Passing Array as a function arguments:

using System;

namespace Array
{

    class Program
    {
        public int getaverage(int[] arr,int size)
        {
            int sum=0;
            foreach (var item in arr)
            {
                sum += item;
            }
            int avg = sum / size;

            return avg;
        }
        
        static void Main(string[] args)
        {
            Console.WriteLine("Understanding Arrays");

            Console.WriteLine("Please enter number of numbers in your avg");
            Int32.TryParse(Console.ReadLine(), out int size);
            int[] arr = new int[size];

            for (int i = 0; i < size; i++)
            {
                Console.WriteLine("Enter {0} value", i+1);
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }

            Program arrayvalue = new Program();
            int finalvalue = arrayvalue.getaverage(arr, size);

            Console.WriteLine("Avg value: {0}", finalvalue);

            Console.ReadKey();
        }
    }
}
Published inC#

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *