[英] question

查看:76
本文介绍了题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

滚动两个骰子的36种可能结果。


7.18以下程序有什么作用?


1 / / Ex。 7.18:Ex07_18.cpp

2 //这个程序做什么用?

3 #include< iostream>

4使用std :: cout;

5使用std :: endl;

6

7 int whatIsThis(int [],int); //函数原型

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<< 结果是 <<结果<<结束;

17返回0; //表示成功终止

18} //结束主

19

20 //这个功能有什么作用?

21 int whatIsThis(int b [],int size)

22 {

23 if(size == 1)//基本情况

24返回b [0];

25其他//递归步骤

26返回b [size - 1] + whatIsThis(b,size - 1);

27} //结束功能whatIsThis

7.19修改图6.11的程序,玩1000场掷骰子游戏。

计划应该跟踪统计数据并回答以下问题

问题:

------------ -------------------------------------------------- ------------------


[Page 394]

1日赢了多少场比赛第20卷之后滚,第2卷,......,第20卷和




第1卷,第2卷丢失了多少游戏,......,第20卷,以及第20卷后



掷骰子的几率是多少? [注意:你应该发现掷骰子是最公平的赌场游戏之一。
。你觉得这是什么意思

这意味着什么?]


掷骰子游戏的平均长度是多少?


赢得比赛的机会是否会随着游戏时间的延长而提高?


7.20(航空预订系统)一家小型航空公司刚购买了新的b $ ba计算机自动预订系统。你被要求对新系统进行编程。

。你要编写一个程序,在航空公司唯一一架飞机的每个航班上分配

座位(容量:10个座位)。


你的程序应该显示以下菜单的替代品请类型1为头等舱请输入2作为经济。如果

的人输入1,你的程序应该在头等舱分配一个座位

部分(1-5号席位)。如果此人输入2,您的计划应在经济部分(座位6-10)分配

。你的程序应打印

a登机牌,表明该人的座位号,以及是否在飞机的头等舱或经济舱内是否为



使用一维数组来表示

飞机的座位表。将数组的所有元素初始化为0表示

所有席位都为空。在分配每个座位时,将阵列的相应

元素设置为1,表示座位不再是
可用。


当然,您的计划应该永远不会分配已经分配了
的席位。当第一堂课满员时,你的课程

应该询问这个人是否可以接受经济待遇(反之亦然)。如果是,那就做出适当的座位

分配。如果不是,则打印消息下一航班离开3

小时。


7.21以下程序做什么?


1 //前。 7.21:Ex07_21.cpp

2 //这个程序做什么用?

3 #include< iostream>

4使用std :: cout;

5使用std :: endl;

6

7 void someFunction(int [],int,int); //函数原型

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<< 数组中的值是: << endl;

15 someFunction(a,0,arraySize);

16 cout<<结束;

17返回0; //表示成功终止

18} //结束主

19

20 //这个功能有什么作用?

21 void someFunction(int b [],int current,int size)

22 {

23 if(current< size)

24 {

25 someFunction(b,当前+ 1,大小);

26 cout<< b [当前]<< " ;

27} //结束如果

28} //结束功能someFunction


- -------------------------------------------------- ----------------------------


[Page 395]

7.22使用二维数组解决以下问题。一个

公司有四个销售人员(1到4人),他们销售五种不同的产品

(1到5)。每天一次,每个销售人员都会为每个销售的不同类型的产品传递一张单据。每张单据包含以下内容:


销售员编号


产品编号


总计当天销售的该产品的美元价值


因此,每位销售人员每天传递0到5个销售单。

假设来自所有的信息上个月的单据是

可用。编写一个程序,将读取上个月销售额的所有这些信息,并通过销售人员总结

产品的总销售额。所有总数应存储在二维数组

销售额中。处理完上个月的所有信息后,以表格格式打印

结果,每个列代表一个

特定销售人员,每行代表一个特定的

产品。每行交叉总计得到每个产品的总销售额

上个月;每个列交叉总计得出上个月销售额为
的销售总额。您的表格打印输出应包括总计行右侧和

总计列底部的这些

交叉总数。


7.23(Turtle Graphics)徽标语言在

小学生中很受欢迎,使得龟图形的概念很有名。想象一下机械龟在一个C ++程序的控制下在房间里走来走去。乌龟在两个

位置之一持有一支笔,上涨或下跌。当笔落下时,乌龟会在移动时描绘出形状。当笔上升时,乌龟自由移动

而不写任何东西。在这个问题中,你将模拟乌龟的

操作并创建一个计算机化的草图板。


使用20×20阵列层被初始化为零。从包含它们的数组中读取命令

。随时跟踪当前位置

的乌龟以及笔目前是否正在上涨或

下降。假设乌龟总是从

楼层的位置(0,0)开始。一套乌龟命令你的程序必须

过程如图7.33所示。


图7.33。 Turtle图形命令。

(此项目显示在打印版本的第396页)命令

含义


1

收官笔


2

笔下来


3

右转


4

左转


5,10

向前移动10个空格(或10个以外的数字)


6

打印20×20阵列


9

数据结束(哨兵)

解决方案

2007-03-11 11 :29, ar*****@gmail.com 写道:


滚动两个骰子的36种可能结果。



有些人在那里寻找好的练习所以这是

有点你在这里张贴一些,但它可能更多如果可以的话,也很有用

也可以发布解决方案。


-

Erik Wikstr?m


ar*****@gmail.com 写道:

[长问题集]

答案是,做你自己的功课。这同样适用于是否在课堂上分配给您的是
,或者您将它们分配给自己。

您只能通过实践来学习编程。在这里发布的阅读解决方案将

帮助你很少。


一次拿一个,处理它们,发布你的解决方案。按照

FAQ说明如何发布。然后你就会学到。



Brian


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)

解决方案

On 2007-03-11 11:29, ar*****@gmail.com wrote:

The 36 possible outcomes of rolling two dice.

There are alwyas people out there looking for good exercises so it was
kind of you to post some here, but it might be more useful if you could
post a solution too.

--
Erik Wikstr?m


ar*****@gmail.com wrote:
[long problem set]
The answer is, "do your own homework". This applies equally whether
those were assigned to you in a class or you assigned them to yourself.
You only learn programming by doing. Reading solutions posted here will
help you very little.

Take those one at a time, work on them, post your solution. Follow the
FAQ instructions on how to post. Then you will learn.


Brian


这篇关于题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