Array ISYS 350.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Arrays. Declaring a Array With subscript: –Dim numbers(2) as Integer –Using variable as subscript: Dim arrayIndex as Integer = 10 Dim myArray(arrayIndex)
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Array and ArrayList ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items in an.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Loop Continue ISYS 350. Straight Line Depreciation Table.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Arrays.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Loop Continue ISYS 350. Exiting a Loop Prematurely In some cases it is necessary to end a loop before the test condition would end it Use the break statement:
Loop Continue ISYS 350. Exiting a Loop Prematurely In some cases it is necessary to end a loop before the test condition would end it Use the break statement:
ARRAYS Multidimensional realities Image courtesy of
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.
Array, ArrayList and List ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Arrays 1.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Chapter 6: Using Arrays.
Passing Objects to Methods
Array ISYS 350.
Two-Dimensional Arrays
Loops ISYS 350.
Chapter 7: Working with Arrays
Microsoft Visual Basic 2005: Reloaded Second Edition
Two Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
ECE Application Programming
Chapter 7 Arrays.
CS 1430: Programming in C++.
ECE Application Programming
Array ISYS 350.
Array ISYS 350.
Arrays Declare the Array of 100 elements Notes
Visual Basic .NET BASICS
Array ISYS 350.
Engineering Problem Solving with C++, Etter/Ingber
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
CNG 140 C Programming (Lecture set 8)
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections
Array ISYS 350.
Chapter 8 Slides from GaddisText
Java Array ISYS 350.
EKT150 : Computer Programming
Review for Final Exam.
Loop Continue ISYS 350.
CIS162AD - C# Arrays ch08.ppt.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Module8 Multi-dimensional Array
Managing Collections of Data
Review for Final Exam.
CIS16 Application Development and Programming using Visual Basic.net
Arrays Part 2.
Loop Continue ISYS 350.
Loops ISYS 350.
Loop Continue ISYS 350.
Array ISYS 350.
Loop Continue ISYS 350.
Loop Continue ISYS 350.
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Presentation transcript:

Array ISYS 350

Array An array allows you to store a group of items of the same type together. Processing a large number of items in an array is easier than processing a large number of items stored in separate variables.

Declaring a Array Declare an array in one statement: Type[] arrayName = new type[array size]; Ex: string[]empName = new string[3]; double[] intRate = new double[6];

Array Elements Array elements are assigned an 0-based index. Each element can be accessed by its index: arrayName[index] Ex: empName[0] intRate[2]

Array Initialization With the declaration statement: string[] empName = new string[3] { "Peter", "Paul", "Mary" }; double[] intRate = new double[6] { .03, .04, .05, .06, .07, .08 }; Initialize each element separately: empName[0] = "Peter"; empName[1] = "Paul"; empName[2] = "Mary";

Accessing Array Elements with a for loop int arrayIndex; for (arrayIndex = 0; arrayIndex <= 2; ++arrayIndex) { MessageBox.Show(empName[arrayIndex].ToString()); } Using array’s Length property: number of elements in array for (arrayIndex = 0; arrayIndex <= empName.Length-1; ++arrayIndex) { MessageBox.Show(empName[arrayIndex].ToString()); } Note: Length - 1

Example: Compute the sum and average of numbers in an array double[] myGPAs = new double[5] { 2.5, 3.2, 3.4, 2.9, 3.6 }; double sumGPA=0, avgGPA; for (int i = 0; i <= myGPAs.Length - 1; ++i) { sumGPA += myGPAs[i]; } avgGPA = sumGPA / myGPAs.Length; MessageBox.Show("Average GPA is: " + avgGPA);

foreach Loop The foreach statement repeats a group of embedded statements for each element in an array.

foreach loop example double[] myGPAs = new double[5] { 2.5, 3.2, 3.4, 2.9, 3.6 }; double sumGPA = 0, avgGPA; foreach (double d in myGPAs) { sumGPA += d; } avgGPA = sumGPA / myGPAs.Length; MessageBox.Show("Average GPA is: " + avgGPA);

