没有合适的资源?快使用搜索试试~ 我知道了~
数据结构与程序设计(英文版)答案
4星 · 超过85%的资源 需积分: 0 20 下载量 112 浏览量
2010-06-07
12:03:58
上传
评论
收藏 3.51MB PDF 举报
温馨提示
试读
676页
数据结构与程序设计(英文版)答案,由Robert L.Kruse Alexander J.Ryba编写
资源推荐
资源详情
资源评论
Programming
Principles
1
1.2 THE GAME OF LIFE
Exercises 1.2
Determine by hand calculation what will happen to each of the configurations shown in Figure 1.1 over
the course of five generations. [Suggestion: Set up the Life configuration on a checkerboard. Use one
color of checkers for living cells in the current generation and a second color to mark those that will be
born or die in the next generation.]
Answer
(a)
Figure remains stable.
(b)
(c)
(d)
Figure is stable.
1
Section 1.3 • Programming Style 3
1.3 PROGRAMMING STYLE
Exercises 1.3
E1. What classeswouldyoudefineinimplementing the following projects? Whatmethodswould your classes
possess?
(a) A program to store telephone numbers.
Answer
The program could use classes called Phone_book and Person. The methods for a Phone_book
object would include look_up_name, add_person, remove_person. The methods for a Person
object would include Look_up_number. Additional methods to initialize and print objects of
both classes would also be useful.
(b)
A program to play Monopoly.
Answer
The program could use classes called Game_board, Property, Bank, Player, and Dice. In addition
toinitializationand printing methods forall classes, the following methods would beuseful. The
class
Game_boardneeds methods next_card and operate_jail. The class Property needs methods
change_owner, look_up_owner, rent, build, mortgage, and unmortgage. The class Bank needs
methods
payandcollect. The class Playerneeds methods roll_dice, move_location, buy_property
and pay_rent. The class Dice needs a method roll.
(c)
A program to play tic-tac-toe.
Answer
The program could use classes called Game_board and Square. The classes need initializa-
tion and printing methods. The class
Game_board would also need methods make_move and
is_game_over. The class Square would need methods is_occupied, occupied_by, and occupy.
(d)
A program to model the build up of queues of cars waiting at a busy intersection with a traffic light.
Answer
TheprogramcoulduseclassesCar, Traffic_light,andQueue. Theclasseswouldallneedinitializa-
tionand printingmethods. The class
Traffic_lightwould needadditionalmethods change_status
and status. The class Queue would need additional methods add_car and remove_car.
E2.
Rewrite the following class definition, which is supposed to model a deck of playing cards, so that it
conforms to our principles of style.
class a { // a deck of cards
int X; thing Y1[52]; /
*
X is the location of the top card in the deck. Y1 lists the cards.
*
/ pub-
lic:
a();
void
Shuffle(); // Shuffle randomly arranges the cards.
thing d()
; // deals the top card off the deck
}
;
Answer
class Card_deck {
Card deck[52];
int
top_card;
public:
Card_deck();
void
Shuffle();
Card deal();
};
4 Chapter 1 • Programming Principles
E3.
Given the declarations
int a[n][n], i, j;
where n is a constant, determine what the following statement does, and rewrite the statement to accom-
plish the same effect in a less tricky way.
for (i
=
0; i
<
n; i++)
for (j
=
0; j
<
n; j++)
a
[i][j]
=
((i
+ 1)/(j + 1))
*
((j + 1)/(i + 1));
Answer
This statement initializes the array a with all 0’s except for 1’s down the main diagonal. A less
tricky way to accomplish this initialization is:
for (i
=
0; i
<
n; i++)
for (j
=
0; j
<
n; j++)
if (i == j) a[i][j]
=
1
;
else
a[i][j]
=
0
;
E4. Rewrite the following function so that it accomplishes the same result in a less tricky way.
void does_something(int &first, int &second)
{
first
=
second − first;
second
=
second − first;
first
=
second + first;
}
Answer
The function interchanges the values of its parameters:
void swap(int &first, int &second)
/
*
Pre: The integers first and second have been initialized.
Post: The values of first and second have been switched.
*
/
{
int temp
=
first;
first
=
second
;
second
=
temp;
}
E5. Determine what each of the following functions does. Rewrite each function with meaningful variable
names, with better format, and without unnecessary variables and statements.
(a) int calculate(int apple, int orange)
{ int peach, lemon;
peach
=
0; lemon
=
0;if(apple
<
orange)
peach
=
orange
; else if (orange <= apple)
peach
=
apple
; else { peach
=
17;
lemon
=
19; }
return(peach);
}
Answer
The function calculate returns the larger of its two parameters.
int larger(int a, int b)
/
*
Pre: The integers a and b have been initialized.
Post: The value of the larger of a and b is returned.
*
/
{
if (a
<
b) return b;
return
a;
}
Section 1.3 • Programming Style 5
(b)
For this part assume the declaration typedef float vector[max];
float
figure (vector vector1)
{ int loop1, loop4; float loop2, loop3;
loop1
=
0; loop2
=
vector1[loop1]; loop3
=
0.0;
loop4
=
loop1; for (loop4
=
0;
loop4
<
max; loop4++) { loop1
=
loop1 + 1;
loop2
=
vector1[loop1 − 1];
loop3
=
loop2 + loop3; } loop1
=
loop1 − 1;
loop2
=
loop1 + 1;
return
(loop2
=
loop3/loop2); }
Answer
The function figure obtains the mean of an array of floating point numbers.
float mean(vector v)
/
*
Pre: The vector v contains max floating point values.
Post: The mean of the values in v is returned.
*
/
{
float total
=
0.0;
for
(int i
=
0; i
<
max; i++)
total += v
[i];
return
total/((float) max);
}
(c) int question(int &a17, int &stuff)
{ int another, yetanother, stillonemore;
another
=
yetanother; stillonemore
=
a17;
yetanother
=
stuff; another
=
stillonemore;
a17
=
yetanother; stillonemore
=
yetanother
;
stuff
=
another; another
=
yetanother;
yetanother
=
stuff; }
Answer
The function question interchanges the values of its parameters.
void swap(int &first, int &second)
/
*
Pre: The integers first and second have been initialized.
Post: The values of first and second have been switched.
*
/
{
int temp
=
first;
first
=
second;
second
=
temp;
}
(d) int mystery(int apple, int orange, int peach)
{ if (apple
>
orange) if (apple
>
peach) if
(peach
>
orange) return(peach); else if (apple
<
orange)
return(apple); else return(orange); else return(apple); else
if
(peach
>
apple) if (peach
>
orange) return(orange); else
return
(peach); else return(apple); }
Answer
The function mystery returns the middle value of its three parameters.
剩余675页未读,继续阅读
资源评论
- VictorDirg2012-10-29很不错- -.只是有些按钮不好用
- u0106530332014-03-18和原著匹配,是不错的参考资料
- oArcher123456782015-03-17不错的书,挺有用的
- lead0012014-05-13非常不错的书,找了很长时间,谢谢
zhoujjjjjj
- 粉丝: 3
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功