Array Concept in C#:-




It is a collection of elements of  Similar and Di similar Type of Elements, We can store multiple-element using Single Variable in Array.


Array Base Index will be started from 0 to size-1.

Type of Array:-


1)   One Dimension Array:-    using this we can store element row-wise, array index will be started from 0.

Datatype [] arrayname = {12,23,111,45,67,89};
Datatype []  arrayname = new Datatype[size];

class ArrExample
    {
        static void Main()
        {
            int []  arr = new int[5];
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("enter element for " + i + " index");
                arr[i] = int.Parse(Console.ReadLine());
            }
            Console.WriteLine("Array Element is ");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i] +" ");
            }
            Console.ReadKey();
        }
    }

2)  Two Dimension Array or Multi Dimension Array:-  It will Store element using rows and columns, we can display data in matrix pattern using Multi dimension Array.

Datatype [,] arrayname = new Datayype[row,column];
int [][] arr = new int[2][2];
class MultiArr
    {
        static void Main()
        {
            int [,] arr = new int[2,2];
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j <2; j++)
                {
                    Console.WriteLine("enter element for " + i +""+ j+ " index");
                    arr[i,j] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine("Array Element is ");
             for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Console.Write(arr[i, j] + " ");
                }
                 Console.WriteLine();
             }
             Console.ReadKey();
            }
        }
 
3)  Jagged Array::-   Jagged is the collection of heterogeneous row elements means we can declare a different number of elements for each row.

Datatype [][] arrayname = new Datatype[rowsize][];

int [][] arr = new int[3][];
arr[0] = new int[5];
arr[1] = new int[2];
arr[2] = new int[1];

 Program of Jagged Array:-
 class JaggedArr
    {
        static void Main()
        {
            int r,c;
            Console.WriteLine("Enter number of rows");
            r = int.Parse(Console.ReadLine());
          
            int[][] arr = new int[r][];

            for (int i = 0; i < r; i++)
            {
                Console.WriteLine("Enter number of column elements");
                c = int.Parse(Console.ReadLine());
                arr[i] = new int[c];
                for (int j = 0; j < c; j++)
                {
                    arr[i][j] = int.Parse(Console.ReadLine());
                }

            }
            Console.WriteLine("Jagged Array Elements Is");
            for (int i = 0; i < r; i++)
            {
             
               for (int j = 0; j < arr[i].Length; j++)
                {
                    Console.Write(arr[i][j] +" ");
                }
                Console.WriteLine();

            }
            Console.ReadKey();

        }
    }

 
4)  Object Array

class ObjectArr
    {
        static void Main()
        {
            object[] arr = {"C","CPP",1200,12.34F,true,'a' };
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
            Console.ReadKey();
        }
    }


Program of Split one Array to two different Sub-Array?

 program to sort the elements of the array in desc order */

/* Program to find a unique element in array */

WAP to find the max element in each row and complete matrix?

WAP to Check Palindrome using Char Array?

Q) WAP to print the reverse of the Char Array?

Q) WAP to count total vowel and consonant in Char Array?

Q) WAP to replace char array each char to the next consecutive char?

Q) WAP to convert Char Array to upper case to lower case and lower case to Upper case
Q) WAP to multiply two matrices?

Q) WAP to find the second max element in each row of the matrix?

Q) WAP to count the total prime element in the matrix?

Q) WAP to find an average of Matrix rows?

Q) WAP to sort matrix rows?


                         

2 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. WAP to print reverse of Char Array?




    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ReverseofArray
    {
    class Program
    {
    static void Main(string[] args)
    {
    char[] arr = { 'A', 'a', 'd', 'a', 'r','s','h' };
    Console.WriteLine("Reverse of char ");

    for (int i= arr.Length-1 ; i>=0 ;i--)
    {
    Console.WriteLine(arr[i]);
    }

    Console.ReadKey();

    }
    }
    }

    ReplyDelete
  2. program to sort the element of array in desc order



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace sorting
    {
    class Program
    {
    static void Main(string[] args)
    {
    int[] Arr = new int[5];
    int i,j,temp;
    for (i = 0; i < Arr.Length; i++)
    {
    Console.WriteLine("enter element of array" + i+"Index");
    Arr[i] = int.Parse(Console.ReadLine());
    }
    for(i=0;i<Arr.Length;i++)
    {
    for(j=i+1;j<Arr.Length;j++)
    {
    if (Arr[i] < Arr[j])
    {
    temp = Arr[i];
    Arr[i] = Arr[j];
    Arr[j] = temp;
    }
    }
    }
    for(i=0;i<=Arr.Length;i++)
    {
    Console.WriteLine("sort of array is " + Arr[i]);
    }

    Console.ReadKey();
    }
    }
    }

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post