C++ source files demonstrating some of the functionality of the language.
also see: posts
— 97 files —
Output, Input, Variables, Arithmetic
Hello example
More output
Variables, input
Variables, input
Division
Arithmetic
Arithmetic, casting
Arithmetic, rounding
Remainders
Change in coins
Decisions
Detect valid input with if
Even or odd using if
Even or odd using if/else
Compound statements allow multiple actions
Enter even: with 2nd chance
Enter even: use while to force correct input
Enter even: 3 strikes, uses while and if
Square roots: loop with while
Powers of 2: loop with while
Reverse digits in a number
Reverse digits in a number, save result
Negated decisions using ! (not)
Compound decisions using || (or)
Compound decisions using && (and)
Testing for prime numbers
Loops
for loop
Column of stars, use for loop
Row of stars, use for loop
Sum of 10 odd numbers: for loop
Square pattern: nested for loop
Table of odd sums: nested for loop
Odd sums: nested for loop
Triangle pattern: nested for loop
Downward Triangle pattern: nested for loop
Arrow pattern: nested for loop
Cross pattern: nested for loop
Sums of cubes: nested for loop
Print numbers with a given digit sum
Functions
Use sqrt() to solve quadratics
Use rand() and srand() to simulate dice
Function to calculate squares
Functions to draw triangle pattern
Triangular numbers
Prime numbers
Factors
Functions to draw diamond pattern
Square Roots
Example of call by reference
Swap using call by reference
Draw triangle using call by reference
Order 2 numbers
Order 3 numbers
Factorials, by recursion
number of digits, by recursion
first digit (recursion)
sum digits (recursion)
reverse digits (recursion)
triangle (recursion)
remove first digit (recursion)
remove zeros (recursion)
factors (recursion)
gcd, Euclid’s algorithm (recursion)
conversion to binary (recursion)
Arrays
Array example
Array example
Find below average scores: Array example
Scores, version 2: Array example
Code digits as letters: Array example
Extract a specified digit
Change the base of a number
Draw a bar graph
Two dimensional array example
Draw a bar graph — use 2d array
Quiz scores, with 2d-array
Pictures, with 2d-array
Arrays as function parameters
Function for minimum array entry
Function for array average
Function for minimum 2d-array entry
swap array entries
sort array entries, version 1
sort array entries, version 2
Pictures with 2d-arrays and functions
recursive function for array minimum
recursive function for array reversal
recursive insertion sort
recursive insertion sort, version from class
Strings and Characters
Character Arithmetic
length function for strings and C-strings
concatenation of strings and C-strings
extraction of characters from strings and C-strings
editing a string
searching a string
read a line of input
strings and functions
strings and functions
sorting strings