This book is meant for students of engineering or science who need both a useful programming language for solving problems in their disciplines and an introduction to ideas from the discipline of computer science. It is a provided free of charge to students at Vanderbilt who enroll in CS 103, “Introductory Programming for Engineers and
Scientists”.
1
It is being provided to you as a “digital” book, meaning that you are provided the digital computer files that contain it. (Because it is in this format, it is very easy to copy portions of it. You are free to do that, but if you incorporate any part of the book in other work, then you must give a proper citation, just as you would any book printed on paper.) The format of the files is compatible with Microsoft Word 1997-2007.
CS 103 provides an introduction to programming. If you study computer science or computer engineering further, you should, upon completion of this course, have a strong foundation for advancing in either area as you enroll in more advanced courses that involve programming. If this is your only course in programming, you should feel confident that you can write the programs that you need to solve the engineering or scientific problems that you encounter in your chosen discipline.
Each of the chapters of this book corresponds to approximately one or two weeks of the semester. This textbook emphasizes more of the general concepts from the discipline of computer science than does the typical introduction to Matlab programming. In addition to helping you to learn how to solve problems by writing programs in Matlab, it should also help you to see how the study of programming in Matlab relates to the study of computer science.
Much of the material is presented in a tutorial style (i.e., concepts are illustrated by means of practical examples). The early chapters include lots of introductory tutorial material.
This tutorial material has two purposes: (1) To help you get started, and (2) to reinforce your understanding of concepts that are presented in lecture. As you gain experience with
Matlab, with concepts from computer science, and with the lecture style of your instructor(s), you will be better able to absorb material more easily. As that happens, the style will become less tutorial, and the rate at which new material is introduced will increase. Unlike most books, this one does not contain sets of worked examples within each chapter, nor does it have sets of problems at the end of the chapters. Instead the examples will be presented, discussed, and worked in class while you watch and, in many cases, participate; and homework problems will be provided for you on-line via the web pages for the course. The worked examples will be available for review on line as well.
1 This book is copyrighted material. It may not be redistributed without permission from the authors, but students in CS 103 may use the book during the course and for as long as they wish after the course is over.
They make copies for their own use including paper print-outs.
ii
To help you with the understanding of the material presented in this book and in class, this book includes many homework problems and a collection of projects. Some of these problems and projects will be assigned to you. Homework problems and projects will be located in at a different link from this preface and the chapters of the book. To complete the assignments you will need to understand the concepts covered in both the book and the lectures. Furthermore, as in virtually all engineering and science courses, because later concepts build on earlier ones, you will find that each later homework assignment refers to concepts, not only from the most recent lecture and text coverage, but from previous coverage as well. Thus, as you work your way through the homework assignments in this class, you will enforce and re-enforce your understanding of concepts that you learned earlier in the semester. You will also be tested on the material covered in the homework assignments by means of in-class, graded tests. If you learn the material for the homework, you should find that studying for the tests is easy, and you should do well on the tests and in the course.
Turning off spelling and grammar checking
Many of the words in this document are in a computer programming language and are not standard English. The spelling and grammar checking tools of Microsoft Word 2 will highlight these words unless the highlighting feature is turned off. This highlighting can be quite distracting. It is recommended that you turn these features off when reading this text. To do that, click the Microsoft Office Button at the upper left. A subwindow will open. Click Word Options at the bottom of that subwindow near the right. Click
Proofing in the list at the left. A new subwindow will appear. At the bottom you will see
Exceptions for: followed by the name of the document that you have open. Click the checkboxes next to Hide spelling errors in this document only and Hide grammar errors in this document only . When they are both checked, click OK at the bottom of that subwindow.
Indexing with Find
There is no index for this book! Instead, because the book is in “soft” form, you can use the versatile “Find” tool of Word to do dynamic indexing. That tool can be accessed most easily by clicking on the tiny circle at the right, bottom of the Word window, as pointed out by the red arrow below:
2 The specific version of Word used for these suggestions is Word 2007.
iii
Clicking there will cause this smaller window to appear:
Clicking on the binoculars will bring up Find .
Alternatively, on the Home tab at the top of the Word window you can click Editing and then
Find…
. Or, you can simply press and hold the Cntl key and hit the F key. With any of these three methods, you will see the following window appear:
iv
To get the full functionality of this feature, you should click on More at the bottom. You will then see a larger window:
You can choose to use the special features by clicking the checkboxes at the lower left.
The “
Use wildcards
” box is the most versatile. If you choose that option, then you can click the “ Specia l” box below to get a list of wildcards to choose from. You can probably learn what you need to learn about wildcards by experimentation, but you can also learn everything by using the Help facility.
Getting help with Word
Word provides an excellent help facility, which can be accessed simply by double clicking the Help icon at the upper right corner of the Word window or hitting the F1 key, and then typing a phrase, such as “find” into the search box and clicking Search .
Annotating your personal copy of this book
You can easily write in your personal copy of this book and you can highlight words and sentences, just as would in a paper book. Do not write directly into the text. Instead add comments, using Word’s comment facility. Let’s consider an example. Suppose you want to annotate the first page of Chapter 1 by highlighting the definition of computer science in the second paragraph and inserting a comment about the definition of algorithm. The result might look like this:
v
To highlight, select a portion of text, then click the icon of a highlighter pen under the
Home tab. To remove the highlighting, repeat the process. To insert a comment, select a portion of text, click New Comment under the Review tab. An empty comment
“balloon” will appear in the margin. Type your comment into that balloon. (If your comments do not appear in the margin, then click Tracking under the Review tab, click
Balloons , and click to the left of Show Only Comments And Formatting In Balloons to put a checkmark there. You will find it easier to work with annotations if you turn these “balloons” on.) To delete a comment, click right on it and choose Delete
Comment .
Updating your personal copy
Periodically, you will be find that a chapter has been updated on line. When that happens, you will want to update your personal version. The simplest, but not the best, approach is simply to replace your current version with the new version. There are two problems with that approach: (1) You will not know what changes were made and (2) you will loose all your highlighting and comments. Fortunately, Word provides a way to solve both problems. Here is how you can use it. Let’s suppose that your current document, which might, for example, be Chapter 1 is called Chapter 1.doc
.
1.
Copy Chapter 1.doc
into a file called old_version.doc
, but do not open it.
2.
Copy the new version into a file called new_version.doc
.
3.
Open new_version.doc
.
4.
Click on the Review tab.
5.
Click Compare. A drop-down menu will appear. Click Compare there too.
6.
Another drop-down menu will appear. Click Compare there as well.
7.
Under Original Document select old_version.doc
.
8.
Under Revised Document select new_version.doc
.
9.
Click Compare
10.
At this point you will see a three documents in separate windows with many annotations in the document called the Compared Document , each of which indicates how the old version must be changed to become the new version. Click the “x” at the top right of the other documents to remove them from view.
11.
At the left, you will find a window in which these changes are listed. There are six categories of changes. The first of these is the only one you need to be concerned with: Main document changes and comments . Under that category there are
vi five types of changes: Insertions , Delections , Moves , Formatting , and
Comments . You will want to keep all changes other than those that eliminate your highlighting. To undo the elimination of highlighting, scroll down and find every instance of “Not Highlight”. Click right and then click Reject Format
Change . (The comments that you have added will remain unless you specifically delete them.)
12.
Now, scroll through the document. Look at each change and accept it. To accept a change, click the right mouse button anywhere inside it and select Accept
Format Change , Accept Deletion , Accept Insertion , etc. as appropriate. (To reject a change, click the right mouse button anywhere inside it and select Reject
…
as appropriate.)
As you accept changes, observe which changes are trivial corrections of typographical errors and which are important enough to take note of in your studies. You may even decide to add new highlights or comments as you examine a change. You can do that at any time during this process as well.
13.
When you have finished accept and rejecting changes to produce your new annotated version, save it as Chapter 1.doc
.
(You are probably saying “Thirteen steps! I don’t think I want to try that!” It is not difficult. Most of the steps are self-explanatory, once you get to them. The second time you do it, you will have it memorized.)
At this point you have updated your personal copy to the latest version, have inspected each change, and have kept all your own annotations.
The Matlab examples shown in this book were run using Matlab Version 6.5, Release 13,
Matlab Version 7.0.4.365 (Release 14) Service Pack 2, and Matlab 7.4.0287 (R2007a).
The C++ example was run using Microsoft Visual Studio.Net, Version 1.1.4322 SP1 and
Microsoft Visual C++, Version 7.1.3088. The operating systems used include both
Windows XP Version 2002 Service Pack 2 and Windows 2000. This book was written using Microsoft Word 2002, 2003, and 2007.
We have used the following set of fonts in this book:
vii
Section subheadings are Times New Roman Italic 12 pt.
Normal text is Times New Roman 12 pt.
Vocabulary terms are Times New Roman Bold Italic 12 pt.
Mathematical symbols (not Matlab) are Time New Roman Italic 12 pt.
Text in the Matlab Command Window is Courier New Bold 12pt,
11pt, or smaller.
Text in a Matlab M-file is Courier New(non-Bold) 12pt,
11pt, or smaller.
Text in the Matlab taskbar, menus, and submenus and filenames are Arial
12 pt or smaller.
This book was developed at Vanderbilt as part of a course for first-year students in the
School of Engineering. We would like to thank over a thousand students, who, during the eight years from 2000 through 2008, have used it, endured its early versions, and provided valuable feedback that has contributed to its current form.
J. Michael Fitzpatrick
John D. Crocetti