### Chapter 8

#### Program 8-7

posted Apr 10, 2013, 12:52 AM by Karwan Jacksi   [ updated Apr 10, 2013, 12:52 AM ]

 Program 8-71 // This program allows the user to select a month and then2 // displays how many days are in that month. It does this3 // by "looking up" information it has stored in arrays.4 #include 5 #include 6 #include 7 using namespace std;89 int main()10 {11 const int NUM_MONTHS = 12;12 int choice;13 string name[NUM_MONTHS] = {"January", "February", "March",14 "April", "May", "June",15 "July", "August", "September",16 "October", "November", "December" };1718 int days[NUM_MONTHS] = {31, 28, 31, 30,19 31, 30, 31, 31,20 30, 31, 30, 31};2122 cout << "This program will tell you how many days are "23 << "in any month.\n\n";2425 // Display the months26 for (int month = 1; month <= NUM_MONTHS; month++)27 cout << setw(2) << month << " " << name[month-1] << endl;2829 cout << "\nEnter the number of the month you want: ";30 cin >> choice;3132 // Use the choice the user entered to get the name of33 // the month and its number of days from the arrays.34 cout << "The month of " << name[choice-1] << " has "35 << days[choice-1] << " days.\n";36 return 0;37 }Program Output with Example Input Shown in BoldThis program will tell you how many days are in any month.1 January2 February3 March4 April5 May6 June7 July8 August9 September10 October11 November12 DecemberEnter the number of the month you want: 4[Enter]The month of April has 30 days.

#### Program 8-6

posted Apr 10, 2013, 12:52 AM by Karwan Jacksi   [ updated Apr 10, 2013, 12:52 AM ]

 Program 8-61 // This program displays the number of days in each month. It uses an2 // array of string objects to hold the month names and an int array3 // to hold the number of days in each month. Both are initialized with4 // initialization lists at the time they are created.5 #include 6 #include 7 #include 8 using namespace std;910 int main()11 {12 Const int NUM_MONTHS = 12;13 string name[NUM_MONTHS] = {"January", "February", "March",14 "April", "May", "June",15 "July", "August", "September",16 "October", "November", "December" };1718 int days[NUM_MONTHS] = {31, 28, 31, 30,19 31, 30, 31, 31,20 30, 31, 30, 31};2122 for (int month = 0; month < NUM_MONTHS; month++)23 {24 cout << setw(9) << left << name[month] << " has ";25 cout << days[month] << " days.\n";26 }27 return 0;28 }Program OutputJanuary has 31 days.February has 28 days.March has 31 days.April has 30 days.May has 31 days.June has 30 days.July has 31 days.August has 31 days.September has 30 days.October has 31 days.November has 30 days.December has 31 days.

#### Program 8-5

posted Apr 10, 2013, 12:51 AM by Karwan Jacksi   [ updated Apr 10, 2013, 12:51 AM ]

 Program 8-51 // This program displays the number of days in each month.2 #include 3 #include 4 using namespace std;56 int main()7 {8 const int NUM_MONTHS = 12;9 int days[NUM_MONTHS];1011 days[0] = 31; // January12 days[1] = 28; // February13 days[2] = 31; // March14 days[3] = 30; // April15 days[4] = 31; // May16 days[5] = 30; // June17 days[6] = 31; // July18 days[7] = 31; // August19 days[8] = 30; // September20 days[9] = 31; // October21 days[10] = 30; // November22 days[11] = 31; // December2324 for (int month = 0; month < NUM_MONTHS; month++)25 {26 cout << "Month " << setw(2) << (month + 1) << " has ";27 cout << days[month] << " days.\n";28 }29 return 0;30 }Program OutputMonth 1 has 31 days.Month 2 has 28 days.Month 3 has 31 days.Month 4 has 30 days.Month 5 has 31 days.Month 6 has 30 days.Month 7 has 31 days.Month 8 has 31 days.Month 9 has 30 days.Month 10 has 31 days.Month 11 has 30 days.Month 12 has 31 days.

#### Program 8-4