Using Array’s Methods and Length property Sum(), Average(), Max(), Min(); double[] myGPAs = new double[5] { 2.5, 3.2, 3.4, 2.9, 3.6 }; double avgGPA, maxGPA, minGPA; avgGPA = myGPAs.Average(); maxGPA = myGPAs.Max(); minGPA=myGPAs.Min(); MessageBox.Show(“The size of array is: " + myGPAs.Length);

Create a Loan Payment Form

If rates are stored in an array Method 1: Using a loop to create the Listbox with rates in the Form Load event string[] strRate = new string[6] { "3%", "4%", "5%", "6%", "7%", "8%" }; for(int i=0;i<=strRate.Length-1;i++) { listBox1.Items.Add(strRate[i]); }

Method 2: Data Binding Binding an array to a control’s DataSource property A string array to store rates with “%”: string[] strRate = new string[6] { "3%", "4%", "5%", "6%", "7%", "8%" }; Bind the array to a listbox: listBox1.DataSource = strRate; Using the Form Load event

Data Binding Example private void Form1_Load(object sender, EventArgs e) { string[] strRate = new string[6] { "3%", "4%", "5%", "6%", "7%", "8%" }; listBox1.DataSource = strRate; }

Parallel Array Example A parallel array to store the numerical rates: double[] intRate = new double[6] { .03, .04, .05, .06, .07, .08 }; Use listbox selectedIndex to access the rate: intRate[listBox1.SelectedIndex]

Code Example double[] intRate = new double[6] { .03, .04, .05, .06, .07, .08 }; double loan, rate, term, payment; loan = double.Parse(textBox1.Text); rate = intRate[listBox1.SelectedIndex]; if (radioButton1.Checked) term = 15; else term = 30; payment = Financial.Pmt(rate / 12, term * 12, -loan); textBox2.Text = payment.ToString("c");

Sort an Array Array Class: Sort Method Example: Sort array elements in increasing order Example: Array.Sort(myGPAs);

Exercise: Weighted Avg of three exams= 60%. highest score +30% Exercise: Weighted Avg of three exams= 60%*highest score +30%*2nd highest score +10%*lowest score Method 1: You may sort the array of exam scores. Or Method 2: You may use the Max, Min and Sum functions.

Count the number of words in a textbox string.Split() returns an array: https://msdn.microsoft.com/en-us/library/tabh47cf(v=vs.110).aspx

Count words example String myText = textBox1.Text; string[] wordList = (myText.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries)); textBox2.Text = wordList.Length.ToString(); foreach (string s in wordList) { MessageBox.Show(s.ToString()); }

Using an array of seperators string[]seperators = new string[3] { "," ," ", ";" }; String myText = textBox1.Text; string[] wordList = (myText.Split(seperators, StringSplitOptions.RemoveEmptyEntries)); foreach (string s in wordList) { MessageBox.Show(s.ToString()); }

2-dimensional array A table which has x number of rows and y number of columns.

Declare and initialize a 2-D array Type[,] arrayName = new type[row size, col size]; Ex: double[,] sales = new double[3, 4] { { 12, 10, 8, 12 }, { 14, 12, 9, 12 }, { 10, 7, 11, 12 } };

Useful Properties and methods Rank: number of the dimensions of the array Array sales has rank 2. Length: number of elements Array sales has 12 elements (3*4) GetUpperBound(0): The last index of the 1st dimension: 2 GetUpperBound(1): The last index of the 2nd dimension: 3

double[,] sales = new double[3, 4] { { 12, 10, 8, 12 }, { 14, 12, 9, 12 }, { 10, 7, 11, 12 } }; MessageBox.Show("Rank is: " + sales.Rank.ToString()); MessageBox.Show("The last index of the 1st dimension is: " + sales.GetUpperBound(0).ToString()); MessageBox.Show("The last index of the 2nd dimension is: " + sales.GetUpperBound(1).ToString()); MessageBox.Show("Total elements in array is: " + sales.Length.ToString());

Using a nested loop to access every element for (int rowIndex = 0; rowIndex <= sales.GetUpperBound(0); rowIndex++) { for (int colIndex=0;colIndex<=sales.GetUpperBound(1); colIndex++) MessageBox.Show(sales[rowIndex, colIndex].ToString()); }

Compute the row sum and column sum double[,] sales = new double[3, 4] { { 12, 10, 8, 12 }, { 14, 12, 9, 12 }, { 10, 7, 11, 12 } }; double [] rowSum = new double [3] {0,0,0}; double[] colSum = new double[4] { 0, 0, 0,0 }; for (int rowIndex = 0; rowIndex <= sales.GetUpperBound(0); rowIndex++) { for (int colIndex = 0; colIndex <= sales.GetUpperBound(1); colIndex++) rowSum[rowIndex] += sales[rowIndex, colIndex]; } MessageBox.Show("Row " + rowIndex.ToString() + " sum is:" + rowSum[rowIndex].ToString()); for(int colIndex=0;colIndex<=sales.GetUpperBound(1);colIndex++) colSum[colIndex] += sales[rowIndex, colIndex]; MessageBox.Show("COlumn " + colIndex.ToString() + " sum is:" + colSum[colIndex].ToString());

Compute grand total using foreach double[,] sales = new double[3, 4] { { 12, 10, 8, 12 }, { 14, 12, 9, 12 }, { 10, 7, 11, 12 } }; double grandSum = 0; foreach(double d in sales) { grandSum += d; } MessageBox.Show("The total is: " + grandSum.ToString());