PROGRAMMING LOGIC AND
DESIGN
C# ARRAYS
ARRAYS
• An array is a collection
of similar types of data.
For example,
• Suppose we need to
record the age of 5
students. Instead of
creating 5 separate
variables, we can simply
create an array:
ARRAY
DECLARATION
HO W MANY
EL EMENTS CAN IT
S TO RE?
To define the
number of
elements that an
array can hold, we
have to allocate
memory for the
array in C#.
ARRAY INITIALIZATION
• We can initialize an array during
the declaration.
• In an array, we use an index
number to determine the position
of each array element. We can
use the index number to initialize
an array in C#.
REMEMBER
• An array index always starts at 0.
That is, the first element of an array is
at index 0.
• If the size of an array is 5, the index of
the last element will be at 4 (5 - 1).
ACCESS
ARRAY
ELEMENTS
We can access the
elements in the array using
the index of the array.
class Program {
static void Main(string[] args) {
// create an array
int[] numbers = {1, 2, 3};
//access first element
Console.WriteLine("Element in first index : " + numbers[0]);
//access second element
Console.WriteLine("Element in second index : " + numbers[1]
);
EXAMPLE
//access third element
Console.WriteLine("Element in third index : " + numbers[2]);
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
// create an array
int[] numbers = { 1, 2, 3 };
Console.WriteLine("Old Value at index 0: " +
numbers[0]);
CHANGE ARRAY
// change the value at index 0
ELEMENTS numbers[0] = 11;
//print new value
Console.WriteLine("New Value at index 0: " +
numbers[0]);
Console.ReadLine();
}
}
ITERATING C# class Program
ARRAY USING {
LOOPS static void Main(string[] args)
{
int[] numbers = { 1, 2, 3 };
We can use loops for (int i = 0; i < numbers.Length; i++)
to iterate through {
Console.WriteLine("Element in index " + i + ": " +
each element of an numbers[i]);
}
array.
Console.ReadLine();
}
}
EXAMPLE: USING FOREACH LOOP
class Program
{
static void Main(string[] args) {
int[] numbers = {1, 2, 3};
Console.WriteLine("Array Elements: ");
foreach(int num in numbers) {
Console.WriteLine(num);
}
Console.ReadLine();
}
}
namespace ArrayMinMax
{
class Program
ARRAY O PERATI O NS {
static void Main(string[] args)
U SI NG SYSTEM.LI NQ {
int[] numbers = { 51, 1, 3, 4, 98 };
// get the minimum element
Console.WriteLine("Smallest Element: " +
We have the System.Linq namespace numbers.Min());
that provides different methods to
perform various operations in an array. // Max() returns the largest number in array
Console.WriteLine("Largest Element: " + numbers.Max()
In the example, );
• numbers.Min() - returns the smallest
number in an array, 1 Console.ReadLine();
}
• numbers.Max() - returns the largest
}
number in an array, 98 }
namespace ArrayFunction
{
class Program
{
static void Main(string[] args)
{
EXAMPLE
int[] numbers = { 30, 31, 94, 86, 55 };
// get the sum of all array elements
float sum = numbers.Sum();
In the above example, we have
// get the total number of elements present in the array
used int count = numbers.Count();
• numbers.Sum() to get the float average = sum / count;
sum of all the elements of the Console.WriteLine("Average : " + average);
array
// compute the average
• numbers.Count() to get the Console.WriteLine("Average using Average() : " +
numbers.Average());
total number of element
present inside the array Console.ReadLine();
}
}
}
REMEMBER
It is compulsory to use the
System.Linq namespace while
using Min(), Max(), Sum(), Count(),
and Average() methods.
MULTIDIMENSIONAL ARRAY
In a multidimensional array, each element of the array is also an array.
Here, x is a multidimensional array which has two elements: {1, 2, 3}
and {3, 4, 5}. And, each element of the array is also an array with 3
elements.
T W O - DIMENS IO NAL
ARRAY
A two-dimensional
array consists of
single-dimensional
arrays as its
elements. It can be
represented as a
table with a specific
number of rows and
columns.
TW O-DIME N SION AL
AR R AY
DE CL AR ATION
TWO-
DIMENS IO NAL
ARRAY
INIT IAL IZ AT IO N
We can
initialize an
array during the
declaration
ACCESS ELEMENTS FROM 2D ARRAY
We use the index number to accesss
elements of a 2D array.
// a 2D array
int[ , ] x = { { 1, 2 ,3}, { 3, 4, 5 } };
// access first element from first row
x[0, 0]; // returns 1
// access third element from second row
x[1, 2]; // returns 5
// access third element from first row
x[0, 2]; // returns 3
namespace MultiDArray
{
class Program
{
static void Main(string[] args)
{
//initializing 2D array
int[,] numbers = { { 2, 3 }, { 4, 5 } };
EXAMPLE: 2D
ARRAY // access first element from the first row
Console.WriteLine("Element at index [0, 0] : " + numbers[0, 0]);
// access first element from second row
Console.WriteLine("Element at index [1, 0] : " + numbers[1, 0]);
}
}
}
namespace MultiDArray
{
class Program
CHANGE ARRAY {
ELEMENTS static void Main(string[] args)
{
int[,] numbers = { { 2, 3 }, { 4, 5 } };
We can also change // old element
the elements of a Console.WriteLine("Old element at index [0, 0] : " +
numbers[0, 0]);
two-dimensional
array. To change the // assigning new value
numbers[0, 0] = 222;
element, we simply
// new element
assign a new value Console.WriteLine("New element at index [0, 0] : " +
to that particular numbers[0, 0]);
}
index. }
}
class Program
{
static void Main(string[] args)
{
int[,] numbers = { { 2, 3, 9 }, { 4, 5, 9 } };
for (int i = 0; i < numbers.GetLength(0); i++)
ITERATING {
Console.Write("Row " + i + ": ");
C# ARRAY
USING for (int j = 0; j < numbers.GetLength(1); j++)
{
LOOP Console.Write(numbers[i, j] + " ");
}
Console.WriteLine();
}
}
}
JAGGED ARRAY
• A jagged array is an array of array. Jagged arrays store arrays
instead of literal values.
• A jagged array is initialized with two square brackets [][]. The
f irst bracket specif ie s the size of an array, and the second
bracket specif ies the dimensions of the array which is going
to be stored.
Syntax:
JAG G E D AR R AY
DE CL AR ATIO N
JAGGED ARRAY
DECLARATI O N
We know each
element of a
jagged array is
also an array, we
can set the size of
the individual array.
INITIALIZING JAGGED ARRAY
There are different ways to initialize a jagged array.
1. Using the index number
Once we declare a jagged array, we can use the index
number to initialize it.
2. Initialize without setting size of array elements
3. Initialize while declaring Jagged Array
USING THE
INDEX NUMBER
Here,
• index at the first square
bracket represents the
index of the jagged array
element
• index at the second
square bracket
represents the index of
the element inside each
element of the jagged
array
INITIALIZE WITHOUT SETTING SIZE OF
ARRAY ELEMENTS
INITIALIZE WHILE DECLARING JAGGED
ARRAY
ACCES S ING
EL EMENTS O F A
JAG G ED ARRAY
We can access
the elements of
the jagged
array using the
index number.
class Program
{
static void Main(string[] args) {
EXAMPLE: C#
JAGGED ARRAY // create a jagged array
int[ ][ ] jaggedArray = {
new int[] {1, 3, 5},
new int[] {2, 4},
Here, inside a jagged array, };
• jaggedArray[1][0] - first // print elements of jagged array
element of the second Console.WriteLine("jaggedArray[1][0]: " + jaggedArray[1][0]);
Console.WriteLine("jaggedArray[1][1]: " + jaggedArray[1][1]);
array
• jaggedArray[1][1] - second Console.WriteLine("jaggedArray[0][2]: " + jaggedArray[0][2]);
element of the second Console.ReadLine();
array }
}
• jaggedArray[0][2] - third
element of the first array
ITERATING
THROUGH A
JAGGED ARRAY
We can use
loops to iterate
through each
element of the
jagged array.
JAGGE D AR R AY
W ITH
MULTIDIME N SION A
L AR R AY
We can also use
multidimensional
arrays as Jagged
Array Elements.
namespace JaggedArray
{
class Program
{
static void Main(string[] args)
{
// declare and initialize jagged array with 2D array
int[][,] jaggedArray = new int[3][,] {
new int[ , ] { {1, 8}, {6, 7} },
new int[ , ] { {0, 3}, {5, 6}, {9, 10} },
new int[ , ] { {11, 23}, {100, 88}, {0, 10} }
EXAMPLE };
Console.WriteLine(jaggedArray[0][0, 1]);
Console.WriteLine(jaggedArray[1][2, 1]);
Console.WriteLine(jaggedArray[2][1, 0]);
Console.ReadLine();
}
}
}