C++ Tutorial/Array/array

Материал из C\C++ эксперт
Версия от 10:31, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

array of strings

#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;  
    }

Initializing an array

#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;
}
0
0
0
0
0
0
0
0
0
0

Initializing an array in a declaration.

#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;
}
2
7
4
8
5
4
9
7
6
3

Linear search of an array

#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;
}
14

Obtaining the number of array elements

#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;
}
There are 10 elements in the array.

Passing arrays and individual array elements to functions

#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 ;
}
200200200200200200

Static arrays are initialized to zero.

#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;
}
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

#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;
}
10
20
30
40
10
20
30
40
10
20
30
40