收藏 分销(赏)

数据结构与程序设计C++描述(Kruse著)高等教育出版社-课后答案.doc

上传人:xrp****65 文档编号:5743378 上传时间:2024-11-19 格式:DOC 页数:800 大小:7.53MB
下载 相关 举报
数据结构与程序设计C++描述(Kruse著)高等教育出版社-课后答案.doc_第1页
第1页 / 共800页
数据结构与程序设计C++描述(Kruse著)高等教育出版社-课后答案.doc_第2页
第2页 / 共800页
数据结构与程序设计C++描述(Kruse著)高等教育出版社-课后答案.doc_第3页
第3页 / 共800页
数据结构与程序设计C++描述(Kruse著)高等教育出版社-课后答案.doc_第4页
第4页 / 共800页
数据结构与程序设计C++描述(Kruse著)高等教育出版社-课后答案.doc_第5页
第5页 / 共800页
点击查看更多>>
资源描述

1、ProgrammingPrinciples 11.2 THE GAME OF LIFEExercises 1.2Determine by hand calculation what will happen to each of the configurations shown in Figure 1.1 overthe course of five generations. Suggestion: Set up the Life configuration on a checkerboard. Use onecolor of checkers for living cells in the c

2、urrent generation and a second color to mark those that will beborn or die in the next generation.Answer(a)Figure remains stable.(b)(c)(d)Figure is stable.12 Chapter 1 _ Programming Principles(e)(f)Figure repeats itself.(g)(h)(i)Figure repeats itself.(j)(k)(l)Figure repeats itself.Section 1.3 _ Prog

3、ramming Style 31.3 PROGRAMMING STYLEExercises 1.3E1. What classes would you define in implementing the following projects? What methods would your classespossess?(a) A program to store telephone numbers.Answer The program could use classes called Phone_book and Person. The methods for a Phone_bookob

4、ject would include look_up_name, add_person, remove_person. The methods for a Personobject would include Look_up_number. Additional methods to initialize and print objects ofboth classes would also be useful.(b) A program to play Monopoly.Answer The program could use classes called Game_board, Prope

5、rty, Bank, Player, and Dice. In additionto initialization and printing methods for all classes, the following methods would be useful. Theclass Game_board needs methods next_card and operate_jail. The class Property needs methodschange_owner, look_up_owner, rent, build, mortgage, and unmortgage. The

6、 class Bank needsmethods pay and collect. The class Player needs methods roll_dice, move_location, buy_propertyand 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 initializationand prin

7、ting methods. The class Game_board would also need methods make_move andis_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 The program could use classe

8、s Car, Traffic_light, and Queue. The classes would all need initializationand printing methods. The class Traffic_light would need additional methods change_statusand status. The class Queue would need additional methods add_car and remove_car.E2. Rewrite the following class definition, which is sup

9、posed to model a deck of playing cards, so that itconforms to our principles of style.class a / a deck of cardsint X; thing Y152; /* X is the location of the top card in the deck. Y1 lists the cards. */ public:a( );void Shuffle( ); / Shuffle randomly arranges the cards.thing d( ); / deals the top ca

10、rd off the deck;Answerclass Card_deck Card deck52;int top_card;public:Card_deck( );void Shuffle( );Card deal( );4 Chapter 1 _ Programming PrinciplesE3. Given the declarationsint ann, i, j;where n is a constant, determine what the following statement does, and rewrite the statement to accomplishthe s

11、ame effect in a less tricky way.for (i = 0; i n; i.)for (j = 0; j n; j.)aij = (i . 1)/(j . 1) * (j . 1)/(i . 1);Answer This statement initializes the array a with all 0s except for 1s down the main diagonal. A lesstricky way to accomplish this initialization is:for (i = 0; i n; i.)for (j = 0; j n; j

12、.)if (i = j) aij = 1;else aij = 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 parameter

13、s: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 vari

14、ablenames, 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 calcul

15、ate 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 vectormax

16、;float figure (vector vector1) int loop1, loop4; float loop2, loop3;loop1 = 0; loop2 = vector1loop1; loop3 = 0.0;loop4 = loop1; for (loop4 = 0;loop4 max; loop4.) loop1 = loop1 . 1;loop2 = vector1loop1 1;loop3 = loop2 . loop3; loop1 = loop1 1;loop2 = loop1 . 1;return(loop2 = loop3/loop2); Answer The

17、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 orange) if (apple peach) if(peach orange) return(peach); else if (ap

18、ple apple) if (peach orange) return(orange); elsereturn(peach); else return(apple); Answer The function mystery returns the middle value of its three parameters.6 Chapter 1 _ Programming Principlesint median(int a, int b, int c)/* Pre: None.Post: Returns the middle value of the three integers a, b,

19、c. */if (a b)if (c a) return a; / c a belse if (c b) return c; / a = c belse return b; / a b = celseif (c b) return b; / c b = aelse if (c a) return c; / b = c aelse return a; / b = a = cE6. The following statement is designed to check the relative sizes of three integers, which you may assumeto be

