Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array ISYS 350.

Similar presentations


Presentation on theme: "Array ISYS 350."— Presentation transcript:

1 Array ISYS 350

2 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.

3 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];

4 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]

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

6 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

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

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

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

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

11 Create a Loan Payment Form

12 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]); }

13 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

14 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; }

15 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]

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

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

18 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.

19 Count the number of words in a textbox
string.Split() returns an array:

20 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()); }

21 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()); }

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

23 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 } };

24 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

25 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());

26 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()); }

27 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());

28 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());


Download ppt "Array ISYS 350."

Similar presentations


Ads by Google