Unformatted text preview:

Chapter 8 ArraysSingle-Dimension ArraysSubscriptsDeclaration Statement for Arraysforeach Statementforeach ExamplesStructuresStructures cont – Simply declaring a datatypeStructures cont.Table LookupUsing List Boxes with ArraysMultidimensional ArraysMultidimensional Arrays cont.Initialize Two-Dimensional ArraysPrinting a Two-Dimensional TableSumming a Two-Dimensional TableLookup Operation for Two-Dimensional TablesPowerPoint PresentationChapter 8 ArraysProgramming in C# .NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.2Single-Dimension ArraysAn array is a list or series of values, similar to a list box without the boxCreate an array to keep a series of variables for later processingEach individual variable in an array is referenced by the same nameArrays also called tables or subscripted variablesEach individual variable is an element of the arraySubscript (or index) inside parentheses is the position of the element within the array3SubscriptsSubscripts may be constants, variables, or numeric expressionsSubscripts must be integersSpecify the number of elements in the array in the array’s declaration statementArray subscripts are zero basedYou declare a data type for the array and all array elements must be the same data typeA subscript must reference a valid element of the arrayException will be thrown if a subscript is out of range4Declaration Statement for ArraysGeneral Forma) Datatype[] ArrayName = new DataType[NumberofElements];b) Datatype[] ArrayName = new DataType[] {InitialValueList};c) Datatype[] ArrayName = {InitialValueList};Examplesa) string[] strName = new string[25]; <- initialized to emptya) decimal[] decBalance = new decimal[10]; <- initialized to zeroa) string[] strProduct = new string[99];b) int[] intValue = new int[] {1,5,12,18,20};c) string[] strName = {“Sean”, “Sam”, “Sally”, “Sara”};5foreach Statementforeach is a looping construct that does not require manipulation of the array subscriptsGeneral Formforeach (DataType ElementName in ArrayName){//Statement(s) in loop}C# automatically references each array element, assigns its value to ElementName, and makes one pass through the loopThe foreach loop will execute if the array has at least one element6foreach Examplesforeach (string strOneName in strName){Console.WriteLine(strOneName); //Write one element of the array}int intSum;foreach (int intOneTotal in intTotal){intSum += intOneTotal; //Add each element of the array to intSum}7StructuresCombine multiple fields of related data to create a new structure (record)Defining a structure is similar to defining a new data typeStruct declaration cannot go inside a methodBy default, a structure is publicIf an array is included inside a structure, you cannot specify the number of elements8Structures cont – Simply declaring a datatypeGeneral Form[public | private] struct NameOfStruct{public Datatype FirstField;public Datatype SecondField;. . .}Examplepublic struct Product{public string strDescription;public string strID;public int intQuantity;public decimal decPrice;}9Structures cont.Once a structure is created, declare variables of the structure typeEach field of data in variable declared as a structure is called an element of the structureC# does not allow declaration of the number of elements in an array in the struct declarationUsing a value as an index to an array is called a direct referenceView contents of array elements in break time using the Autos window by clicking on the plus sign at left of the array name10Table LookupDetermine which array element to update or access using a table lookupUse a while loop for a table lookupValidate input before performing table lookupCompare input to each element in array to find a matchIt is not necessary to arrange fields being searched in any particular sequence11Using List Boxes with ArraysIt is a good idea to use a list box for input of information to lookup in arrayUse the SelectedIndex property to determine the array subscriptSelectedIndex holds the position or index of the selected item from the list12Multidimensional ArraysTwo-dimensional arrays have rows and columnsArray declaration specifies number of rows and columns in the arrayRow is horizontal and column is verticalMust always use two subscripts to refer to individual elements of tableRow is first subscriptColumn is second subscript13Multidimensional Arrays cont.General FormDataType[,] ArrayName = new Datatype[NumberOfElements,NumberOfElements];DataType[,] ArrayName = new DataType[ , ] = {ListOfValues};Cannot specify the number of elements within parentheses and specify the initial valuesMust use a comma to specify two dimensions to the arrayExamples:string[,] strName = new string[3,4]; <- 3 rows , 4 columnsstring[,] strName = new string[ , ] = {{“James”, “Mary”, “Sammie”, “Sean”}, <- row 0{“Tom”, “Lee”, “Leon”, “Larry”}, <- row 1 {“Maria”, “Margaret”, “Jill”, “John”}}; <- row 214Initialize Two-Dimensional ArraysNumeric array elements are initialized to 0 and strings are initialized to empty stringsUse nested for loops to initialize array elementsint intRow, intColumn;for (intRow = 0; intRow < 3; intRow++){for (intColumn = 0; intColumn < 4; intColumn++){strName[intRow, intColumn] = “”;}}15Printing a Two-Dimensional TableUse a foreach loop to print contents of a two-dimensional tableProcesses all the columns in a row thenmoves to the next rowUse a for loop and specify a new x coordinate for each element to print on the same line16Summing a Two-Dimensional TableThere are several ways to sum a tableSum the columnsSum the rowsSum the figures in both directions and double-check the totalsTo sum an array in both directions, create two one-dimensional arrays to store the total fields17Lookup Operation for Two-Dimensional TablesUse direct reference and table lookup as in single-dimensional arraysLimitationsRow and column subscripts must be available to use direct reference Table lookup is the most common lookup techniqueLookup processes may require additional one-dimensional arrays or listsUse list boxes or text


View Full Document

St. Ambrose CSCI 480 - Arrays

Download Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?