Week11

advertisement
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]
Array[0][4]
0
1
2
3
4
5
6
0
4
18
9
3
-4
6
0
1
12
45
74
15
0
98
0
2
84
87
75
67
81
85
79
3rd value in 6th column
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 twodimensional 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]
int Array2[2][3]
int Array3[2][3]
= { {1, 2, 3} , {4, 5, 6} };
= { 1, 2, 3, 4, 5 };
= { {1, 2} , {4 } };
If we printed these arrays by rows, we would find the following initializations
had taken place:
Rows of Array1:
for (int row = 0; row < 2; row++) {
1 2 3
4 5 6
for (int col = 0; col < 3; col++) {
cout << setw(3)
Rows of Array2:
<< Array1[row][col];
1 2 3
4 5 0
}
cout << endl;
Rows of Array3:
1 2 0
4 0 0
}
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
Season
Hemisphere
Mon;
Period;
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 selfdocumenting the program.
C++ Simple Data Types
simple types
integral
bool

char
shoirt
floating
int
enum
–
–
a user-defined data type
formed by listing identifiers
long
enum
float
double
long
double
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;
if (grade = = B)
….;
// does not contain the character ‘A’
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;
Download