0% found this document useful (0 votes)
13 views9 pages

ARRAYS

Uploaded by

chintu chintu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

ARRAYS

Uploaded by

chintu chintu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

************ ARRAYS *****************

------------------------------------------
VARIABLE:-
---------- IT IS USED TO STORE A SINGLE DATA.
EX:-
int age =23;

Array:-
-------- It is used to store multiple data of same data type (Homogenous data)

Collection :-
-------------It is used to store the multiple data of different data types ,
where as a collection is known as Heterogenous data.

Array
------
It is a data structure in which we can store multiple data of same data types
-> It is a Homogenous in nature.
-> While creating array Object, we need to have capacity.
-> It is not groupable means(we cannot increase its size)
->Whenever if we are declaring array , we used to configure the size of an array.
->Array are string in index form.
-> An array index always starts from zero position.
-> Index ends with -1.

types of Arrays
---------------- 3 types of arrays
1.one dimensional array
2.Two dimensional array / multi dimensional array.
3.jagged Array.

Declaration of Array
--------------------
To declare a array we can use []

1.Array Declaration
------------------
syntax:-
--------
datatype[] arrayname; ----- ex:- int[] marks;

2.Array Creation
-------------------
syntax
------
Arrayname = new datatype[size]; --------- ex:- marks = new int[4];

3.Array Declaration & creation


--------------------------------
syntax:-
----------
datatype[] arrayname = new datatype[size]; ----- ex:- int[] marks = new int[4];

4.Array initialization
----------------------------
syntax:-
---------
Arrayname[index] = value; --------- ex;- marks[1]=10; --- marks[2]=10;

5.Array initialization directly


------------------------

syntax;-
--------
datatype[] arrayname = {v1,v2,v3,v3,v4,v5,v6.......vn};

ex:-
int[] marks={10,20,300,400,500,600};

=======================================================================

using System;

class Program
{
static void Main()
{
//datatype[] arrayname;
int[] age;
//Arrayname = new datatype[size];
age = new int[5];

// default values
Console.WriteLine(age[0]);
Console.WriteLine(age[1]);
Console.WriteLine(age[2]);
Console.WriteLine(age[3]);
Console.WriteLine(age[4]);
Console.WriteLine("-----------------");
//Arrayname[index] = value;
age[0] = 21;
age[1] = 22;
age[2] = 23;
age[3] = 24;
age[4] = 25;
Console.WriteLine(age[0]);
Console.WriteLine(age[1]);
Console.WriteLine(age[2]);
Console.WriteLine(age[3]);
Console.WriteLine(age[4]);

}
}
-----------------------------------------------------------------------------------
----------------------------
Advantages of an Array:-
-------------------------
->Using arrays we can store multiple values in a single variable.
-> save memory
->code reduce

DisAdvantages of an Array:-
-------------------------
-> Array are limit in size
-> We can't change the size in runtime.
->There is no predefined methods to manipulate the values in arrays.

Note:-
------
To Overcome the drawbacks of arrays in C#. collections are introduced.

1.One dimensional Array (1D array)


-----------------------------------
1D array is simplest type of array that contains only one row of storing the data.

using System;

class Program
{
static void Main()
{
int[] a = new int[5];
//adding the values to an variable name
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
Console.WriteLine(a[0]);//output -> 10
Console.WriteLine(a[1]);//output -> 20
Console.WriteLine(a[2]);//output -> 30
Console.WriteLine(a[3]);//output -> 40
Console.WriteLine(a[4]);//output -> 0

}
}

-----------------------------------------------------------------------------------
-----------------------------

using System;

class Program
{
static void Main()
{
int[] age = new int[4];
string[] name = new string[4];
age[0] = 21;
age[1] = 22;
age[2] = 23;
age[3] = 24;
name[0] = "bhavana";
name[1] = "laxmi";
name[2] = "bharathi";
name[3] = "anusha";
Console.WriteLine("my name is :" + name[0] + " "+"my age is :"+
age[0]);
Console.WriteLine("my name is :" + name[1] + " " + "my age is :" +
age[1]);
Console.WriteLine("my name is :" + name[2] + " " + "my age is :" +
age[2]);
Console.WriteLine("my name is :" + name[3] + " " + "my age is :" +
age[3]);
}
}

===================================================================================
=====================

using System;

class Program
{
static void Main()
{
//5.array initilization directly
int[] id = { 1, 2, 3, 4, 5, 6, 7 };

Console.WriteLine(id[0]);
Console.WriteLine(id[1]);
Console.WriteLine(id[2]);
Console.WriteLine(id[3]);
Console.WriteLine(id[4]);
Console.WriteLine(id[5]);
Console.WriteLine(id[6]);

}
}

===================================================================================
=====

note:- To find the length of array

length:-
---------
syntax"- Array.Length

Array length property:-


------------------------
length property is used to find the length of an array. it returns int value

-----------------------------------------------------------------------------------
---

using System;

