Course List http://www.c-jump.com/bcc/

C++ Arrays and STL Algorithms


  1. C++ Arrays
  2. The C++ Array Type
  3. Array Declaration Example
  4. Elements of an Array
  5. Array Navigation
  6. Array Initializer
  7. C++ Standard Library algoritm header
  8. Count algoritm example
  9. What is array name ?
  10. Copy algoritm example
  11. Pseudo-random numbers
  12. Transform algoritm example
  13. Standard Library swap algoritm
  14. Array as Function Argument
  15. Two-dimensional Array
  16. Two-dimensional Array Example
  17. Multidimensional Arrays
  18. Multidimensional Array as Function Argument

1. C++ Arrays



2. The C++ Array Type



3. Array Declaration Example



4. Elements of an Array



5. Array Navigation



6. Array Initializer



7. C++ Standard Library algoritm header



8. Count algoritm example



9. What is array name ?



10. Copy algoritm example



11. Pseudo-random numbers

  • 
    #include <iostream>
    #include <cstdlib> // for rand() and srand()
    #include <ctime>
    
    int main()
    {
        srand( (unsigned)time( 0 ) );
        for(;;) {
            std::cout << rand() % 10;
            std::cout << '\t';
        }
        return 0;
    }
    
    
  • time function returns the number of seconds elapsed since midnight, January 1, 1970.

  • srand will seed new sequence of pseudo-random numbers, so that the numbers will be different every time we run.
    (The initial seed is 1.)

  • rand returns an integer pseudo-random number.


12. Transform algoritm example



13. Standard Library swap algoritm



14. Array as Function Argument

  • 
    #include <iostream>
    using namespace std;
    void foo( double dbls[] )
    {
        cout
            << "in foo() size of dbls is "
            << sizeof( dbls )
            << '\n'
            ;
    }
    int main()
    {
        double dummy[ 4 ] = { 0 };
        cout
            << "in main() size of dummy is "
            << sizeof(dummy)
            << '\n'
            ;
        foo( dummy );
        return 0;
    }
    /* Program output:
    in main() size of dummy is 32
    in foo() size of dbls is 4
    */
    
    
  • The size of an array is not available to the called function.

  • No matter what is the array type, function argument is simply a constant address of the initial element of the array.

  • Array differs from all other C++ types in such way that array is not (and cannot be) passed to a function by value!

15. Two-dimensional Array



16. Two-dimensional Array Example

  • 
    #include <iostream>
    
    // day_of_year: returns day of year:
    int day_of_year( int month, int day, int year )
    {
      // Table of the number of days in each month:
      static const int daytab[2][13] = {
        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
      };
    
      int leap =
          year%4 == 0 && year%100 != 0 || year%400 == 0;
    
      for ( int i = 1; i < month; i++ ) {
          day += daytab[leap][i];
      }
      return day;
    }
    
    
  • 
    int main()
    {
      for (;;) {
        int month;
        int day;
        int year;
    
        std::cout << "Enter M D Y: ";
        std::cin >> month;
        std::cin >> day;
        std::cin >> year;
    
        std::cout
            << "day of year: "
            << day_of_year( month, day, year )
            << std::endl
            ;
      }
    
      return 0;
    }
    
    

17. Multidimensional Arrays



18. Multidimensional Array as Function Argument