120 likes | 364 Views
Multidimensional Arrays. C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of rows and columns: const int NUMROWS = 3; const int NUMCOLS = 7; int Array[NUMROWS][NUMCOLS];.
E N D
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of rows and columns: const int NUMROWS = 3; const int NUMCOLS = 7; int Array[NUMROWS][NUMCOLS]; Array[2][5] 3rd value in 6th column Array[0][4] 1st value in 5th column The declaration must specify the number of rows and the number of columns, and both must be constants.
Processing a 2-D Array A one-dimensional array is usually processed via a for loop. Similarly, a two-dimensional array may be processed with a nested for loop: for (int Row = 0; Row < NUMROWS; Row++) { for (int Col = 0; Col < NUMCOLS; Col++) { Array[Row][Col] = 0; } } Each pass through the inner for loop will initialize all the elements of the current row to 0. The outer for loop drives the inner loop to process each of the array's rows.
Initializing in Declarations int Array1[2][3] = { {1, 2, 3} , {4, 5, 6} }; int Array2[2][3] = { 1, 2, 3, 4, 5 }; int Array3[2][3] = { {1, 2} , {4 } }; If we printed these arrays by rows, we would find the following initializations had taken place: Rows of Array1: 1 2 3 4 5 6 Rows of Array2: 1 2 3 4 5 0 Rows of Array3: 1 2 0 4 0 0 for (int row = 0; row < 2; row++) { for (int col = 0; col < 3; col++) { cout << setw(3) << Array1[row][col]; } cout << endl; }
Higher-Dimensional Arrays An array can be declared with multiple dimensions. 2 Dimensional 3 Dimensional double Coord[100][100][100]; Multiple dimensions get difficult to visualize graphically. •
2-D Arrays as Parameters When passing a two-dimensional array as a parameter, the base address is passed, as is the case with one-dimensional arrays. But now the number of columns in the array parameter must be specified. This is because arrays are stored in row-major order, and the number of columns must be known in order to calculate the location at which each row begins in memory: address of element (r, c) = base address of array + r*(number of elements in a row)*(size of an element) + c*(size of an element) void Initialize(int TwoD[][NUMCOLS], const int NUMROWS) { for (int i = 0; i < NUMROWS; i++) { for (int j = 0; j < NUMCOLS; j++) TwoD[i][j] = -1; } }
Data Types data type: a collection of values and the definition of one or more operations that can be performed on those values C++ includes a variety of built-in or base data types: short, int, long, float, double, char, etc. The values are ordered and atomic. C++ supports several mechanisms for aggregate data types: arrays, structures, classes. These allow complex combinations of other types as single entities. C++ also supports other mechanisms that allow programmers to define their own custom data types: enum types and typedefs.
Enumerated Types An enumerated type is defined by giving a name for the type and then giving a list of labels, which are the only values that a variable of that type is allowed to have. Enumerated types allow the creation of specialized types that support the use of meaningful labels within a program. They promote code readability with very little overhead in memory or runtime cost. enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; enum Season {WINTER, SPRING, SUMMER, FALL}; enum Hemisphere {NORTH, SOUTH, EAST, WEST}; Month Mon; Season Period; Hemisphere Region; . . . if (Mon == JAN && Hemisphere == NORTH) Period = WINTER; An enumerated type is allowed to have up to 256 values and a variable of an enumerated type will occupy one byte of memory. It is an error for the same label to be listed as a value for two different enumerated types.
Designing with Enumerated Types Enumerated types provide a mechanism for the abstraction of real world entities. Enumerated type variables do not contain the character string of the value. The internal representation uses integer values; it is bad practice to rely on those values. Since the labels are logically constant values it is common to express them using all capital letters, just as for named constants. Enumerated type values cannot be read or (usefully) written directly. Enumerated type variables and values can be used in relational comparisons, in assignments and in switch statements as selectors and case labels. Enumerated type variables can be passed as parameters and used as the return value of a function. Good use of enumerated data types make the program more readable by providing a way to incorporate meaningful labels (thus easier to debug) and they help in self-documenting the program.
C++ Simple Data Types • enum • a user-defined data type • formed by listing identifiers
Enumerated Types • examples enum Birds {BLUEJAY, CARDINAL, ROBIN, SEAGULL}; enum LetterGrade {A, B, C, D, F}; Birds aBird; LetterGrade grade; • Valid Statements aBird = ROBIN; grade = A; // does not contain the character ‘A’ if (grade = = B) ….;
Enumerated Types in Arrays const int Numcities = 10; enum Climate {SUN, RAIN, FROST, SNOW}; Climate Weather[NumCities]; Valid assignments: Weather[0]=RAIN; . . Weather[9]=SNOW; To set all cities to SUN: int wIndex; for (wIndex = 0; wIndex < Numcities; wIndex++) Weather[wIndex]=SUN;