Professional Documents
Culture Documents
2-Dimensional arrays
A one-dimensional array can be initialized
like
int myIntegerArray [ ] = {1,3,5,7,47};
Can you have an array of arrays? YES!
int myInteger2DArray [3][4] =
{{1,2,3,4},{5,6,7,8},{4,3,2,1}};
Example
Matrix
double myMatrix [5][5];
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
myMatrix [i][j] = i+j;
Before strings
We had to use character arrays. Strings are
much nicer for most applications, but you may
encounter old code, or need to squeeze every
last flop out of your code, or it may be easier to
work with characters rather than avoid them.
char favoriteLetter = S;
char favoriteWord [ ] = {H, e, l, l, o, \0};
The \0 is the end of array character.
Alternatively, one can write
char favoriteWord [ ] = Hello;
Remember Strings?
string fullName = Stephen DeSalvo;
How would you find out where the first space
character is ?
You can extract a string data type using
fullName.substr(0,7), but how do you test an
individual character?
You can try fullName.substr(i,1), or you can
access the characters directly.
int counter = 0;
while(fullName[counter] != ) {
counter++;
}