Array ISYS 350.

Slides:



Advertisements
Similar presentations
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Advertisements

© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Arrays. Declaring a Array With subscript: –Dim numbers(2) as Integer –Using variable as subscript: Dim arrayIndex as Integer = 10 Dim myArray(arrayIndex)
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
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.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Loops ISYS 350. A Box of Chocolate Repeat this process until box is empty: – Take one chocolate from the box – Eat the chocolate – Box has more chocolate?
1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Command Object’s ExecuteNonQuery Method ISYS 512.
Loops ISYS 350. Compute the sum of a list of numbers: Example: 5, 2, 10, 8, 4 Process: Sum= 0 Get a number from the list Sum = Sum + the number Repeat.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
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.
Coding ADO.NET Objects: Connection, Command, DataReader.
Loops ISYS 350. Two Types of Loops while loop for loop.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
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:
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Introduction to Methods ISYS 350. Methods Methods can be used to break a complex program into small, manageable pieces – This approach is known as divide.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
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.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
Passing Objects to Methods
Loops ISYS 350.
Microsoft Visual Basic 2005: Reloaded Second Edition
Two Dimensional Array Mr. Jacobs.
What’s cheating and what is not?
Loops ISYS 350.
C++ Arrays.
Programming Interface Controls
Programming Interface Controls
Array ISYS 350.
Array ISYS 350.
Decision Structure - 1 ISYS 350.
Arrays Declare the Array of 100 elements Notes
ASP.Net Demo ISYS 350.
Visual Basic .NET BASICS
Array ISYS 350.
Loops ISYS 350.
An Introduction to Java – Part I, language basics
Array ISYS 350.
CS 106 Computing Fundamentals II Chapter 71 “Indexing”
Java Array ISYS 350.
EKT150 : Computer Programming
Arrays November 8, 2017.
Review for Final Exam.
CS 1430: Programming in C++.
Arrays .
Loops ISYS 350.
Lecture Set 10 Windows Controls and Forms
Decision Structure - 1 ISYS 350.
Module8 Multi-dimensional Array
Review for Final Exam.
Loops ISYS 350.
Arrays Part 2.
Loops ISYS 350.
Decision Structure - 1 ISYS 350.
Array ISYS 350.
Array ISYS 350.
Loops ISYS 350.
BMI Form1 Body Mass Index textBox3-4 (readonly) trackBar1
Programming Interface Controls
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 indexed from 0 to array size – 1. 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: 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);

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");