Answer to Question 2.12
001: /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
002: /* The C++ Answer Book */
003: /* Tony Hansen */
004: /* All rights reserved. */
005: char *months[] =
006: {
007: "January", "February", "March",
008: "April", "May", "June",
009: "July", "August", "September",
010: "October", "November", "December"
011: };
012:
013: int days[] =
014: {
015: 31, 28, 31, 30, 31, 30,
016: 31, 31, 30, 31, 30, 31
017: };
005: // print the names and days of the months
006: // version 1
007: #include <stream.h>
008:
009: int main(int, char**)
010: {
011: for (int i = 0; i < 12; i++)
012: cout << months[i] << " " << days[i] << "\n";
013: return 0;
014: }
005: struct
006: {
007: char *month;
008: int day;
009: } md[] =
010: {
011: { "January", 31 },
012: { "February", 28 },
013: { "March", 31 },
014: { "April", 30 },
015: { "May", 31 },
016: { "June", 30 },
017: { "July", 31 },
018: { "August", 31 },
019: { "September", 30 },
020: { "October", 31 },
021: { "November", 30 },
022: { "December", 31 }
023: };
005: // print the names and days of the months
006: // version 2
007: #include <stream.h>
008:
009: int main(int, char**)
010: {
011: for (int i = 0; i < 12; i++)
012: cout << md[i].month << " " <<
013: md[i].day << "\n";
014: return 0;
015: }
Menu of Chapter 2 Answers
Answer to Question 2.13