Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ For Dummies (2004) [eng].pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
8.09 Mб
Скачать

100 Part II: Becoming a Functional C++ Programmer

Defining and using arrays of arrays

Arrays are adept at storing sequences of numbers. Some applications require sequences of sequences. A classic example of this matrix configuration is the spreadsheet. Laid out like a chessboard, each element in the spreadsheet has both an x and a y offset.

C++ implements the matrix as follows:

int intMatrix[10][5];

This matrix is 10 elements in 1 dimension, and 5 in another, for a total of 50 elements. In other words, intMatrix is a 10-element array, each element of which is a 5-int array. As you might expect, one corner of the matrix is in intMatrix[0][0], while the other corner is intMatrix[9][4].

Whether you consider intMatrix to be 10 elements long in the x dimension and in the y dimension is a matter of taste. A matrix can be initialized in the same way that an array is

int intMatrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

This line initializes the 3-element array intMatrix[0] to 1, 2, and 3 and the 3-element array intMatrix[1] to 4, 5, and 6, respectively.

Using Arrays of Characters

The elements of an array are of any type. Arrays of floats, doubles, and longs are all possible; however, arrays of characters have particular significance.

Creating an array of characters

Human words and sentences can be expressed as an array of characters. An array of characters containing my first name would appear as

char sMyName[] = {‘S’, ‘t’, ‘e’, ‘p’, ‘h’, ‘e’, ‘n’};

The following small program displays my name:

//

CharDisplay - output a

character array to

//

standard

output, the MS-DOS window

Chapter 7: Storing Sequences in Arrays 101

#include <stdio.h> #include <iostream.h>

// prototype declarations

void displayCharArray(char stringArray[], int sizeOfloatArray);

int main(int nArg, char* pszArgs[])

{

char charMyName[] = {‘S’, ‘t’, ‘e’, ‘p’, ‘h’, ‘e’, ‘n’}; displayCharArray(charMyName, 7);

cout << “\n”; return 0;

}

// displayCharArray - display an array of characters

//

by outputing one character at

//

a time

void displayCharArray(char stringArray[], int sizeOfloatArray)

{

for(int i = 0; i< sizeOfloatArray; i++)

{

cout << stringArray[i];

}

}

The program declares a fixed array of characters charMyName containing — you guessed it — my name (what better name?). This array is passed to the function displayCharArray() along with its length. The displayCharArray() function is identical to the displayArray() function in the earlier example program except that this version displays chars rather than ints.

This program works fine; however, it is inconvenient to pass the length of the array around with the array itself. If we could come up with a rule for deter­ mining the end of the array, we wouldn’t need to pass its length — you would know that the array was complete when you encountered the special rule that told you so.

Creating a string of characters

In many cases, all values for each element are possible. However, C++ reserves the special “character” 0 as the non-character. We can use ‘\0’ to mark the end of a character array. (The numeric value of ‘\0’ is zero; how­ ever, the type of ‘\0’ is char.)

102 Part II: Becoming a Functional C++ Programmer

The character \y is the character whose numeric value is y. The character \0 is the character with a value of 0, otherwise known as the null character. Using that rule, the previous small program becomes

// DisplayString - output a character array to

// standard output, the MS-DOS window #include <cstdio>

#include <cstdlib> #include <iostream> #include <string> using namespace std;

// prototype declarations

void displayString(char stringArray[]);

int main(int nNumberofArgs, char* pszArgs[])

{

char charMyName[] =

{‘S’, ‘t’, ‘e’, ‘p’, ‘h’, ‘e’, ‘n’, 0}; displayString(charMyName);

//wait until user is ready before terminating program

//to allow the user to see the program results system(“PAUSE”);

return 0;

}

// displayString - display a character string // one character at a time void displayString(char stringArray[])

{

for(int i = 0; stringArray[i] != ‘\0’; i++)

{

cout << stringArray[i];

}

}

The declaration of charMyName declares the character array with the extra null character \0 on the end. The displayString program iterates through the character array until a null character is encountered.

The function displayString() is simpler to use than its displayCharArray() predecessor because it is no longer necessary to pass along the length of the character array. This secret handshake of terminating a character array with a null is so convenient that it is used throughout C++ language. C++ even gives such an array a special name.

A string of characters is a null terminated character array. Confusingly enough, this is often shortened to simply string, even though C++ defines the separate type string.