lecture set 9

22
Lecture Set 9 Arrays, Collections and Repetition Part C - Random Numbers Rectangular and Jagged arrays 6/13/22 07:18 AM

Upload: wynn

Post on 09-Feb-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Lecture Set 9. Arrays, Collections and Repetition Part C - Random Numbers Rectangular and Jagged arrays. Objectives. Understand the concept of random numbers and how to generate random numbers Understand how to declare and manipulate rectangular and higher dimension arrays - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture Set 9

Lecture Set 9

Arrays, Collections and Repetition

Part C - Random NumbersRectangular and Jagged arrays

04/22/2023 07:27 AM

Page 2: Lecture Set 9

Slide 2

Objectives Understand the concept of random numbers and

how to generate random numbers Understand how to declare and manipulate

rectangular and higher dimension arrays Understand how to declare and manipulate

jagged arrays Working with arrays of objects

04/22/2023 07:27 AM

Page 3: Lecture Set 9

Slide 3

Introduction to Random Numbers Applications that require random

numbers Gaming and casino applications Computer simulations Test data generation

The process of creating a sequence of random numbers is called random number generation

8/18/2013 1:06 PM

Page 4: Lecture Set 9

Slide 4

Operation of a Random Number Generator

A random number generator is initialized based on a seed value Given the same seed value, the same

random number sequence will be generated Seed = r0, r1, r2, … where usually ri = f(ri-1)

Different seed values will generate different random sequences

The time of day (often expressed in milliseconds) is often used to create random seed values

If you do not use a different seed every time you execute your program, the random number gene-rator will generate the same sequence of numbers 8/18/2013 1:06 PM

Page 5: Lecture Set 9

Slide 5

The System.Random Class The System.Random class of the FCL is used to

create a random number generator and generate and get random values

We can generate integer and double random values

The constructor of the System.Random class creates the random number generator (an instance of the random class)

Using no constructor arguments, a random seed value is used

The seed value is based on the time of dayRandom rndVal = new Random();

This is the same asRandom rndVal = new

Random(DateTime.Now.Millisecond);

04/22/2023 07:27 AM

Page 6: Lecture Set 9

Slide 6

The Next Method Without arguments, the Next method gets a

positive random Integer valueRandom rndValR = new Random;;int rndValI = rndValR.Next();

With two arguments, a value within a range is returned

The first argument contains the lower bound and the second argument contains the upper bound

rndValI = rndValR.Next(1, 10);rndValI = rndValR.Next(1, 6);// Let index be an integerindex = rndValR.Next(0, myDictionaryWords.Length - 1);

The second line could be used to generate the value of the roll of a single die. How would you generate a random value representing the roll of two dice?

1/4/2016 10:06 PM

Page 7: Lecture Set 9

Slide 7

The NextDouble Method The NextDouble method returns a

random value between 0.0 and 1.0 The value is in the interval

[0.0<=value<1.0) Closed on the left and open on the right

Example:double randomDouble;randomDouble = _

rndValR.NextDouble()‘A value such a .4599695143908786 is generated

8/18/2013 1:06 PM

Page 8: Lecture Set 9

Slide 8

Introduction to Two-dimensional Arrays A two-dimensional array has rows and columns

Conceptually, it's similar to a grid or table Two-dimensional arrays have two subscripts instead

of one Declare a two-dimensional array with

unspecified dimensionsint [,] Table;

Declare a two-dimensional array with 10 rows and 10 columnsint [9, 9] Table;

8/18/2013 1:06 PM

Page 9: Lecture Set 9

Slide 9

Initializing Two-dimensional Arrays Two-dimensional arrays can be initialized just as

one-dimensional arrays can be initialized The array must not be given an initial size

The initialization list is nested as follows:int SalesArray[,] = { {150, 140, 170, 178}, {155, 148, 182, 190}, {162, 153, 191, 184}, {181, 176, 201, 203}};

8/18/2013 1:06 PM

Page 10: Lecture Set 9

Slide 10

Referencing Elements in a Two-dimensional Array Two-dimensional arrays require two

subscripts instead of one A comma separates the two subscripts

Reference the first row and column in the array named SalesArrayint cell;cell = SalesArray[0, 0];

8/18/2013 1:06 PM

Page 11: Lecture Set 9

Slide 11

Introduction to Three-dimensional Arrays (optional)

It's possible to create arrays with three dimensions

A three-dimensional array has three subscripts instead of two

Declare a dynamic three-dimensional arrayint cube[,,];

Declare a 10 by 10 by 10 array (1000 elements)double cube[10, 10, 10];

8/18/2013 1:06 PM

Page 12: Lecture Set 9

Slide 12

Working with Arrays of Objects Arrays can store object references in

