Cover image for Think Like a Programmer

Book description

In this one-of-a-kind text, author V. Anton Spraul breaks down the ways that programmers solve problems and teaches readers what other introductory books often ignore: how to Think Like a Programmer. Each chapter tackles a single programming concept and illustrates how you can apply these tools to real problems in innovative ways.

Table of Contents

  1. Think Like a Programmer
  2. Acknowledgments
  3. Introduction
    1. About This Book
      1. Prerequisites
      2. Chosen Topics
      3. Programming Style
      4. Exercises
      5. Why C++?
  4. 1. Strategies for Problem Solving
    1. Classic Puzzles
      1. The Fox, the Goose, and the Corn
        1. Lessons Learned
      2. Sliding Tile Puzzles
        1. Lessons Learned
      3. Sudoku
        1. Lessons Learned
      4. The Quarrasi Lock
        1. Lessons Learned
    2. General Problem-Solving Techniques
      1. Always Have a Plan
      2. Restate the Problem
      3. Divide the Problem
      4. Start with What You Know
      5. Reduce the Problem
      6. Look for Analogies
      7. Experiment
      8. Don’t Get Frustrated
    3. Exercises
  5. 2. Pure Puzzles
    1. Review of C++ Used in This Chapter
    2. Output Patterns
    3. Input Processing
      1. Breaking Down the Problem
      2. Putting the Pieces Together
    4. Tracking State
    5. Conclusion
    6. Exercises
  6. 3. Solving Problems with Arrays
    1. Review of Array Fundamentals
      1. Store
      2. Copy
      3. Retrieval and Search
        1. Searching for a Specific Value
        2. Criterion-Based Search
      4. Sort
        1. Fast-and-Easy Sorting with qsort
        2. Easy-to-Modify Sorting with Insertion Sort
      5. Compute Statistics
    2. Solving Problems with Arrays
      1. Refactoring
    3. Arrays of Fixed Data
    4. Non-scalar Arrays
    5. Multidimensional Arrays
    6. Deciding When to Use Arrays
    7. Exercises
  7. 4. Solving Problems with Pointers and Dynamic Memory
    1. Review of Pointer Fundamentals
    2. Benefits of Pointers
      1. Runtime-Sized Data Structures
      2. Resizable Data Structures
      3. Memory Sharing
    3. When to Use Pointers
    4. Memory Matters
      1. The Stack and the Heap
      2. Memory Size
      3. Lifetime
    5. Solving Pointer Problems
      1. Variable-Length Strings
        1. Checking for Special Cases
        2. Copying Dynamically Allocated Strings
      2. Linked Lists
        1. Building a List of Nodes
        2. Adding Nodes to a List
        3. List Traversal
    6. Conclusion and Next Steps
    7. Exercises
  8. 5. Solving Problems with Classes
    1. Review of Class Fundamentals
    2. Goals of Class Use
      1. Encapsulation
      2. Code Reuse
      3. Dividing the Problem
      4. Information Hiding
      5. Readability
      6. Expressiveness
    3. Building a Simple Class
      1. The Basic Class Framework
      2. Support Methods
    4. Classes with Dynamic Data
      1. Adding a Node
      2. Rearranging the List
      3. Destructor
      4. Deep Copy
      5. The Big Picture for Classes with Dynamic Memory
    5. Mistakes to Avoid
      1. The Fake Class
      2. Single-Taskers
    6. Exercises
  9. 6. Solving Problems with Recursion
    1. Review of Recursion Fundamentals
    2. Head and Tail Recursion
      1. Approach 1
      2. Approach 2
      3. Approach 1
      4. Approach 2
    3. The Big Recursive Idea
    4. Common Mistakes
      1. Too Many Parameters
      2. Global Variables
    5. Applying Recursion to Dynamic Data Structures
      1. Recursion and Linked Lists
      2. Recursion and Binary Trees
    6. Wrapper Functions
    7. When to Choose Recursion
      1. Arguments Against Recursion
    8. Exercises
  10. 7. Solving Problems with Code Reuse
    1. Good Reuse and Bad Reuse
    2. Review of Component Fundamentals
      1. Code Block
      2. Algorithms
      3. Patterns
      4. Abstract Data Types
      5. Libraries
    3. Building Component Knowledge
      1. Exploratory Learning
        1. Putting Learning into Practice
        2. Analysis of First Student Solution
      2. As-Needed Learning
        1. When to Search for a Component
        2. Finding a Component
        3. Applying the Component
        4. Analysis of Efficient Traversal Solution
    4. Choosing a Component Type
      1. Component Choice in Action
      2. Comparing the Results
    5. Exercises
  11. 8. Thinking Like a Programmer
    1. Creating Your Own Master Plan
      1. Playing to Your Strengths and Weaknesses
        1. Planning Against Coding Weaknesses
        2. Planning Against Design Weaknesses
        3. Planning for Your Strengths
      2. Putting the Master Plan Together
    2. Tackling Any Problem
      1. Finding a Way to Cheat
    3. Required Operations for Cheating at Hangman
      1. Initial Design
      2. Initial Coding
      3. Analysis of Initial Results
      4. The Art of Problem Solving
    4. Learning New Programming Skills
      1. New Languages
        1. Take the Time to Learn
        2. Start with What You Know
        3. Investigate What’s Different
        4. Study Well-Written Code
      2. New Skills for a Language You Already Know
      3. New Libraries
      4. Take a Class
    5. Conclusion
    6. Exercises
  12. Index
  13. About the Author
  14. Colophon
  15. A. Updates
  16. Copyright