Chapter 8

Program 8-7

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

Program 8-7
1 // This program allows the user to select a month and then
2 // displays how many days are in that month. It does this
3 // by "looking up" information it has stored in arrays.
4 #include <iostream>
5 #include <iomanip>
6 #include <string>
7 using namespace std;
8
9 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" };
17
18 int days[NUM_MONTHS] = {31, 28, 31, 30,
19 31, 30, 31, 31,
20 30, 31, 30, 31};
21
22 cout << "This program will tell you how many days are "
23 << "in any month.\n\n";
24
25 // Display the months
26 for (int month = 1; month <= NUM_MONTHS; month++)
27 cout << setw(2) << month << " " << name[month-1] << endl;
28
29 cout << "\nEnter the number of the month you want: ";
30 cin >> choice;
31
32 // Use the choice the user entered to get the name of
33 // 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 Bold
This program will tell you how many days are in any month.
1 January
2 February
3 March
4 April
5 May
6 June
7 July
8 August
9 September
10 October
11 November
12 December
Enter 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-6
1 // This program displays the number of days in each month. It uses an
2 // array of string objects to hold the month names and an int array
3 // to hold the number of days in each month. Both are initialized with
4 // initialization lists at the time they are created.
5 #include <iostream>
6 #include <iomanip>
7 #include <string>
8 using namespace std;
9
10 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" };
17
18 int days[NUM_MONTHS] = {31, 28, 31, 30,
19 31, 30, 31, 31,
20 30, 31, 30, 31};
21
22 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 Output
January 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-5
1 // This program displays the number of days in each month.
2 #include <iostream>
3 #include <iomanip>
4 using namespace std;
5
6 int main()
7 {
8 const int NUM_MONTHS = 12;
9 int days[NUM_MONTHS];
10
11 days[0] = 31; // January
12 days[1] = 28; // February
13 days[2] = 31; // March
14 days[3] = 30; // April
15 days[4] = 31; // May
16 days[5] = 30; // June
17 days[6] = 31; // July
18 days[7] = 31; // August
19 days[8] = 30; // September
20 days[9] = 31; // October
21 days[10] = 30; // November
22 days[11] = 31; // December
23
24 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 Output
Month 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-4
1 // 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 <iostream>
5 using namespace std;
6
7 int main()
8 {
9 const int SIZE = 3;
10 int A[SIZE] = {1, 1, 1}; // Define A as a 3-element int array
11 // holding the values 1, 1, 1
12 int B[SIZE]; // Define B as another 3-element int array
13
14 // Here is what is stored in array A
15 cout << "Here are the original numbers in 3-element array A: ";
16 for (int count = 0; count < 3; count++)
17 cout << A[count] << " ";
18
19 // Attempt to store seven numbers in the 3-element array
20 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;
23
24 // If the program is still running, display the numbers
25 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] << " ";
29
30 cout << "\nHere are the numbers now in array A: ";
31 for (int count = 0; count < 3; count++)
32 cout << A[count] << " ";
33
34 return 0;
35 }

Program Output
Here are the original numbers in 3-element array A: 1 1 1
Now 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 5
Here 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-3
1 // This program reads employee work hours from a file and stores them
2 // in an int array. It uses one for loop to input the hours and another
3 // for loop to display them.
4 #include <iostream>
5 #include <fstream>
6 using namespace std;
7
8 int main()
9 {
10 const int NUM_EMPLOYEES = 6; // Sets number of employees
11 int hours[NUM_EMPLOYEES]; // Holds each employee's hours
12 int count; // Loop counter
13 ifstream datafile; // Used to read data from a file
14
15 // Open the data file
16 datafile.open("work.dat");
17 if (!datafile)
18 cout << "Error opening data file\n";
19 else
20 { // Input hours worked by each employee
21 for (count = 0; count < NUM_EMPLOYEES; count++)
22 datafile >> hours[count];
23 datafile.close();
24
25 // Display the contents of the array
26 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 Output
The hours worked by each employee are
Employee 1: 20
Employee 2: 12
Employee 3: 40
Employee 4: 30
Employee 5: 30
Employee 6: 15

Program 8-2

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

Program 8-2
1 // This program stores employee work hours in an int array. It uses
2 // one for loop to input the hours and another for loop to display them.
3 #include <iostream>
4 using namespace std;
5
6 int main()
7 {
8 const int NUM_EMPLOYEES = 6;
9 int hours[NUM_EMPLOYEES]; // Holds hours worked for 6 employees
10 int count; // Loop counter
11
12 // Input hours worked by each employee
13 cout << "Enter the hours worked by " << NUM_EMPLOYEES
14 << " employees: ";
15
16 for (count = 0; count < NUM_EMPLOYEES; count++)
17 cin >> hours[count];
18
19 // Display the contents of the array
20 cout << "The hours you entered are:";
21
22 for (count = 0; count < NUM_EMPLOYEES; count++)
23 cout << " " << hours[count];
24
25 cout << endl;
26 return 0;
27 }

//Output
Program Output with Example Input Shown in Bold
Enter 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-1
1 // This program stores employee work hours in an int array.
2 #include <iostream>
3 using namespace std;
4
5 int main()
6 {
7 const int NUM_EMPLOYEES = 6;
8 int hours[NUM_EMPLOYEES]; // Holds hours worked for 6 employees
9
10 // Input hours worked by each employee
11 cout << "Enter the hours worked by " << NUM_EMPLOYEES
12 << " 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];
19
20 // Display the contents of the array
21 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 Bold
Enter 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