C++ Tutorial/Array/array — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
(нет различий)
|
Текущая версия на 13:31, 25 мая 2010
Содержание
- 1 array of strings
- 2 Initializing an array
- 3 Initializing an array in a declaration.
- 4 Linear search of an array
- 5 Obtaining the number of array elements
- 6 Passing arrays and individual array elements to functions
- 7 Static arrays are initialized to zero.
- 8 Use subscripting and pointer notations with arrays
array of strings
<source lang="cpp">#include <iostream>
using namespace std; int main(){ const int DAYS = 7; const int MAX = 10; char star[DAYS][MAX] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; for(int j=0; j<DAYS; j++) cout << star[j] << endl; return 0; }</source>
Initializing an array
<source lang="cpp">#include <iostream> using std::cout; using std::endl;
- include <iomanip>
using std::setw; int main() {
int n[ 10 ]; for ( int i = 0; i < 10; i++ ) n[ i ] = 0; for ( int j = 0; j < 10; j++ ) cout << n[ j ] << endl; return 0;
}</source>
0 0 0 0 0 0 0 0 0 0
Initializing an array in a declaration.
<source lang="cpp">#include <iostream> using std::cout; using std::endl;
- include <iomanip>
using std::setw; int main() {
int n[ 10 ] = { 2, 7, 4, 8, 5, 4, 9, 7, 6, 3 }; for ( int i = 0; i < 10; i++ ) cout << n[ i ] << endl; return 0;
}</source>
2 7 4 8 5 4 9 7 6 3
Linear search of an array
<source lang="cpp">#include <iostream> using std::cout; using std::cin; using std::endl; int linearSearch( const int [], int, int ); // prototype int main() {
const int arraySize = 100; int a[ arraySize ]; int searchKey = 28; for ( int i = 0; i < arraySize; i++ ) a[ i ] = 2 * i; int element = linearSearch( a, searchKey, arraySize ); cout << element << endl; return 0;
} int linearSearch( const int array[], int key, int sizeOfArray ) {
for ( int j = 0; j < sizeOfArray; j++ ){ if ( array[ j ] == key ){ return j; } } return -1;
}</source>
14
Obtaining the number of array elements
<source lang="cpp">#include <iostream> using std::cout; using std::endl; int main() {
int values[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; cout << endl << "There are " << sizeof values/sizeof values[0] << " elements in the array." << endl; return 0;
}</source>
There are 10 elements in the array.
Passing arrays and individual array elements to functions
<source lang="cpp">#include <iostream> using std::cout; using std::endl;
- include <iomanip>
using std::setw; void modifyArray( int [], int ); // appears strange void modifyElement( int ); int main() {
const int arraySize = 5; // size of array a int a[ arraySize ] = { 0, 1, 2, 3, 4 }; // initialize array a modifyArray( a, arraySize ); for ( int i = 0; i < arraySize; i++ ) cout << setw( 3 ) << a[ i ]; modifyElement( a[ 3 ] ); cout << a[ 3 ] << endl; return 0;
} void modifyArray( int b[], int sizeOfArray ) {
for ( int i = 0; i < sizeOfArray; i++ ) b[ i ] = 200;
} void modifyElement( int e ) {
e = 200 ;
}</source>
200200200200200200
Static arrays are initialized to zero.
<source lang="cpp">#include <iostream> using std::cout; using std::endl; void staticArrayInit( void ); void automaticArrayInit( void ); int main() {
staticArrayInit(); automaticArrayInit(); staticArrayInit(); automaticArrayInit(); return 0;
} void staticArrayInit( void ) {
static int array1[ 3 ]; for ( int i = 0; i < 3; i++ ) cout << "array1[" << i << "] = " << array1[ i ] << " "; for ( int j = 0; j < 3; j++ ) array1[ j ] = 0;
} void automaticArrayInit( void ) {
int array2[ 3 ] = { 1, 2, 3 }; for ( int i = 0; i < 3; i++ ) cout << "array2[" << i << "] = " << array2[ i ] << " "; for ( int j = 0; j < 3; j++ ) array2[ j ] = 0;
}</source>
array1[0] = 0 array1[1] = 0 array1[2] = 0 array2[0] = 1 array2[1] = 2 array 2[2] = 3 array1[0] = 0 array1[1] = 0 array1[2] = 0 array2[0] = 1 array2[1] = 2 array2[2] = 3
Use subscripting and pointer notations with arrays
<source lang="cpp">#include <iostream> using std::cout; using std::endl; int main() {
int b[] = { 10, 20, 30, 40 }; int *bPtr = b; for ( int i = 0; i < 4; i++ ) cout << *( b + i ) << "\n"; for ( int j = 0; j < 4; j++ ) cout << bPtr[ j ] << "\n"; for ( int i = 0; i < 4; i++ ) cout << *( bPtr + i ) << "\n"; return 0;
}</source>
10 20 30 40 10 20 30 40 10 20 30 40