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

上传人:h**** 文档编号:879068 上传时间:2018-11-04 格式: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 orange) if (apple peach) if(peach orange) return(peach); else if (apple 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

12、 Principlesint median(int a, int b, int c)/* Pre: None.Post: Returns the middle value of the three integers a, b, 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 =

13、cE6. The following statement is designed to check the relative sizes of three integers, which you may assumeto be different from each other:if (x tiny) if (april*april*april tamtolerance | x z * z * z tolerance);return z;(b) Write a function for calculating the cube root of x directly from the mathe

14、matical 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 write a program fromscratch than it is to decipher and rewrite a poorly

15、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, minusthe square of the mean of the numbers in the sequence. The standard d

16、eviation 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 to n1,which is a parameter to the function. Use, then write, subsidiary f

17、unctions 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 to calculate the variance.double mean(double v, int n);double variance(d

18、ouble v, int n)/ Find the variance for n numbers in vint i;double temp;double sum_squares = 0.;for (i = 0; i #include “calls.h“#include “bounds.c“#include “draw.c“int main(int argc, char *argv)/ Read coordinates from file and plot coordinate pairs.ifstream file(argv1);if (file = 0) cout x y;plot(x,

19、y);(b) Write the function bounds.Answer void bounds(ifstream 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 p

20、urposes 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)/* Pre: The para

21、meters x and y give co-ordinates of a point.Post: The point is plotted to the ouput graph. */1.4 CODING, TESTING, AND FURTHER REFINEMENTExercises 1.4E1. If you suspected that the Life program contained errors, where would be a good place to insert scaffoldinginto the main program? What information s

22、hould be printed out?Answer Since much of the programs work is done in neighbor_count, a good place would be withinthe loops of the update method, just before the switch statement. The values of row, col, andneighbor_count could be printed.E2. Take your solution to Section 1.3, Exercise E9 (designin

23、g a program to plot a set of points), and indicategood places to insert scaffolding if needed.Answer Suitable places might be after draw_axes (printing its four parameters) and (with a very smalltest file to plot) after the call to plot (printing the coordinates of the point just plotted).E3. Find s

24、uitable black-box test data for each of the following:(a) A function that returns the largest of its three parameters, which are floating-point numbers.Answereasy values: .1; 2; 3., .2; 3; 1., .3; 2; 1.typical values: .0; 0:5;9:6., .1:3; 3:5; 0:4., .2:1;3:5;1:6.extreme values: .0; 0; 0., .0; 1; 1., .1; 0; 1., .0; 0; 1.(b) A function that returns the square root of a floating-point number.Answereasy values: 1, 4, 9, 16.typical values: 0.4836, 56.7, 9762.34.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育教学资料库 > 参考答案

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。