addition to storing primary data types Example to store text box references:

textbox [3] textBoxList;textBoxList[0] = txtFirstName;textBoxList[1] = txtLastName;textBoxList[2] = txtAddress;

What does this array look like?

8/18/2013 1:06 PM

Page 13: Lecture Set 9

Slide 13

Memory Allocation to Store an Array of Text Boxes (VB)

8/18/2013 1:06 PM

Page 14: Lecture Set 9

Slide 14

Another View of 2D (Rectangular) Arrays

The syntax for declaring a rectangular array

type [firstupperbound, secondupperbound] arrayName;

A statement that creates a 3x2 array int [2, 1] numbers;

The syntax for referring to an element of a rectangular array

arrayName[rowindex, columnindex]

8/18/2013 1:06 PM

Page 15: Lecture Set 9

Slide 15

The syntax for using the GetLength method of a rectangular array

arrayName.GetLength(dimensionindex)

Code that works with the numbers array int [,] numbers = { {1,2}, {3,4}, {5,6} }; int numberOfRows = numbers.GetLength(0); int numberOfColumns = numbers.GetLength(1); int sumOfFirstRow = numbers[0,0]+ numbers[0,1];

Operations on a 2-Dimension Array

8/18/2013 1:06 PM

Page 16: Lecture Set 9

Slide 16

More Operations on a 2-Dimension Array

Code that displays the numbers array in a message box

string numbersString = ""; for (int i = 0; i < numbers.GetLength(0); i++) { for (int j = 0; j < numbers.GetLength(1); i++) { numbersString += numbers[i, j] + " "; } // end inner loop numbersString += "\n"; } // end outer loop MessageBox.Show(numbersString, "Numbers Test");

The message box that’s displayed

8/18/2013 1:06 PM

Page 17: Lecture Set 9

Slide 17

Jagged Arrays (by Example)

The syntax for declaring a jagged array type [numberofrows][] arrayName;

Code that declares a jagged array with three rows int numbers [3][]; ' 3 rows are declared numbers[0] = new int[2]; ' 3 columns for 1st row numbers[1] = new int[3]; ' 4 columns for 2nd row numbers[2] = new int[1]; ' 2 columns for 3rd row

8/18/2013 1:06 PM

Page 18: Lecture Set 9

Slide 18

Jagged Array References (optional)

I am not sure you will have occasion to do this – but maybe

The example is interesting from a pedagogic view

It illustrates memory allocation issues discussed already Referring to an element of a jagged array

arrayName[rowindex][columnindex]

Statements that assign values to the numbers array numbers[0][0) = 1; numbers[1][0] = 4; numbers[2][0] = 8; numbers[0][1) = 2; numbers[1][1] = 5; numbers[2][1] = 9; numbers[0][2) = 3; numbers[1][2] = 6; ] numbers[1][3] = 7;

8/18/2013 1:06 PM

Page 19: Lecture Set 9

Slide 19

More Jagged Array (examples – optional)

Code that creates the numbers array with one statement int [][] numbers = {new int[2] {1, 2, 3}, new int[3] {4, 5, 6, 7}, new int[1] {8, 9}}

Code that creates a jagged array of strings string [][]titles = _ {new string[2] {"War and Peace", "Wuthering Heights", "1984"}, new string[3] {"Casablanca", "Wizard of Oz", "Star Wars", "Birdy"}, new string[1] {"Blue Suede Shoes", "Yellow Submarine"}};

8/18/2013 1:06 PM

Page 20: Lecture Set 9

Slide 20

Yet ANOTHER Example (optional)

Code that displays the numbers array in a message box

string numbersString = ""; for (int i = 0; i < numbers.GetLength(0); i++) { for (int j = 0; j < numbers(i).Length; j++) { numbersString += numbers[i][j] + " "; } // end inner loop numbersString += "\n"; } // end outer loop MessageBox.Show(numbersString, "Jagged Numbers Test")

The message box that’s displayed

8/18/2013 1:06 PM

Page 21: Lecture Set 9

Slide 21

Common properties and methods of the Array class

Property Description Length Gets the number of elements in all of

the dimensions of an array. Instance method Description GetLength(dimension) Gets the number of elements in the

specified dimension of an array. GetUpperBound(dimension) Gets the index of the last element in

the specified dimension of an array.

Page 22: Lecture Set 9

Slide 22

Common properties and methods of the Array class (continued)

Static method Description Copy(array1, array2, length) Copies some or all of the values in

one array to another array. BinarySearch(array, value) Searches a one-dimensional array

that’s in ascending order for an element with a specified value and returns the index for that element.

Sort(array) Sorts the elements in a one-dimensional array into ascending order.