<<< Two-dimensional Array     Index     Multidimensional Arrays >>>

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;
    }
    
    
<<< Two-dimensional Array     Index     Multidimensional Arrays >>>