how_to_think_like_a_computer_scientist_cpp_version

how_to_think_like_a_computer_scientist_cpp_version - How to...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
How to think like a computer scientist Allen B. Downey C++ Version, First Edition
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 How to think like a computer scientist C++ Version, First Edition Copyright (C) 1999 Allen B. Downey This book is an Open Source Textbook (OST). Permission is granted to reproduce, store or transmit the text of this book by any means, electrical, mechanical, or biological, in accordance with the terms of the GNU General Public License as published by the Free Software Foundation (version 2). This book is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABIL- ITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The original form of this book is LaTeX source code. Compiling this LaTeX source has the effect of generating a device-independent representation of a textbook, which can be converted to other formats and printed. All intermediate representations (including DVI and Postscript), and all printed copies of the textbook are also covered by the GNU General Public License. The LaTeX source for this book, and more information about the Open Source Textbook project, is available from http://www.cs.colby.edu/~downey/ost or by writing to Allen B. Downey, 5850 Mayflower Hill, Waterville, ME 04901. The GNU General Public License is available from www.gnu.org or by writ- ing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. This book was typeset by the author using LaTeX and dvips, which are both free, open-source programs. Cover Art, “The Magician,” by Scott H. Reed Copyright (C) 1999 Scott H. Reed All rights reserved. No part of the cover art may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electrical, me- chanical or biological, without prior written permission of the copyright holder: Scott H. Reed, 5639 Mayflower Hill, Waterville, ME 04901.
Image of page 2
Contents 1 The way of the program 1 1.1 What is a programming language? . . . . . . . . . . . . . . . . . 1 1.2 What is a program? . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.3 What is debugging? . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.3.1 Compile-time errors . . . . . . . . . . . . . . . . . . . . . 4 1.3.2 Run-time errors . . . . . . . . . . . . . . . . . . . . . . . . 4 1.3.3 Logic errors and semantics . . . . . . . . . . . . . . . . . 4 1.3.4 Experimental debugging . . . . . . . . . . . . . . . . . . . 5 1.4 Formal and natural languages . . . . . . . . . . . . . . . . . . . . 5 1.5 The first program . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.6 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2 Variables and types 11 2.1 More output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2 Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.4 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.5 Outputting variables . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.6 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.7 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.8 Order of operations . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.9 Operators for characters . . . . . . . . . . . . . . . . . . . . . . . 17 2.10 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3 Function 21 3.1 Floating-point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.2 Converting from double to int . . . . . . . . . . . . . . . . . . . 22 3.3 Math functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.4 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.5 Adding new functions . . . . . . . . . . . . . . . . . . . . . . . . 24 3.6 Definitions and uses . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.7 Programs with multiple functions . . . . . . . . . . . . . . . . . . 27 3.8 Parameters and arguments . . . . . . . . . . . . . . . . . . . . . 27 i
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
ii CONTENTS 3.9 Parameters and variables are local . . . . . . . . . . . . . . . . . 28 3.10 Functions with multiple parameters . . . . . . . . . . . . . . . . . 29 3.11 Functions with results . . . . . . . . . . . . . . . . . . . . . . . . 30 3.12 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 4 Conditionals and recursion 31 4.1 The modulus operator . . . . . . . . . . . . . . . . . . . . . . . . 31 4.2 Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . 31 4.3 Alternative execution . . . . . . . . . . . . . . . . . . . . . . . . . 32 4.4 Chained conditionals . . . . . . . . . . . . . . . . . . . . . . . . . 33 4.5 Nested conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . 33 4.6 The return statement . . . . . . . . . . . . . . . . . . . . . . . . 34 4.7 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 4.8 Infinite recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 4.9 Stack diagrams for recursive functions . . . . . . . . . . . . . . . 36 4.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 5
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern