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

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

Param Array:

There are times when you want the arguments/ parameters supplied to the function be variable, in these cases param array should be used. Once arguments are declared as param, the function will not worry about the size of the array or the number of arguments that are passed to it.

using System;

namespace Array
{

    public class arrayclass
    {
        public int getaverage(params int[] arr)
        {
            int sum = 0;
            foreach (var item in arr)
            {
                sum += item;
            }
            int avg = sum / arr.Length;

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

            arrayclass arr = new arrayclass();

            int sum = arr.getaverage(1, 2, 3, 4, 5, 6, 7, 8, 9);

            Console.WriteLine("avg value: {0}", sum);
            
            Console.ReadKey();
        }
    }
}

Leave a Reply

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