Example programs by topic
Here are a variety of sample programs and code snippets,
categorized by the C++ features they make use of. (They are
roughly in the order we cover the topics.)
Basics
- i/o with formatting (scanf, printf)
- making change for cash amounts (basic i/o, calculation)
Using our own functions
- simple functions
- program to compute the cost of a car rental
- rotating parameter values (pass-by-reference)
- solving quadratic equations
Functions and recursion
- function to get an integer, recursive error checking
- Towers of Hanoi
Functions and loops
- loops and printf
Arrays, functions, and loops
- filling, summing, comparing arrays
- generate a list of primes
- Seeing if one string is an anagram of another
Arrays, functions, and recursion
- generate all the anagrams of a string
Random number generators
- Ask the octal magic ball a question
- Shuffling and displaying a deck of cards
- Computer vs player guessing game (command line arguments)
Using two-dimensional arrays
- Cannon simulation (pick an angle/velocity, try to hit the target)
- Langton's ant: simulation (uses some ascii terminal control)
Using file i/o and structs
- Program to remove comments from another C++ program
Using two-dimensional arrays and structs
- Working with a 2D matrix (2d arrays)
Aside: if you're interested in seeing the source code for linux programs such as cp, ls, mv, rm, etc. see:
git.savannah.gnu.org/cgit/coreutils.git/tree/src
Similarly, if you're interested in seeing the source code for bash commands such as alias, cd, source,
etc. see:
git.savannah.gnu.org/cgit/bash.git/tree/builtins