0% found this document useful (0 votes)
2 views

2.Arrays-One-Dimensional

Uploaded by

saffronstunicorn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2.Arrays-One-Dimensional

Uploaded by

saffronstunicorn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ARRAYS

ONE DIMENSIONAL
Objectives

▷ Define arrays;
▷ Declare and create an array;
▷ Access elements of an array

2
What is an Array?

▷ collection of a fixed no. of components


where in all components are of the same
data type.
▷ a sequence of objects all of which have the
same data type.

3
What is an Array?

▷ used to store a collection of data, but it is


often more useful to think of an array as a
collection of variables of the same type
stored at contiguous memory locations.

4
Array
Elements

12 15 20 16 1 7 9 17

0 1 2 3 4 5 6 7

Index

5
Array Components
Element Index/Subscript
▷ how each of these ▷ specifies the position of the
objects are called. elements of the array. How
▷ the values inside the the elements of an array are
array numbered consecutively as
0,1,2, 3...
▷ the index of an array always
starts with 0.

6
Declaring an Array

▷ To declare an array in C#, you can use the


following syntax:

datatype[] arrayName;

7
where:

▷ datatype is used to specify the type


of elements to be stored in the array.
▷ [ ] specifies the rank of the array. The
rank specifies the size of the array.
▷ arrayName specifies the name of the
array.

8
Example:

double [] balance;

int [] num;

9
Initializing an Array:

▷ Declaring an array does not initialize


the array in the memory. When the
array variable is initialized, you can
assign values to the array.
▷ Array is a reference type, so you
need to use the new keyword to
create an instance of the array.
10
Example:

double[] balance = new double[10];

int[] num = new int[5];

11
Assigning values to Array:

▷ You can assign values to individual array


elements, by using the index number,
like:

double[] balance = new


double[10];
balance[0] = 4500.0;
12
Assigning values to Array:

▷ You can assign values to the array at the


time of declaration, like:

double[] balance = {2340.0, 4523.69, 3421.0};

13
Assigning values to Array:

▷ You can also create and initialize an array,


like:

int [] marks = new int[5] { 99, 98, 92, 97,


95};

14
Accessing Array Elements

▷ An element is accessed by indexing the


array name. This is done by placing the
index of the element within square
brackets after the name of the array. For
example:

double salary = balance[9];


15
Example 1:
string [] cars = {“Mazda”, “Subaru”, “Toyota”, “Chery”, “BMW”};

Console.WriteLine(cars[2]);
Console.WriteLine(cars[0]);
Console.WriteLine(cars[1]);
Console.WriteLine(cars[3]);
Console.WriteLine(cars[4]);

16
Example 1:
string [] cars = {“Mazda”, “Subaru”, “Toyota”, “Chery”, “BMW”};

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


{
Console.WriteLine(cars[i]);
}

17
Example 2:

int[] evenNums = { 2, 4, 6, 8, 10 };

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


evenNums[i] = evenNums[i] + 10;

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


Console.WriteLine(evenNums[i]);

18
Example 3:
int[] evenNums = { 2, 4, 6, 8, 10, 12 };
int temp=0;
for(int x=0; x<evenNums.Length; x+=2)
{
temp=evenNums[x];
evenNums[x]=evenNums[x+1];
evenNums[x+1]=temp;
}
for(int y=0; y<evenNums.Length; y++)
Console.Write(evenNums[y] + " ");
19
Array Index out of bounds

▷ This exception will typically occur when


a statement tries to access an element
at an index greater than the maximum
allowable index. The example program
below will display an error saying “array
index out of bounds”.

20
Array Index out of bounds

double[] a = new double[] { 23.3, 4.5,7.9 };

for (int i = 0; i < 7; i++)


{
Console.WriteLine(a[i]);
}
21
Array Length

To find out how many elements an array


has, use the Length property:

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


Console.WriteLine(cars.Length);

// Outputs 4
22
Array Sort

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


Array.Sort(cars);
for(int i = 0; i < cars.Length; i++)
{
Console.WriteLine(cars[i]);
}

23
Array Sort

int[] myNumbers = {5, 1, 8, 9};


Array.Sort(myNumbers);
for(int i = 0; i < myNumbers.Length; i++)
{
Console.WriteLine(myNumbers[i]);
}

24

You might also like