logo资料库

C语言的科学和艺术 课后习题解答(英文版).pdf

第1页 / 共404页
第2页 / 共404页
第3页 / 共404页
第4页 / 共404页
第5页 / 共404页
第6页 / 共404页
第7页 / 共404页
第8页 / 共404页
资料共404页,剩余部分请下载后查看
The Art and Science of C Instructor’s Manual Eric S. Roberts Stanford University Stanford, California
Instructor’s Manual for The Art and Science of C Converting from Pascal to C 2 Section 1 Converting from Pascal to C From the late 1970s to the early 1990s, Pascal was the standard language for introductory computer science instruction throughout the United States and in much of the rest of the world as well. Over the last several years, however, many institutions have been moving away from Pascal as the language begins to show its age. Although some schools have taken a different direction, many—including Stanford—have adopted ANSI C as the language of instruction for the introductory course. Along with its object-oriented successor C++, ANSI C is the dominant language in the industry today. Teaching C early in the curriculum gives students additional preparation in the language they are most likely to use and makes for a smoother transition to C++ when that material appears later in the curriculum. The reasons behind Stanford’s decision to adopt C in the introductory course are outlined in the “To the Instructor” section of The Art and Science of C and discussed in detail in the paper “Using C in CS1: Evaluating the Stanford experience,” which is included in Section 8 of this Instructor’s Manual. The purpose of this section is not to recapitulate those reasons but to talk instead about strategies and tactics for making the change. The two most important lessons to pass on from our experience at Stanford are the following: 1. The transition will not be easy. Anyone who suggests that changing the implementation language in an established curriculum is a simple task would be better employed marketing snake oil. The overall design of the introductory programming course depends to a surprising extent on the mechanics of the language system used to provide instruction; changing the language therefore has implications that affect the entire curriculum. The instructional staff needs to rethink how to present material, someone must rewrite the assignments, and the institutional structures supporting the introductory course must adapt to the new conditions. 2. The advantages of making the change will be greater than anticipated. At Stanford, our original goal in making the transition was to produce students in the new introductory course who programmed as effectively in C as their predecessors had in Pascal. Because C programming skills are more useful in subsequent courses, we believed that having the students attain the previous level of skill would be sufficient. What we’ve found is that students are much better programmers coming out of the revised course than they ever were before. They are more excited about the material, have worked harder to attain a level of mastery, and understand certain essential concepts—most notably modularity and data abstraction—much better than their predecessors did. There are several things you can do to simplify the transition. • Take advantage of the benefits afforded by the library-based approach. Because there are more details involved in learning C than in learning Pascal, it is important to simplify the conceptual process for students at the introductory level. In our experience, the most effective way to do so is to use the abstraction capabilities of the language to hide its complexity until the students are better able to understand it. The essential characteristics of the library-based approach are discussed in Section 2. • Prepare any affected staff for the transition. Before making a change of this magnitude, it is important to make sure that the teaching staff responsible for the course—including TAs and anyone else who has direct contact with students in the course—have a chance to prepare for the transition. In the first year, we continued to teach a Pascal-based course while running a small, experimental C section. This phased introduction gave us time to refine the course materials and develop a cadre of support staff with the necessary expertise.
Instructor’s Manual for The Art and Science of C Converting from Pascal to C 3 • Recognize that the conversion will take time to stabilize. One of the realities that we failed to understand when we first made the change at Stanford was how much we depend on the campus community as a whole to provide support for introductory teaching. In most years, students rely on older students in the dormitories for assistance. When we began to convert the introductory course, the students had no one to consult beyond the course staff, which was already overloaded trying to institute the changes. After a year, however, the informal support structures had been largely reestablished.
Instructor’s Manual for The Art and Science of C The Library-Based Approach 4 Section 2 The Library-Based Approach Using The Art and Science of C makes it possible to teach C to introductory students without forcing them to assimilate all the details that make C difficult as a first language. For this purpose, the text uses a library-based approach that emphasizes the principle of abstraction. Adopting the library-based approach has the following advantages: 1. Libraries and modular development are covered in considerable detail early in the presentation. Students using this text are exposed to the design and implementation of library packages much earlier than in most introductory courses. As a result, they complete the course with a much deeper understanding of not only the mechanics of libraries but also the associated concepts of abstraction and information hiding. By the middle of the first course, students understand the idea of an interface and appreciate the difference in perspective between the client and the implementation. This understanding makes it much easier for them to structure applications that adhere to the principles of modern software engineering. 2. The libraries conceal many of the most difficult and confusing aspects of the language. For the introductory student, the process of learning C is complicated by the fact that many of the concepts that one needs for simple programs are complex operations in the C domain. One of the classic examples is the scanf function, which requires students to use the & operator and to understand in detail the process of character I/O before they write their first program. Because they are using a library containing a much simpler collection of input functions, students do not have to confront these difficulties until much later in the course. 3. The use of libraries makes it possible to present concepts in a logical, easily assimilated order. The C programming language was designed for use by experienced programmers. As a result, understanding the underlying logic of the language often requires more detailed knowledge of programming concepts than the introductory student is likely to have. For example, C’s approach to strings makes sense if you already understand arrays, which in turn make sense if you understand pointers. From the student’s perspective, however, the internal dependencies among these concepts make learning much more difficult because strings are in fact conceptually simpler than the concepts on which strings are based. Introducing a library like strlib.h breaks the internal dependency and makes it possible to introduce the concepts in an order that makes them much easier for new students to grasp. 4. Using libraries dramatically extends a student’s ability to write exciting applications. The graphics library introduced in Chapter 7 does more than introduce the notion of a library interface. By giving the students tools to draw simple figures on the computer screen, the graphics.h interface frees them from the sterile world of the text-based user interface and opens up the more exciting domain of computer graphics. Particularly now that most students have experience working with computers that offer graphical capabilities, being able to build graphical applications is essential to keeping student interest high. 5. The text demonstrates the power of libraries by using them. The libraries in the text serve not only as tools but also as objects of study in their own right. As the text reveals the implementation of the libraries, students gain a better understanding of how libraries work and how they can be used to control the complexity of a large software application. The details and advantages of the library-based approach are considered more thoroughly in two papers included in Section 8 of this Instructor’s Manual: “Using C in CS1: Evaluating the Stanford experience” and “A C-based graphics library for CS1.”
Instructor’s Manual for The Art and Science of C Obtaining Copies of the Libraries 5 Section 3 Obtaining Copies of the Libraries To make it possible to teach ANSI C at the introductory level, The Art and Science of C is designed around several special libraries—collectively known as the cslib library—that hide much of the complexity associated with C until students are better equipped to understand how everything works. To use the text, you must have access to the cslib library on your own machine. Although the code for the cslib library is included in Appendix B of the text, it is much easier to obtain an electronic copy from the following FTP site: ftp://ftp.awl.com/cseng/authors/roberts/cs1-c With the exception of the graphics library presented in Chapter 7, the cslib library is entirely standard and requires nothing more than what is provided as part of ANSI C. Thus, if you do not intend to use the graphics library, it doesn’t matter what version of cslib you get—they are all the same. However, because the mechanics of displaying screen images depend on the hardware and software configuration of each computer, you need an implementation of the graphics library that is appropriate to your computing environment. To find the right version of the cslib library for your computing platform: 1. Check to see if your computing platform is any of the following: • A Macintosh running THINK C or Symantec C++ • An IBM PC (or clone) running the Borland C/C++ compiler • Any Unix system running the X Window System For each of these platforms, there is a corresponding implementation of the libraries specific to that computing environment. To obtain it, you should connect to the appropriate subdirectory and follow the instructions in that README file. You should also check the README file on the FTP directory to see if new library releases have been made since this guide was published. 2. If you are using one of the hardware systems from the preceding list with a different compiler or operating system, consider purchasing the software necessary to make your platform correspond to one of the standard implementations. For example, if you own an IBM PC but use some compiler other than the Borland compiler, it might be worth installing the Borland compiler so that you can use all the features provided by the libraries, including interactive graphics. Although implementations for other platforms may be released in the future, adapting the graphics library is not a high-priority task now that there is at least one implementation for each of the major hardware platforms used in most universities. 3. If you cannot obtain the desired software or are using some other type of machine, get a copy of the standard version of the library code from the standard subdirectory. This implementation defines the libraries in an ANSI-standard form but does not provide any on-screen graphics. The standard implementation of the graphics library instead writes a file containing a text representation of the image suitable for printing on any PostScript printer. 4. Although the standard library implementation is highly portable and works without change on every machine I’ve tried, there may be some systems that cannot support it. If you find that your system does not support the standard libraries for some reason, you should try the simplified version of the library, which provides the minimum level of support necessary to run the programs in the text. Subdirectory index The top-level ftp directory contains no relevant files other than a README file. The files you need for the various libraries are collected in the following subdirectories, each of which is described in more detail in the sections that follow: simplified This subdirectory contains the simplest possible version of the library code and matches the code printed in Appendix B of the text (except for two minor changes introduced to increase
Instructor’s Manual for The Art and Science of C Obtaining Copies of the Libraries 6 portability). This implementation is machine-independent and requires no system support beyond the standard ANSI libraries. The implementation of the graphics library provides no actual screen display but instead writes a PostScript file containing the image. The simplified version of the cslib library should be used only if you are unable to get the standard version or one of the platform-specific implementations to work in your environment. This subdirectory offers a more advanced version of the libraries than that used in the simplified package. One advantage of the standard version is that it is compatible with the programs in the forthcoming companion volume to The Art and Science of C, which will cover more advanced material focusing on data structures and algorithms. Most importantly, the standard version of the library includes a portable exception-handling package that makes it possible to use better error-recovery strategies. Using the standard version of the cslib library, as opposed to the simplified set, makes for a smoother transition to more advanced work. Like the simplified package, the standard package implements the graphics library in a machine-independent way by writing a PostScript file. If you are working on a system for which there is a platform-specific implementation of the graphics library (these systems are enumerated later in this list), you should use that version instead. The standard version exists so that the fundamental libraries can be used on the widest possible set of platforms. Along with the facilities provided by the standard library package, this subdirectory contains an implementation of the graphics library designed for use with the X Window System, which is available on many computers that run the Unix operating system. The implementation is designed to work with several different versions of the Unix operating system and automatically configures itself to use the appropriate code for each. This subdirectory contains an implementation of the graphics library for THINK C (or Symantec C/C++) on the Apple Macintosh. It also includes an interface that allows students to make a typescript of their computer session, which is a difficult operation with older versions of THINK C. To use this library, your Macintosh must be running System 7 of the operating system, and the THINK C compiler must be version 5.0 or later. This subdirectory contains two separate implementations of the graphics library for the Borland C/C++ compilers used on the IBM PC and its clones. The dos subdirectory provides a simple implementation of the basic graphics library on top of DOS. The windows subdirectory has a complete implementation of the graphics library that is compatible with Microsoft Windows. Except for the graphics library, the package is identical to the standard package. The pc-borland version of the libraries has been tested with Version 4 of the Borland C/C++ compiler. The same code may work with other versions of the compiler and associated software, but it is impossible to test it with every version of the compiler. This subdirectory contains documentation on the libraries and the overall approach used in The Art and Science of C. See the README file in the documents directory for more information. This subdirectory contains electronic copies of all sample programs used in the text. If you are teaching a course using this text and want solutions to the exercises, please contact your Addison-Wesley representative. standard unix-xwindows mac-think-c pc-borland documents programs Notes and disclaimers The cslib libraries are in the public domain and may be freely copied and distributed, although they remain under development. No warranties are made concerning their correctness or stability, and no user support is offered. Section 4 Designing a Syllabus
Instructor’s Manual for The Art and Science of C Designing a Syllabus 7 The structure of the syllabus for an introductory programming course depends significantly on many factors specific to the institution. Stanford, for example, is on the quarter system, which means that the course material must fit into a ten-week quarter. Schools that follow a more traditional semester calendar have more class meetings, although there is nonetheless considerable variability in the length of the term even among those institutions. Because of this variability, it is difficult to design a syllabus—or a text for that matter—that is precisely appropriate to the needs of all institutions. Beyond the variations in the academic calendar, different schools also tend to cover material at different speeds. At Stanford, we move at a relatively fast pace that may not be appropriate for all institutions. To ensure that students do not get left behind in the process, we provide an extensive infrastructure that supports the more traditional lecture component of the course. Every quarter, we hire a large number of advanced undergraduates to lead sections and provide general course assistance, which includes staffing the main computing cluster 70 hours a week. (This program is described in the paper “Using undergraduates as teaching assistants in introductory programming courses,” which appears in Section 8 of this Instructor’s Manual.) It is the fact that we have a large, easily accessible staff that makes it possible for us to move as quickly as we do, covering essentially a semester’s worth of material in a ten-week quarter. The “To the Instructor” section of the text describes each of the chapters and offers various suggestions about how to order the material. The next two pages show a typical syllabus from Stanford’s CS106A course, which encompasses the basic CS1 material from Curriculum ’78. As the syllabus shows, we cover the entire the text with the exception of the topics in Chapter 17, which are presented the following quarter in CS106B. The syllabus also shows several additions to the curriculum beyond the material in The Art and Science of C. The major extensions in the Stanford course are that • We start the quarter with a “preprogramming” exercise. At the beginning of the first programming course, it is much more important for students to learn about programming as a general problem-solving methodology than it is for them to learn all the syntactic details of the specific language used in the course. Over the years, we have found that the best way to focus the students’ attention on the more important questions of algorithmic design is devote the first three days to Richard Pattis’s fine book Karel the Robot: A Gentle Introduction to the Art of Programming. Having used this book for many years when the course was taught in Pascal, we were pleased that it continued to be very effective after the transition to C. • We include a unit on a simulated computer called CSim. Particularly when students begin to learn about arrays and pointers in C, it is essential for them to have a model of how memory and addressing work. To give students more of a hands-on sense of how the computer operates, we start this section by introducing a locally developed simulated computer called CSim. Like Donald Knuth’s MIX machine described in The Art of Computer Programming, the CSim machine has a restricted set of capabilities and a simplified instruction set that are nonetheless sufficient to illustrate basic principles of how computers work at the machine-language level. Copies of the CSim simulator and accompanying descriptive materials will become publicly available sometime in 1995; in the meantime, it is sufficient to rely on the discussion of memory organization in Chapter 11. • We include discussion of the social impact of computing. Computers play an increasingly important role in all aspects of our society and, as a result, computer professionals have a greater responsibility to ensure that their work benefits society. At Stanford, we emphasize the importance of good software engineering throughout the introductory course and talk about the risks associated with the software development process.
Instructor’s Manual for The Art and Science of C Designing a Syllabus 8 Syllabus for CS106A Autumn Quarter 1993–94 Monday Wednesday September 29 Friday October 1 Administration What is programming? Introduction to Karel Defining new instructions Control structures in Karel Read: Pattis, Chapters 1–2 Read: Pattis, Sections 3.1–3.5, 4.1–4.5, 5.1–5.4 4 6 8 Program decomposition Stepwise refinement Read: Pattis, Sections 3.8–3.9, 5.5 11 Problem solving Programming idioms Loops: for and while Introduction to C C program structure Simple input and output Values, variables, and types Arithmetic expressions Precedence Read: Roberts, Chapter 1, Sections 2.1–2.3 Due: Assignment #1 13 More problem solving Conditional testing The if and switch statement Read: Sections 2.4–2.5 15 Functions and procedures Writing your own functions Using libraries Read: Chapter 3 Due: Karel contest entry Read: Chapter 4 Read: Chapter 5 Due: Assignment #2 18 20 22 A simple graphics library Defining tools Decomposition in C More on the graphics library The contents of an interface Procedural abstraction Random numbers Defining an interface Read: Sections 7.1–7.3 Read: Section 7.4 Read: Chapter 8 Due: Assignment #3 25 27 29 Algorithms Debugging strategies Midterm Exam (90 minutes, open book) Read: Chapter 6 Read: Handout on debugging Due: Assignment #4
分享到:
收藏