arrays
DESCRIPTION
Arrays. 01204111 Computer & Programming Group 350-352 Dept. of Computer Engineering Kasetsart University. Adopted from Thanomkulabut’s experiences. Brushed up by MikeLab .Net Factory @KU. Version 2012. Review. An array is an indexed collection of objects, all of the same type - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/1.jpg)
ARRAYS
Adopted from Thanomkulabut’s experiences.
Brushed up by MikeLab.Net Factory @KU.
01204111 Computer & Programming01204111 Computer & ProgrammingGroup 350-352Group 350-352Dept. of Computer EngineeringDept. of Computer EngineeringKasetsart UniversityKasetsart University
Version 2012
![Page 2: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/2.jpg)
Review
An array is an indexed An array is an indexed collectioncollection of objects, of objects, all of the all of the same typesame type
Array declaration
2
double[] score;score = new double[5];
score
double[] score;score = new double[5]{1,2,3,3,5};
1 2 3 4 5
score
![Page 3: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/3.jpg)
Review (2)3
Declaration onlyint [] ai;
Declaration & creationint [] ai = newnew int[4];
Declaration & creation & initialization
int [] ai = newnew int[4] {1, 2, 3, 4};
int [] ai = newnew int[] {1, 2, 3, 4};
int [] ai = {1, 2, 3, 4};
![Page 4: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/4.jpg)
Review (3)
Access array elements
4
intint [] score = [] score = newnew intint[4][4];;
1100 22 33scorescore
score[0] = -3;score[0] = -3;score[2+1] = 7;score[2+1] = 7;Score[3-1] = score[0]+score[3]Score[3-1] = score[0]+score[3]Console.WriteLine(score[2]);Console.WriteLine(score[2]);
-3-3 77
44
44
score[4] = 5;score[4] = 5;
Runtime Error!!
![Page 5: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/5.jpg)
Arrays Concept5
Array is a reference data type. Reference variables are used to refer to
the data, not the data themselves.
double[] score;score = new double[5];
Create Label
Reserve Memory
id 0 1 2 3 4
score
![Page 6: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/6.jpg)
Arrays with Methods :Example1
6
class AM { static void Main(){ int[] Arr = {1, 4, 5}; Console.WriteLine (“1:{0} {1} {2}”,Arr[0],Arr[1],Arr[2]); Add10(Arr); Console.WriteLine (“2:{0} {1} {2}”,Arr[0],Arr[1],Arr[2]); }
static void Add10(int[] micky){ for(int i=0;i<micky.Length;i++) micky[i] += 10; }}
id 0 1 2
15
14
11
541
1: 1 4 5Output
Arr
micky
2: 11 14 15
![Page 7: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/7.jpg)
Arrays with Methods :Example2
7
static void Main(){ char[] data; data = ReadArray(4); showArray(data);}static char[] ReadArray(int N){ char[] info = new char[N]; for(int i=0;i<N;i++) info[i] = char.Parse(Console.ReadLine()); return info;}static void showArray(char[] Nobi){ for(int i=0;i<Nobi.Length;i++) Console.Write(Nobi[i]);}
N=4 id 0 1 2 3
info
Monitor
B
B
e
e
a
a
r
rinfo
data
Nobi
Bear
![Page 8: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/8.jpg)
Self test8
What is the output of this partial program?
static void Main(){ int[] mynum; mynum = new int[5] {4,0,-1,2,3}; Change(mynum); for(int i=0; i<mynum.Length; i++) Console.Write("{0} ",mynum[i]);}static void Change(int[] arr){ arr[0] = 5; arr[2] = 7; arr[3] = -8;}
![Page 9: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/9.jpg)
Example 19
What is the output of this partial program?
static void Main(){ char[] name1,name2; name1 = new char[4] {'N','O','B','I'}; name2 = reverse(name1); showArr(name1); showArr(name2);}static char[] reverse(char[] arr){ int j=0; char[] arr_re = new char[arr.Length]; for(int i=arr.Length-1;i>=0;i--){ arr_re[j] = arr[i]; j++; } return arr_re;}static void showArr(char[] arr){ foreach(char x in arr) Console.Write(x); Console.WriteLine();}
NOBIIBON
Output
![Page 10: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/10.jpg)
Pass by value VS by reference
10
Pass by value We can only change the elements inside
the array
Pass by reference We can change the elements inside the
array We can change the array that variable
refers to
![Page 11: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/11.jpg)
Pass by Value11
static void Main(){ int[] mynum; mynum = new int[3] {4,0,-1}; Console.WriteLine ("Before mynum[0] = {0}",mynum[0]); Change(mynum); Console.WriteLine ("After mynum[0] = {0}",mynum[0]);}static void Change(int[] arr){ arr[0] = 5; arr = new int[3] {10,13,15}; Console.WriteLine ("In Change arr[0] = {0}",arr[0]);}
mynum
id 0 1 24 0 -1
OutputBefore mynum[0] = 4
arr
5
id 0 1 2
13 1510
In Change arr[0] = 10After mynum[0] = 5
![Page 12: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/12.jpg)
Pass by Reference12
static void Main(){ int[] mynum; mynum = new int[3] {4,0,-1}; Console.WriteLine ("Before mynum[0] = {0}",mynum[0]); Change(ref mynum); Console.WriteLine ("After mynum[0] = {0}",mynum[0]);}static void Change(ref int[] arr){ arr[0] = 5; arr = new int[3] {10,13,15}; Console.WriteLine ("In Change arr[0] = {0}",arr[0]);}
mynum
id 0 1 24 0 -15
OutputBefore mynum[0] = 4
arr
mynum
arr
id 0 1 2
13 1510
In Change arr[0] = 10After mynum[0] = 10
![Page 13: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/13.jpg)
Multi-dimensional Array (introduction)
13
If you want to keep score of 50 students
If each student have 10 test
double[] score = new double[50];
double[] score0 = new double[50];double[] score1 = new double[50];double[] score2 = new double[50];
double[] score9 = new double[50];
![Page 14: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/14.jpg)
Multi-dimensional Array Declaration
1 dimension <type> []
<name>; int [] score;
Multi-dimensional
2 Dimension <type> [ , ]
<name>; int [ , ] score;
3 Dimension <type> [ , , ]
<name>; int [ , , ] score;
14
![Page 15: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/15.jpg)
Multi-dimensional Array Declaration
15
1 Dimension <name> = newnew <type>[<dim size>]; score = new int[4];
2 Dimension <name> = newnew <type>[<dim1 size >,<dim2 size>]; score = new int[4,2];
3 Dimension <name> = newnew <type>[<dim1 size>,<dim2 size>,<dim3
size>]; score = new int[4, 2, 3];
scorescore
score
![Page 16: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/16.jpg)
Multi-dimensional Array Declaration with Initialization
16
1 Dimension int[] score = new int[3] {6, 1, 3};
2 Dimension int [,] score = new int[2, 3] { {1, 8, 4} , {3, 6,
9} }; int [,] score = { {1, 8, 4} , {3, 6, 9} }; int [,] score = { {1, 8, 4}
, {3, 6, 9} };
6 1 3score
1 8 4
3 6 9
score
![Page 17: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/17.jpg)
Index of Multi-dimensional Array
17
score[0,1] = 7; score[2-1 , 1+1] = 0; Console.Write(score[0,0]); for(int i = 0; i<=2 ; i++)
score[0,i] = 3; Console.Write(score.Length);
int[,] score = { {5,3,1},
{9,2,4} };
score
0 1 20
1
5 3 1
9 2 4
7
0
5
3 33
6
![Page 18: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/18.jpg)
Selftest18
Declare, create and initialize array name Matrix
‘v’
‘a’
‘p’
‘y’
‘q’
‘s’
Matrix
‘z’‘b’
char[,] Matrix;Format I
Matrix = new char[4,2]{ {'v','a'}, {'y','q'}, {'p','z'}, {'s','b'} };
![Page 19: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/19.jpg)
Array.GetLength()19
Get numbers of ROW in ARRAY arrayname.GetLength(0); score.GetLength(0);
Get numbers of Column in ARRAY arrayname.GetLength(1); score.GetLength(1);
Get numbers of all elements in ARRAY arrayname.Length; score.Length;
score
0 1 20
1
5 3 1
9 2 4
2
3
6
![Page 20: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/20.jpg)
Example 220
Write the program to get score from 4 students (each student has 2 test)
Student 1score1=score2=Student 2score1=score2=Student 3score1=score2=Student 4score1=score2=
38
67
810
97
![Page 21: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/21.jpg)
Example 221
Write the program to get score from 4 students (each student has 2 test)
double[,] score = new double[4,2];for(int i=0;i<score.GetLength(0);i++){ Console.WriteLine("Student {0}",i+1); for(int j=0;j<score.GetLength(1);j++) { Console.Write("score{0} = ",j+1); score[i,j] = double.Parse(Console.ReadLine()); } Console.WriteLine();}
Student 1score1=score2=
Student 2score1=score2=
Student 3score1=score2=
Student 4score1=score2=
38
67
810
97
scorscoree0 1
0
1
2
3
3 8
6 7
8 10
9 7
![Page 22: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/22.jpg)
Example 2 with Method
22
static double[,] ReadArray2(int row, int col){ double[,] score = new double[4,2]; for(int i=0;i<score.GetLength(0);i++){ Console.WriteLine("Student {0}",i+1); for(int j=0;j<score.GetLength(1);j++){ Console.Write("score{0} = ",j+1); score[i,j] = double.Parse(Console.ReadLine());
} Console.WriteLine(); } return score;}
static void Main(){ double [,] arr; arr = ReadArray2(2,4);}
![Page 23: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/23.jpg)
Self test23
From previous example, write the partial program to find summation of all scores of all students
double sum = 0;for(int i=0;i<score.GetLength(0);i++){ for(int j=0; j<score.GetLength(1); j++){ sum += score[i,j]; }}Console.WriteLine("Sumation = {0}",sum);
scorscoree0 1
0
1
2
3
3 8
6 7
8 10
9 7
![Page 24: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/24.jpg)
Self test24
From previous example, write the partial program to find average of scores of all students in first test
scorscoree0 1
0
1
2
3
3 8
6 7
8 10
9 7
double sum = 0;for(int i=0;i<score.GetLength(0);i++){ sum += score[i,0];}double avg = sum/score.GetLength(0);Console.WriteLine ("Average of First test = {0}",avg);
![Page 25: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/25.jpg)
Self test25
From previous example, write the partial program to find average scores of all test of last student
scorscoree0 1
0
1
2
3
3 8
6 7
8 10
9 7
double sum = 0;for(int j=0;j<score.GetLength(1);j++){ sum += score[3,j];}double avg = sum/score.GetLength(1);Console.WriteLine ("Average of last student = {0}",avg);
![Page 26: Arrays](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814880550346895db58c4b/html5/thumbnails/26.jpg)
How to find Array’s #dimension?
By property Rank
int [] matrix = new int[5];int [] matrix = new int[5];
Console.WriteLine(matrix.Console.WriteLine(matrix.RankRank););
int [,,] matrix = new int[5,2,3];int [,,] matrix = new int[5,2,3];
Console.WriteLine(matrix.Console.WriteLine(matrix.RankRank););
int [,] matrix = new int[5,2];int [,] matrix = new int[5,2];
Console.WriteLine(matrix.Console.WriteLine(matrix.RankRank););
11
22
33