Arrays Computer & Programming Group

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Section 3 - Arrays and Methods. Arrays Array: collection of group of data variables of same type, sharing the same name for convenience - Easy to search.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
Multidimensional Arrays Computer and Programming.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Pointers and Arrays Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
C# — Console Application
Two-Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
Hassan Khosravi / Geoffrey Tien
ECE Application Programming
Parameter Passing & Array Examples – Part 1
EKT120 : Computer Programming
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Hassan Khosravi / Geoffrey Tien
ECE Application Programming
C++ Arrays.
C Passing arrays to a Function
1-Dimensional Arrays 2-Dimensional Arrays => Read section 1.4
Engineering Problem Solving with C++, Etter/Ingber
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Chapter 8 Multi-Dimensional Arrays
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
EKT150 : Computer Programming
CS111 Computer Programming
CS150 Introduction to Computer Science 1
EKT120: Computer Programming
Multidimensional Arrays
Thanachat Thanomkulabut
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Module8 Multi-dimensional Array
Multidimensional array
Multidimensional Arrays
Module 8 & 9 Method :Part I & Array :Part II
Thanachat Thanomkulabut
Dr Tripty Singh Arrays.
INC 161 , CPE 100 Computer Programming
Array Data Structure Chapter 6
EECE.2160 ECE Application Programming
Lets Play with arrays Singh Tripty
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Thanachat Thanomkulabut
Arrays Imran Rashid CTO at ManiWeber Technologies.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

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 Array declaration An array is an indexed collection of objects, all of the same type Array declaration double[] score; score = new double[5]; double[] score; score = new double[5]{1,2,3,3,5}; score 1 2 3 4 5 score

Review (2) int [] ai; int [] ai = new int[4]; Declaration only int [] ai; Declaration & creation int [] ai = new int[4]; Declaration & creation & initialization int [] ai = new int[4] {1, 2, 3, 4}; int [] ai = new int[] {1, 2, 3, 4}; int [] ai = {1, 2, 3, 4};

Review (3) 1 2 3 score int [] score = new int[4]; Access array elements int [] score = new int[4]; 1 2 3 score Runtime Error!! -3 4 7 score[0] = -3; score[2+1] = 7; Score[3-1] = score[0]+score[3] Console.WriteLine(score[2]); score[4] = 5; 4

Arrays Concept id 1 2 3 4 Array is a reference data type. Reference variables are used to refer to the data, not the data themselves. score Create Label double[] score; score = new double[5]; Reserve Memory id 1 2 3 4

Arrays with Methods :Example1 micky class AM { static void Main(){ int[] Arr = {1, 4, 5}; Console.WriteLine (“1:{0} {1} {2}”,Arr[0],Arr[1],Arr[2]); Add10(Arr); (“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; Arr id 1 2 5 4 1 15 14 11 Output 1: 1 4 5 2: 11 14 15

Arrays with Methods :Example2 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]); data info Nobi info id 1 2 3 B e a r N=4 B e a r B e a r Monitor

Self test 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;

Example 1 What is the output of this partial program? Output NOBI IBON 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(); What is the output of this partial program? Output NOBI IBON

Pass by value VS by reference 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

Pass by Value mynum id 1 2 5 4 -1 arr id 1 2 static void Main(){ 1 2 5 4 -1 static void Main(){ int[] mynum; mynum = new int[3] {4,0,-1}; Console.WriteLine ("Before mynum[0] = {0}",mynum[0]); Change(mynum); ("After mynum[0] = {0}",mynum[0]); } static void Change(int[] arr){ arr[0] = 5; arr = new int[3] {10,13,15}; ("In Change arr[0] = {0}",arr[0]); arr id 1 2 10 13 15 Output Before mynum[0] = 4 In Change arr[0] = 10 After mynum[0] = 5

Pass by Reference mynum arr id 1 2 5 4 -1 mynum arr id 1 2 1 2 5 4 -1 static void Main(){ int[] mynum; mynum = new int[3] {4,0,-1}; Console.WriteLine ("Before mynum[0] = {0}",mynum[0]); Change(ref mynum); ("After mynum[0] = {0}",mynum[0]); } static void Change(ref int[] arr){ arr[0] = 5; arr = new int[3] {10,13,15}; ("In Change arr[0] = {0}",arr[0]); mynum arr id 1 2 10 13 15 Output Before mynum[0] = 4 In Change arr[0] = 10 After mynum[0] = 10

Multi-dimensional Array (introduction) 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];

Multi-dimensional Array Declaration <type> [] <name>; int [] score; Multi-dimensional 2 Dimension <type> [ , ] <name>; int [ , ] score; 3 Dimension <type> [ , , ] <name>; int [ , , ] score;

Multi-dimensional Array Declaration score 1 Dimension <name> = new <type>[<dim size>]; score = new int[4]; 2 Dimension <name> = new <type>[<dim1 size >,<dim2 size>]; score = new int[4,2]; 3 Dimension <name> = new <type>[<dim1 size>,<dim2 size>,<dim3 size>]; score = new int[4, 2, 3]; score score

Multi-dimensional Array Declaration with Initialization 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} }; score 6 1 3 score 1 8 4 3 6 9

Index of Multi-dimensional Array int[,] score = { {5,3,1}, {9,2,4} }; 1 2 score 5 3 3 7 3 1 3 1 9 2 4 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); 5 6

Selftest Declare, create and initialize array name Matrix Matrix Format I ‘v’ ‘a’ ‘p’ ‘y’ ‘q’ ‘s’ Matrix ‘z’ ‘b’ char[,] Matrix; Matrix = new char[4,2]{ {'v','a'}, {'y','q'}, {'p','z'}, {'s','b'} };

Array.GetLength() Get numbers of ROW in ARRAY score 1 2 5 3 9 4 Array.GetLength() 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; 2 3 6

Example 2 Student 1 Write the program to get score from 4 students (each student has 2 test) score1= 3 score2= 8 Student 2 score1= 6 score2= 7 Student 3 score1= 8 score2= 10 Student 4 score1= 9 score2= 7

Student 1 score1= score2= Student 2 Student 3 Student 4 3 8 6 7 10 9 Example 2 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(); score 1 2 3 3 8 6 7 8 10 9 7

Example 2 with Method static void Main(){ double [,] arr; arr = ReadArray2(2,4); } 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;

score 1 2 3 8 6 7 10 9 Self test 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);

score 1 2 3 8 6 7 10 9 Self test From previous example, write the partial program to find average of scores of all students in first test 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);

score 1 2 3 8 6 7 10 9 Self test From previous example, write the partial program to find average scores of all test of last student 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);

How to find Array’s #dimension? By property Rank int [] matrix = new int[5]; Console.WriteLine(matrix.Rank); 1 int [,] matrix = new int[5,2]; Console.WriteLine(matrix.Rank); 2 int [,,] matrix = new int[5,2,3]; Console.WriteLine(matrix.Rank); 3