20、different from each other:if (x z) if (x y) if (y z) c = 1; else c = 2; elseif (y z) c = 3; else c = 4; else if (x y)if (x z) c = 5; else c = 6; else if (y z) c = 7; elseif (z x) if (z y) c = 8; else c = 9; else c = 10;(a) Rewrite this statement in a form that is easier to read.Answerif (x z)if (x y

21、) / x z and x yif (y z) c = 1; / x y zelse c = 2; / x z = yelse / y = x zif (y z) c = 3; / y = x zelse c = 4; / impossibleelse / z = xif (x y) / z = x yif (x z) c = 5; / impossibleelse c = 6; / z = x yelse / z = x and y = xif (y z) c = 7; / y z = x/ z = y = xif (z x) / z = y = x, z xif (z y) c = 8;

22、/ z y = xelse c = 9; / z = y x, impossibleelse c = 10; / y = z = x, impossible(b) Since there are only six possible orderings for the three integers, only six of the ten cases can actuallyoccur. Find those that can never occur, and eliminate the redundant checks.Answer The impossible cases are shown

23、 in the remarks for the preceding program segment. After theirremoval we have:if (x z)if (x y) / x z and x yif (y z) c = 1; / x y zelse c = 2; / x z = yelse c = 3; / y = x zelse / z = xif (x y) c = 6; / z = x yelse / z = x and y = xif (y z) c = 7; / y z = xelse c = 8; / z = y = xSection 1.3 _ Progra

24、mming Style 7(c) Write a simpler, shorter statement that accomplishes the same result.Answerif (x y) & (y z) c = 1;else if (x z) & (z y) c = 2;else if (y x) & (x z) c = 3;else if (z x) & (x y) c = 6;else if (y z) & (z tiny) if (april*april*april tam tolerance | x z * z * z tolerance);return z;(b) Wr

25、ite a function for calculating the cube root of x directly from the mathematical formula, by startingwith the assignment y = x and then repeatingy = (2 * y . (x/(y * y)/3until abs(y * y * y x) tolerance | x y * y * y tolerance);return y;(c) Which of these tasks is easier?Answer It is often easier to

26、 write a program fromscratch than it is to decipher and rewrite a poorly writtenprogram.E8. The mean of a sequence of numbers is their sum divided by the count of numbers in the sequence. Thestatistics (population) variance of the sequence is the mean of the squares of all numbers in the sequence, m

27、inusthe square of the mean of the numbers in the sequence. The standard deviation is the square root of thevariance. Write a well-structured C+ function to calculate the standard deviation of a sequence of nfloating-point numbers, where n is a constant and the numbers are in an array indexed from 0

28、to n1,which is a parameter to the function. Use, then write, subsidiary functions to calculate the mean andvariance.Answer #include double variance(double v, int n);double standard_deviation(double v, int n) / Standard deviation of vreturn sqrt(variance(v, n);This function uses a subsidiary function

29、 to calculate the variance.double mean(double v, int n);double variance(double v, int n)/ Find the variance for n numbers in vint i;double temp;double sum_squares = 0.;for (i = 0; i n; i.)sum_squares += vi * vi;temp = mean(v, n);return sum_squares/n temp * temp;This function in turn requires another

30、 function to calculate the mean.double mean(double v, int n) / Find the mean of an array of n numbersint i;double sum = 0.0;for (i = 0; i n; i.)sum += vi;return sum/n;Section 1.3 _ Programming Style 9E9. Design a program that will plot a given set of points on a graph. The input to the program will

31、be a textfile, each line of which contains two numbers that are the x and y coordinates of a point to be plotted.The program will use a function to plot one such pair of coordinates. The details of the function involveplotting the specificmethod of plotting and cannot be written since they depend on

32、 the requirements of the plottingequipment, which we do not know. Before plotting the points the program needs to know the maximumand minimum values of x and y that appear in its input file. The program should therefore use anotherfunction bounds that will read the whole file and determine these fou

33、r maxima and minima. Afterward,another function is used to draw and label the axes; then the file can be reset and the individual pointsplotted.(a) Write the main program, not including the functions.Answer #include #include calls.h#include bounds.c#include draw.cint main(int argc, char *argv)/ Read

34、 coordinates from file and plot coordinate pairs.ifstream file(argv1);if (file = 0) cout Can not open input points file endl;cout Usage:nt plotter input_points x y;plot(x, y);(b) Write the function bounds.Answer void bounds(ifstream &file, double &xmax, double &xmin,double &ymax, double &ymin)/ dete

35、rmine maximum and minimum values for x and ydouble x, y;file x y;xmax = xmin = x;ymax = ymin = y;while (!file.eof( ) file x y;if (x xmax)xmax = x;if (y ymax)ymax = y;(c) Write the preconditions and postconditions for the remaining functions together with appropriate documentationshowing their purposes and their requirements.Answer void draw_axes(double xmax, double xmin,double ymax, double ymin)/* Pre: The parameters xmin, xmax, ymin, and xmax give bounds for the x and y co-ordinates.Post: Draws and labels axes according to the given bounds. */void plot(double x, double y)

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服