Electrical Engineering - Chapter 9: One-Dimensional Numeric Arrays

Data structure Grouping of like-type data Indicated with brackets containing positive integer constant or expression following identifier Subscript or index Loops commonly used for manipulation

ppt26 trang | Chia sẻ: thuongdt324 | Lượt xem: 501 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Electrical Engineering - Chapter 9: One-Dimensional Numeric Arrays, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Chapter 9 – One-Dimensional Numeric ArraysArrayData structureGrouping of like-type dataIndicated with brackets containing positive integer constant or expression following identifierSubscript or indexLoops commonly used for manipulationLesson 9.1 One-Dimensional ArraysDeclaration indicates name and reserves space for all elementsValues assigned to array elements using assignment statementsArray names classified as identifiersProgrammer sets size of array explicitlyLesson 9.1Array LengthDetermined by expression or value enclosed in brackets in declarationGeneral form for declaration type name[value];Value in brackets can be constant variable, expression, or literalLesson 9.1const int N = 26;double b[N];int a[25];int b[5+2];Array LengthMust be integer constant greater than 0Only integer type variables (with modifiers)int, charsigned, unsigned, short, longAlways within brackets following identifierExamples: int c[32]; int c[-25], b[43.5];Lesson 9.1ValidInvalidArray SubscriptsFirst index or subscript is 0int a[2];Data type of elements is intName of array is aNumber of elements is 2Valid subscripts are 0 and 1Address as a[0] and a[1]Lesson 9.1Printing Array ElementsUse coutTreat like single variablePrint one element at a timeExample: cout > num[a];Initialize by reading from the keyboardInitialize by assigning values in loopnum[a] = 0;Lesson 9.2Input/OutputTypically use filesReading from file, number of elements ?Guard against reading past end of fileUse function eof( ) which returns 1 when endReading from file, number of elements knownLesson 9.3Loop to Read Data Into an Arrayfor (j = 0; !infile1.eof ( ); j++) { infile1 >> a[j]; }Example of for loopLesson 9.3Number of elements unknown!Loop to Read Data Into an Arraylength = 0;infile >> data;while ((length > data;}Lesson 9.3Example of while loopNumber of elements unknown!File Input with Known Number of Elements First line of file contains number of array elements.infile1 >> num_elem;for (j = 0; j > a[j]; }File Input – Sentinel ValueParticular predefined value contained in file that indicates end of data groupLoop and read data value by value until sentinel is readfor (j = 0; a[j] != -1; j++) { infile1 >> a[j]; }Where –1 is thesentinel value.Loop to Print Data From Array (scores is array of 20 test scores)for (int j = 0; j < 20; ++j) { cout << scores[ j ] << endl; }Lesson 9.3Arrays and FunctionsPass address of array to function instead of element valuesFunction declarationData type and empty brackets int[ ]Function callArray name with no brackets xFunction headerData type, name, and empty brackets int x[ ]Lesson 9.4Passing Array InformationThree pieces of information Size of single array elementIndicated by data typeAddress of arrayIndicated by array name in function callLocation to store array addressIndicated by identifier followed by brackets in function headerLesson 9.4General FormLesson 9.4rtype function (type [ ], int);function (array, num);rtype function (type b[ ], int num_elem)DeclarationCallHeaderFunction return typeFunction nameType of values stored in arrayType of second argumentArray name in calling functionNumber of array elementsb is identifier used to represent array within functionnum_elem used to represent number of array elements in functionClasses with Array Data MembersAutomatically made accessible to public class member functionsUse enumeration to size data member arraysComma-separated list of identifiersIdentifier automatically assigned constant integer valueValue assigned depends on order in enumeration listLesson 9.5EnumerationKeyword enumMakes code more readableExample: enum {sun = 1, mon, tues, wed, thur, fri, sat};Assigns integer constant 1 to sun, 2 to mon, etc.Example of usage: int day; day = wed;Lesson 9.5Same as day = 4;Find Smallest Value in Arraysmall = scores [0];for (int j = 1; j < 20; ++j) { if (scores[ j ] < small) small = scores [ j ]; }Lesson 9.5Arrays of ObjectsDeclared using class name, object name, and brackets enclosing integer constant Vehicle truck[3];Three objects (truck[0], truck[1], truck[2] of class VehicleCall member functionobject, dot operator, and function name truck[0].set_data (50, 2, 3);Lesson 9.6Arrays of ObjectsGood when multiple pieces of information are linkedUse assignment statement to copy one object array element to another truck[1] = truck[0];Lesson 9.6SummaryCreate and manipulate arraysTrace and debug loops that manipulate arraysReserve memory during program executionUse arrays to solve problemsLearned how to: