Introduction to Computers and Programming using C++ and MATLAB Alex F. Bielajew The University of Michigan Department of Nuclear Engineering and Radiological Sciences 2927 Cooley Building (North Campus) 2355 Bonisteel Boulevard Ann Arbor, Michigan 48109-2104 U. S. A. Tel: 734 764 6364 Fax: 734 763 4540 email:
[email protected] c 2000—2010 Alex F Bielajew Revision: October 12, 2010
2
Preface This “book” arises out of a course I teach, a four-credit (52 hour) freshman-level course Introduction to Computers and Programming being taught in the College of Engineering at the University of Michigan. The book is in reasonably rough shape at this stage. It was assembled from my lecture notes several years ago and is under constant revision. I may never finish it! A wise person once said, “Old age happens when you dwell more on the past than on the future.” By this definition, I have found eternal youth, insofar as this book is concerned. My educational objectives are quite simple. This is not really a course in computing. It is a course in thinking, technical thinking, logical thinking, about formulating a problem, a mathematical problem, a physics problem, a game, and then executing a solution and making it “work”. I consider the computer and the ability to program it as a kind of laboratory—a laboratory to investigate practically, the theories and ideas of other technical courses. It is possible, for example, to teach a lot of Calculus to students without ever mentioning the word, and there are several examples throughout this book. This course is not about syntax. Hence, the book introduces the minimum amount syntax to get through a problem. Indeed, I even keep some syntax hidden, to encourage students to discover their own algorithms. So, if you are thinking of using this book as a technical reference in C++ or Matlab, I anticipate that you will be disappointed. The greatest value in this book, if there is any to be found, is in the exercises, problems and projects at the back of almost every chapter. The ideal way to learn a computer language is to learn a little syntax and then try it out on a computer. The ideal way to think is not to read about it, but to actually do it! The book reflects this. The material in the chapters, separated from the exercises, is worse than useless, for reading the material and not doing the problems is just a waste of time. Do the problems! Moreover, don’t ask me for the solutions! There is much more pedagogical value in a well-posed question than a well-articulated answer. OK, I’ll step off my soap box now! Several professors and many students have contributed to the ideas put into this book. Professor James Holloway and I have had endless discussion on the general problem of teaching algorithmic thinking to freshman. We still have not come to any conclusions except i
ii that it is highly challenging and rewarding. As for how it actually gets done, well, to be sure it is a works-in-progress. I spent a very enjoyable term co-teaching this course (the first time I taught it) with Professor Ken Powell. Thanks, Ken, for nursing me through that first year! Both James’s and Ken’s diverse computational backgrounds are reflected to some degree in this book. I owe a debt of gratitude to the dozens of Graduate Student Instructors who have taught with me on this course. One of them, Dan Osborne, deserves special recognition. He is one of the most gifted and committed teachers I have ever encountered. Dan and I spent hour after hour discussing the challenges of teaching computing and thinking skills to undergraduates. To the 3000 or so undergraduates, mostly freshmen, who have taken my course: Every time I teach this course I learn something new—about computing, about teaching, about the joy of ma