posted Apr 10, 2013, 12:51 AM by Karwan Jacksi   [ updated Apr 10, 2013, 12:51 AM ]

 Program 8-41 // This program unsafely stores values beyond an array's boundary.2 // What happens depends on how your computer manages memory.3 // It MAY overwrite other memory variables. It MAY crash your computer.4 #include 5 using namespace std;67 int main()8 {9 const int SIZE = 3;10 int A[SIZE] = {1, 1, 1}; // Define A as a 3-element int array11 // holding the values 1, 1, 112 int B[SIZE]; // Define B as another 3-element int array1314 // Here is what is stored in array A15 cout << "Here are the original numbers in 3-element array A: ";16 for (int count = 0; count < 3; count++)17 cout << A[count] << " ";1819 // Attempt to store seven numbers in the 3-element array20 cout << "\n\nNow I'm storing 7 numbers in 3-element array B.";21 for (int count = 0; count < 7; count++)22 B[count] = 5;2324 // If the program is still running, display the numbers25 cout << "\nIf you see this message, the computer did not crash.";26 cout << "\n\nHere are the 7 numbers in array B : ";27 for (int count = 0; count < 7; count++)28 cout << B[count] << " ";2930 cout << "\nHere are the numbers now in array A: ";31 for (int count = 0; count < 3; count++)32 cout << A[count] << " ";3334 return 0;35 }Program OutputHere are the original numbers in 3-element array A: 1 1 1Now I'm storing 7 numbers in 3-element array B.If you see this message, the computer did not crash.Here are the 7 numbers in array B : 5 5 5 5 5 5 5Here are the numbers now in array A: 5 5 5

#### Program 8-3

posted Apr 10, 2013, 12:50 AM by Karwan Jacksi   [ updated Apr 10, 2013, 12:50 AM ]

 Program 8-31 // This program reads employee work hours from a file and stores them2 // in an int array. It uses one for loop to input the hours and another3 // for loop to display them.4 #include 5 #include 6 using namespace std;78 int main()9 {10 const int NUM_EMPLOYEES = 6; // Sets number of employees11 int hours[NUM_EMPLOYEES]; // Holds each employee's hours12 int count; // Loop counter13 ifstream datafile; // Used to read data from a file1415 // Open the data file16 datafile.open("work.dat");17 if (!datafile)18 cout << "Error opening data file\n";19 else20 { // Input hours worked by each employee21 for (count = 0; count < NUM_EMPLOYEES; count++)22 datafile >> hours[count];23 datafile.close();2425 // Display the contents of the array26 cout << "The hours worked by each employee are\n";27 for (count = 0; count < NUM_EMPLOYEES; count++)28 { cout << "Employee " << count+1 << ": ";29 cout << hours[count] << endl;30 }31 }32 return 0;33 }Program OutputThe hours worked by each employee areEmployee 1: 20Employee 2: 12Employee 3: 40Employee 4: 30Employee 5: 30Employee 6: 15

#### Program 8-2

posted Apr 10, 2013, 12:50 AM by Karwan Jacksi   [ updated Apr 10, 2013, 12:50 AM ]

 Program 8-21 // This program stores employee work hours in an int array. It uses2 // one for loop to input the hours and another for loop to display them.3 #include 4 using namespace std;56 int main()7 {8 const int NUM_EMPLOYEES = 6;9 int hours[NUM_EMPLOYEES]; // Holds hours worked for 6 employees10 int count; // Loop counter1112 // Input hours worked by each employee13 cout << "Enter the hours worked by " << NUM_EMPLOYEES14 << " employees: ";1516 for (count = 0; count < NUM_EMPLOYEES; count++)17 cin >> hours[count];1819 // Display the contents of the array20 cout << "The hours you entered are:";2122 for (count = 0; count < NUM_EMPLOYEES; count++)23 cout << " " << hours[count];2425 cout << endl;26 return 0;27 }//OutputProgram Output with Example Input Shown in BoldEnter the hours worked by 6 employees: 20 12 40 30 30 15[Enter]The hours you entered are: 20 12 40 30 30 15

#### Program 8-1

posted Apr 10, 2013, 12:49 AM by Karwan Jacksi   [ updated Apr 10, 2013, 12:49 AM ]

 Program 8-11 // This program stores employee work hours in an int array.2 #include 3 using namespace std;45 int main()6 {7 const int NUM_EMPLOYEES = 6;8 int hours[NUM_EMPLOYEES]; // Holds hours worked for 6 employees910 // Input hours worked by each employee11 cout << "Enter the hours worked by " << NUM_EMPLOYEES12 << " employees: ";13 cin >> hours[0];14 cin >> hours[1];15 cin >> hours[2];16 cin >> hours[3];17 cin >> hours[4];18 cin >> hours[5];1920 // Display the contents of the array21 cout << "The hours you entered are:";22 cout << " " << hours[0];23 cout << " " << hours[1];24 cout << " " << hours[2];25 cout << " " << hours[3];26 cout << " " << hours[4];27 cout << " " << hours[5] << endl;28 return 0;29 }//Output..Program Output with Example Input Shown in BoldEnter the hours worked by 6 employees: 20 12 40 30 30 15[Enter]The hours you entered are: 20 12 40 30 30 15

1-7 of 7