A
ar30067
The 36 possible outcomes of rolling two dice.
7.18 What does the following program do?
1 // Ex. 7.18: Ex07_18.cpp
2 // What does this program do?
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 int whatIsThis( int [], int ); // function prototype
8
9 int main()
10 {
11 const int arraySize = 10;
12 int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
13
14 int result = whatIsThis( a, arraySize );
15
16 cout << "Result is " << result << endl;
17 return 0; // indicates successful termination
18 } // end main
19
20 // What does this function do?
21 int whatIsThis( int b[], int size )
22 {
23 if ( size == 1 ) // base case
24 return b[ 0 ];
25 else // recursive step
26 return b[ size - 1 ] + whatIsThis( b, size - 1 );
27 } // end function whatIsThis
7.19 Modify the program of Fig. 6.11 to play 1000 games of craps. The
program should keep track of the statistics and answer the following
questions:
--------------------------------------------------------------------------------
[Page 394]
How many games are won on the 1st roll, 2nd roll, ..., 20th roll, and
after the 20th roll?
How many games are lost on the 1st roll, 2nd roll, ..., 20th roll, and
after the 20th roll?
What are the chances of winning at craps? [ Note: You should discover
that craps is one of the fairest casino games. What do you suppose
this means?]
What is the average length of a game of craps?
Do the chances of winning improve with the length of the game?
7.20 ( Airline Reservations System) A small airline has just purchased
a computer for its new automated reservations system. You have been
asked to program the new system. You are to write a program to assign
seats on each flight of the airline's only plane (capacity: 10 seats).
Your program should display the following menu of alternativesPlease
type 1 for "First Class" and Please type 2 for "Economy". If the
person types 1, your program should assign a seat in the first class
section (seats 1-5). If the person types 2, your program should assign
a seat in the economy section (seats 6-10). Your program should print
a boarding pass indicating the person's seat number and whether it is
in the first class or economy section of the plane.
Use a one-dimensional array to represent the seating chart of the
plane. Initialize all the elements of the array to 0 to indicate that
all seats are empty. As each seat is assigned, set the corresponding
elements of the array to 1 to indicate that the seat is no longer
available.
Your program should, of course, never assign a seat that has already
been assigned. When the first class section is full, your program
should ask the person if it is acceptable to be placed in the economy
section (and vice versa). If yes, then make the appropriate seat
assignment. If no, then print the message "Next flight leaves in 3
hours".
7.21 What does the following program do?
1 // Ex. 7.21: Ex07_21.cpp
2 // What does this program do?
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 void someFunction( int [], int, int ); // function prototype
8
9 int main()
10 {
11 const int arraySize = 10;
12 int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
13
14 cout << "The values in the array are:" << endl;
15 someFunction( a, 0, arraySize );
16 cout << endl;
17 return 0; // indicates successful termination
18 } // end main
19
20 // What does this function do?
21 void someFunction( int b[], int current, int size )
22 {
23 if ( current < size )
24 {
25 someFunction( b, current + 1, size );
26 cout << b[ current ] << " ";
27 } // end if
28 } // end function someFunction
--------------------------------------------------------------------------------
[Page 395]
7.22 Use a two-dimensional array to solve the following problem. A
company has four salespeople (1 to 4) who sell five different products
(1 to 5). Once a day, each salesperson passes in a slip for each
different type of product sold. Each slip contains the following:
The salesperson number
The product number
The total dollar value of that product sold that day
Thus, each salesperson passes in between 0 and 5 sales slips per day.
Assume that the information from all of the slips for last month is
available. Write a program that will read all this information for
last month's sales and summarize the total sales by salesperson by
product. All totals should be stored in the two-dimensional array
sales. After processing all the information for last month, print the
results in tabular format with each of the columns representing a
particular salesperson and each of the rows representing a particular
product. Cross total each row to get the total sales of each product
for last month; cross total each column to get the total sales by
salesperson for last month. Your tabular printout should include these
cross totals to the right of the totaled rows and to the bottom of the
totaled columns.
7.23 ( Turtle Graphics ) The Logo language, which is popular among
elementary school children, made the concept of turtle graphics
famous. Imagine a mechanical turtle that walks around the room under
the control of a C++ program. The turtle holds a pen in one of two
positions, up or down. While the pen is down, the turtle traces out
shapes as it moves; while the pen is up, the turtle moves about freely
without writing anything. In this problem, you will simulate the
operation of the turtle and create a computerized sketchpad as well.
Use a 20-by-20 array floor that is initialized to zeros. Read commands
from an array that contains them. Keep track of the current position
of the turtle at all times and whether the pen is currently up or
down. Assume that the turtle always starts at position (0, 0) of the
floor with its pen up. The set of turtle commands your program must
process are shown in Fig. 7.33.
Figure 7.33. Turtle graphics commands.
(This item is displayed on page 396 in the print version) Command
Meaning
1
Pen up
2
Pen down
3
Turn right
4
Turn left
5,10
Move forward 10 spaces (or a number other than 10)
6
Print the 20-by-20 array
9
End of data (sentinel)
7.18 What does the following program do?
1 // Ex. 7.18: Ex07_18.cpp
2 // What does this program do?
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 int whatIsThis( int [], int ); // function prototype
8
9 int main()
10 {
11 const int arraySize = 10;
12 int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
13
14 int result = whatIsThis( a, arraySize );
15
16 cout << "Result is " << result << endl;
17 return 0; // indicates successful termination
18 } // end main
19
20 // What does this function do?
21 int whatIsThis( int b[], int size )
22 {
23 if ( size == 1 ) // base case
24 return b[ 0 ];
25 else // recursive step
26 return b[ size - 1 ] + whatIsThis( b, size - 1 );
27 } // end function whatIsThis
7.19 Modify the program of Fig. 6.11 to play 1000 games of craps. The
program should keep track of the statistics and answer the following
questions:
--------------------------------------------------------------------------------
[Page 394]
How many games are won on the 1st roll, 2nd roll, ..., 20th roll, and
after the 20th roll?
How many games are lost on the 1st roll, 2nd roll, ..., 20th roll, and
after the 20th roll?
What are the chances of winning at craps? [ Note: You should discover
that craps is one of the fairest casino games. What do you suppose
this means?]
What is the average length of a game of craps?
Do the chances of winning improve with the length of the game?
7.20 ( Airline Reservations System) A small airline has just purchased
a computer for its new automated reservations system. You have been
asked to program the new system. You are to write a program to assign
seats on each flight of the airline's only plane (capacity: 10 seats).
Your program should display the following menu of alternativesPlease
type 1 for "First Class" and Please type 2 for "Economy". If the
person types 1, your program should assign a seat in the first class
section (seats 1-5). If the person types 2, your program should assign
a seat in the economy section (seats 6-10). Your program should print
a boarding pass indicating the person's seat number and whether it is
in the first class or economy section of the plane.
Use a one-dimensional array to represent the seating chart of the
plane. Initialize all the elements of the array to 0 to indicate that
all seats are empty. As each seat is assigned, set the corresponding
elements of the array to 1 to indicate that the seat is no longer
available.
Your program should, of course, never assign a seat that has already
been assigned. When the first class section is full, your program
should ask the person if it is acceptable to be placed in the economy
section (and vice versa). If yes, then make the appropriate seat
assignment. If no, then print the message "Next flight leaves in 3
hours".
7.21 What does the following program do?
1 // Ex. 7.21: Ex07_21.cpp
2 // What does this program do?
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 void someFunction( int [], int, int ); // function prototype
8
9 int main()
10 {
11 const int arraySize = 10;
12 int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
13
14 cout << "The values in the array are:" << endl;
15 someFunction( a, 0, arraySize );
16 cout << endl;
17 return 0; // indicates successful termination
18 } // end main
19
20 // What does this function do?
21 void someFunction( int b[], int current, int size )
22 {
23 if ( current < size )
24 {
25 someFunction( b, current + 1, size );
26 cout << b[ current ] << " ";
27 } // end if
28 } // end function someFunction
--------------------------------------------------------------------------------
[Page 395]
7.22 Use a two-dimensional array to solve the following problem. A
company has four salespeople (1 to 4) who sell five different products
(1 to 5). Once a day, each salesperson passes in a slip for each
different type of product sold. Each slip contains the following:
The salesperson number
The product number
The total dollar value of that product sold that day
Thus, each salesperson passes in between 0 and 5 sales slips per day.
Assume that the information from all of the slips for last month is
available. Write a program that will read all this information for
last month's sales and summarize the total sales by salesperson by
product. All totals should be stored in the two-dimensional array
sales. After processing all the information for last month, print the
results in tabular format with each of the columns representing a
particular salesperson and each of the rows representing a particular
product. Cross total each row to get the total sales of each product
for last month; cross total each column to get the total sales by
salesperson for last month. Your tabular printout should include these
cross totals to the right of the totaled rows and to the bottom of the
totaled columns.
7.23 ( Turtle Graphics ) The Logo language, which is popular among
elementary school children, made the concept of turtle graphics
famous. Imagine a mechanical turtle that walks around the room under
the control of a C++ program. The turtle holds a pen in one of two
positions, up or down. While the pen is down, the turtle traces out
shapes as it moves; while the pen is up, the turtle moves about freely
without writing anything. In this problem, you will simulate the
operation of the turtle and create a computerized sketchpad as well.
Use a 20-by-20 array floor that is initialized to zeros. Read commands
from an array that contains them. Keep track of the current position
of the turtle at all times and whether the pen is currently up or
down. Assume that the turtle always starts at position (0, 0) of the
floor with its pen up. The set of turtle commands your program must
process are shown in Fig. 7.33.
Figure 7.33. Turtle graphics commands.
(This item is displayed on page 396 in the print version) Command
Meaning
1
Pen up
2
Pen down
3
Turn right
4
Turn left
5,10
Move forward 10 spaces (or a number other than 10)
6
Print the 20-by-20 array
9
End of data (sentinel)