Chapter 3 Introduction to array Third lecture Program

  • Slides: 11
Download presentation
Chapter 3: Introduction to array Third lecture Program: 4 th Semester Department: BS Due

Chapter 3: Introduction to array Third lecture Program: 4 th Semester Department: BS Due Date : 30 -04 -18 By: Serat

Chapter 3 Outline �Introduction to array �Array declaration �One dimensional array �Two dimensional array

Chapter 3 Outline �Introduction to array �Array declaration �One dimensional array �Two dimensional array �Multi dimensional array 2

Introduction to array o An array stores a fixed-size sequential collection of elements of

Introduction to array o An array stores a fixed-size sequential collection of elements of the same type. An array is 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. o All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. 3

Declaring array o To declare an array in C#, you can use the following

Declaring array o To declare an array in C#, you can use the following syntax: data type[] array name; o Data type is used to specify the type of elements in the array. o [ ] specifies the rank of the array. The rank specifies the size of the array. o Array name specifies the name of the array. For example, int[] x; 4

Initializing an Array o Declaring an array does not initialize the array in the

Initializing an Array o Declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assign values to the array. o Array is a reference type, so you need to use the new keyword to create an instance of the array. For example, int [ ] x = new int [5]; 5

Assigning Values to an Array o You can assign values to individual array elements,

Assigning Values to an Array o You can assign values to individual array elements, by using the index number, like: int[] x = new int[4]; x[0] = 2; o You can assign values to the array at the time of declaration as shown: int[] x = { 1, 2, 3, 4}; You can also create and initialize an array as shown: int [] x = new int[4] { 1, 2, 3, 4}; You may also omit the size of the array as shown: int [] x = new int[] { 1, 2, 3, 4}; o You can copy an array variable into another target array variable. In such case, both the target and source point to the same memory location: int [] x = new int[] { 1, 2, 3, 4}; int[] y = x; 6

One dimensional array using System; namespace onedimension { class Program { static void Main(string[]

One dimensional array using System; namespace onedimension { class Program { static void Main(string[] args) { int[] a = { 1, 2, 3, 4 }; int[] b = new int[] { 1, 2, 3, 4 }; int[] c = new int[4] { 1, 2, 3, 4 }; int[] d; d = new int[] { 1, 2, 3, 4 }; Console. Write. Line("a={0} b={1} c={2} d={3}", a[0], b[1], c[2], d[3]); //now we want to display first array elements through for loop for (int x = 0; x < 4; x++) { Console. Write. Line("a={0}", a[x]); } Console. Read. Key(); }}} 7

Two dimensional array using System; namespace two. Dimension { class Program { static void

Two dimensional array using System; namespace two. Dimension { class Program { static void Main(string[] args) { int[, ] a = {{ 1, 2, 3, 4, 5, 6 }}; int[, ] b = new int[, ] {{1, 2, 3, 4, 5, 6}}; int[, ] c = new int[, ] { { 1, 2, 3 }, { 4, 5, 6 } }; int[, ] d = new int[2, 3] {{ 1, 2, 3}, {4, 5, 6 }} ; //Console. Write. Line("{0}", d[1, 0]); //display through nested loop for (int x = 0; x <2; x++) { for (int y = 0; y < 3; y++) { Console. Write. Line("{0}", d[x, y]); Console. Read. Key(); } }} } } 8

Multi dimensional array using System; using System. Collections. Generic; using System. Text; namespace Console.

Multi dimensional array using System; using System. Collections. Generic; using System. Text; namespace Console. Application 15 { class Program { static void Main(string[] args) { int[, , ] a = {{ { 1, 2, 3, 4, 5, 6, 7, 8 }} }; int[, , ] b = new int[, , ] { { { 1, 2, 3, 4, 5, 6, 7, 8 } } }; int[, , ] c=new int[, , ] {{ { 1, 2}, {3, 4}}, { {5, 6}, {7, 8}}}; int[, , ] d=new int[2, 2, 2] {{ { 1, 2}, {3, 4}}, { {5, 6}, {7, 8}}}; //Console. Write. Line(c[0, 0, 0]); //display through nested for loop for (int x = 0; x < 2; x++) { for (int y = 0; y < 2; y++) { for (int z = 0; z < 2; z++) { Console. Write. Line("{0}", c[x, y, z]); } }} Console. Read. Key(); } }} 9

Multi dimensional array using System; namespace multi. Dimision { class Program { static void

Multi dimensional array using System; namespace multi. Dimision { class Program { static void Main(string[] args) { int[, , , ] a ={ {{ { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }} }}; int[, , , ] b = new int[, , , ] { {{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 } } }}; int[, , , ] c = new int[2, 2, 2, 2] { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }, { { { 9, 10 }, { 11, 12 } }, { { 13, 14 }, { 15, 16 } }; int[, , , ] d = new int[2, 2, 2, 2] { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }, { { { 9, 10 }, { 11, 12 } }, { { 13, 14 }, { 15, 16 } }; // Console. Write. Line(c[0, 0, 0]); //display through nested for loop for (int w = 0; w < 2; w++) { for (int x = 0; x < 2; x++) { for (int y = 0; y < 2; y++){ for (int z = 0; z < 2; z++){ Console. Write. Line("{0}", c[w, x, y, z]); }}}} Console. Read. Key(); }} } 10

Thanks 11

Thanks 11