class Array
{
static void Main(string[] args)
{
int[] a = { 3, 4, 5, 8, 6, 8, 9, 0 };
Console.WriteLine(a.Length);

int arratLength = a.Length;


Console.WriteLine(arratLength);

char[] ch = { 'M', 'H', 'A', 'E','S','H'};


Console.WriteLine(ch.Length);
int chLength = ch.Length;
Console.WriteLine(chLength);

-------------------------------------------------------------------------------

using System;

class Program
{
static void Main()
{
//5.array initilization directly
string[] name = { "chandu", "jaggu", "rana", "NtR", "pawan kalyan",
"laddu", " mummy", " daddy", " krishna" };

Console.WriteLine("length of an array is :"+name.Length);


Console.WriteLine(name[0]);
Console.WriteLine(name[1]);
Console.WriteLine(name[2]);
Console.WriteLine(name[3]);
Console.WriteLine(name[4]);
Console.WriteLine(name[5]);
Console.WriteLine(name[6]);
Console.WriteLine(name[7]);
Console.WriteLine("-------------------");
// for loop
for (int i = 0; i < name.Length; i++)
{
Console.WriteLine(name[i]);

}
Console.WriteLine("----- this will print the number in order wise-----");

int[] num = { 10, 2, 3, 40, 5, 60,7 };

for (int i = 0; i < num.Length; i++)


{
Console.WriteLine(num[i]);

// printing the array in reverse order


Console.WriteLine(" ---- printing in reverse order -----" );
for (int i = num.Length - 1; i >= 0; i--)
{
Console.WriteLine(num[i]);
}
}
}

--------------------------------------------------------------------------

using System;

class Array
{
static void Main(string[] args)
{
// even numbers
int[] a = { 1,2,3, 4, 5, 8, 6, 8, 9, };

for (int i = 0; i < a.Length; i++)


{
if (a[i] %2== 0)
{
Console.WriteLine(a[i]);
}

}
Console.WriteLine("-------------");
// sum of even numbers
int sum = 0;

int[] num = { 1, 2, 3, 4, 5, 6, 7, 8,9 };

for (int i = 0; i < num.Length; i++)


{
if (num[i] % 2 == 0)
{
sum = sum + num[i];
Console.WriteLine(num[i]);
}
}
Console.WriteLine("sum of even numbers : "+ sum);
}

----------------------------------------------------------------------------------

using System;

class Array
{
static void Main(string[] args)

{
// FINDING THE VOWELS IN THE LIST OF CHARACTERS
char[] ch = { 'M', 'H', 'A', 'E', 'S', 'H' };
for (int i = 0; i < ch.Length; i++)
{
if (ch[i] == 'A' || ch[i] == 'E' || ch[i] == 'I' || ch[i] == 'O' ||
ch[i] == 'U')
{
Console.WriteLine(ch[i]);
}
}
}
}

-----------------------------------------------------------------------------------
-------------------------------
//Finding the large number in a array

using System;

class Array
{
static void Main(string[] args)
{
//Finding the large number in a array
int large = a[0];
for (int i = 0; i < a.Length; i++)
{
if(large < a[i])
{
large=a[i];
}
}
Console.WriteLine("the large number in array:"+large);
}
}

-----------------------------------------------------------------------------------

using System;

class Array
{
static void Main(string[] args)
{
//Finding the smaller number in a array
int[] a = { 3, 4, 9, 12,56, 24, 36, 1 };
int large = a[0];
for (int i = 0; i < a.Length; i++)
{
if(large > a[i])
{
large=a[i];
}
}
Console.WriteLine("the Smaller number in array: "+large);
}
}

-----------------------------------------------------------------------------------
--

Occurrence of a characters in a array


---------------------------------------
using System;

class Array
{
static void Main(string[] args)
{
char[] arr = { 'a', 'b', 'a', 'c', 'a', 'b', 'a', 'c' };
int acount = 0;

for (int i = 0; i < arr.Length; i++)


{
if (arr[i] == 'a')
{
acount++;
}
}
Console.WriteLine(acount);
}
}

---------------------------------------------------------------
wap to print 50' zero in an array

using System;

class Array
{
static void Main(string[] args)
{
int [] a = new int [10];
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
}
}

-----------------------------------------------------------------

sorting
--------:-

It will display the numbers in a sequential either ascending or descending order

*****Multi Dimensional Array / 2D array********


------------------------------------------------
=> Two dimensional array represents matrix type (rows and columns)
=>It contains multiple rows and multiple columns for storing the data.

syntax:-
-------
1.datatype [ , ] arrayname = new datatype[rows, columnsa]
ex:- int [,] a = new int [2,3]

syntax:-
-------

2. data type [ , ] arrayname = {{v1,v2,v3,... vn},{v1,v2,v3... vn}};

ex:-
----
int [ , ] a = { {1,2,3} ,{4,5,6} };

=========================================================================

using System;
using System.CodeDom.Compiler;

class Array
{
static void Main(string[] args)
{
int[,] a = new int[2, 3];
// adding the values
a[0, 0] = 10;
a[0, 1] = 20;
a[0, 2] = 30;
// a[1,0] = 100;
a[1, 1] = 2;
// a[1, 2] = 300;
for (int i = 0; i < 2; i++)
{
for(int j = 0; j < 3; j++)
{
Console.Write(a[i, j]);
}
Console.WriteLine();
}

}
}

You might also like