You are previewing Think Like a Programmer.

Think Like a Programmer

Cover of Think Like a Programmer by V. Anton Spraul Published by No Starch Press
  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
      2. Sliding Tile Puzzles
      3. Sudoku
      4. The Quarrasi Lock
    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
      4. 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
      2. Linked Lists
    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
      2. As-Needed Learning
    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
      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
      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
O'Reilly logo

Chapter 3. Solving Problems with Arrays

image with no caption

In the previous chapter, we limited ourselves to scalar variables, that is, variables that can hold only one value at a time. In this chapter, we’ll look at problems using the most common aggregate data structure, the array. Although arrays are simple structures with fundamental limitations, their use greatly magnifies the power of our programs.

In this chapter, we will primarily deal with actual arrays, that is, those declared with the built-in C++ syntax, such as:

int tenIntegerArray[10];

However, the techniques we discuss apply just as well to data structures with similar attributes. The most common of these ...

The best content for your career. Discover unlimited learning on demand for around $1/day.