Dr. Seppi's Fall 2015 In-Class Examples
These may not run as is, some are used just to make an isolated point in class. In some cases they may contain purposefully bad examples, as discussed in class. Since I edit examples in class, these may not contain everything you saw in class.
Hello - This is a very basic example of a c++ program
Bulb - Compute power cost using floats
Types and assignment - These are just isolated lines of code demonstrating the use of variables, ints and doubles
-
-
-
-
-
-
-
Guessing Numbers - Computer picks a number and the user has to guess what it is.
-
-
-
-
Scope - A bad example of scope
-
-
-
-
-
-
-
-
-
-
-
-
Cars - a very simple, perhaps even stupid class example
-
-
-
Sort - The sort example from the book
-
For later:
Grades - a grade conversion example with interesting input checking
-
Paul Bodily's Spring 2015 In-Class Examples
examples are intended to supplement those in the text book, not replace them. Most examples have the problem stated atop the code. Note that a proper solution would begin with a flow chart, which is missing from most of these solutions. Footnotes specify principles illustrated by each example.
If, If/else, Switch Statements
-
Cougar Cash† - Charge an expense without letting the balance go below 0
-
Hot Peppers‡ - Multiple if statement to pick a pepper hotter than a certain threshold
-
* - if without else, † - if with else, ‡ - multiple if, § - nested if
Booleans, Boolean operators
* - uses booleans, † - uses boolean or, ‡ - uses boolean not, § - uses boolean and
Guessing Numbers*‡§‖ - Computer picks a number and the user has to guess what it is.
-
-
-
-
GoFundMe* - A program to track donations in order to reach a fundraising goal.
NWS Rainfall†§ - Simulate and report summary data average daily rainfall in the state of Utah.
-
* - while loop, † - for loop, ‡ - do-while loop, § - nested loops, ‖ - cin.fail(),
Common Loop Algorithms
-
GoFundMe*†‡§ - A program to track donations in order to reach a fundraising goal.
NWS Rainfall*†‡ - Simulate and report summary data average daily rainfall in the state of Utah.
-
* - summing, † - find average, ‡ - finding max/min, § - uses sentinel, ‖ - comparing adjacent numbers
Random numbers
Guessing Numbers* - Computer picks a number and the user has to guess what it is.
-
GoFundMe† - A program to track donations in order to reach a fundraising goal.
NWS Rainfall† - Simulate and report summary data average daily rainfall in the state of Utah.
* - random integers, † - random doubles
Functions
-
-
How much wood*†‡§‖ - Calculate the area of wood needed to construct certain 2D structures
* - functions within functions,
† - functions with separate declaration and definition,
‡ - function with void return type,
§ - function with no parameters,
‖ - function reused in multiple places
Arrays
All examples demonstrate arrays as function parameters
* - fill an array,
† - linear search of an array,
‡ - removing an element from a non-sorted list,
§ - copy an array,
‖ - swapping elements and selection sort,
¶ - 2d array
Vectors
Help queue*†‡¶ - A queue in which students can add their names or choose to help students on the queue
-
* - passing vector as reference parameter,
† - const reference parameter,
‡ - using getline,
§ - character functions,
‖ - read and write to/from a file,
¶ - using cin.sync()
Recursion
Classes and Objects
-
Facespace - Build a Comment class, with a comment, commenter name, and like count; then test it
Pointers
Family Feud - use an int* to point to the team score to which we want to add scored points
Super Smash Brothers - use Player* to point to which player is currently attacking and which is currently defending
-
Inheritance and Polymorphism
-
Shapes*† - An abstract shape base class and two derived classes, Rectangle and Circle
* - pure virtual function/abstract class, † - static class variable
Dr. Seppi's Examples from Fall 2014
These may not run as is, some are used just to make an isolated point in class. In some cases they may contain purposefully bad examples, as discussed in class. Since I edit examples in class, these may not contain everything you saw in class.
Hello - This is a very basic example of a c++ program
Bulb - Compute power cost using floats
Types and assignment - These are just isolated lines of code demonstrating the use of variables, ints and doubles
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Cars - a very simple, perhaps even stupid class example
-
-
-
Sort - The sort example from the book
-
For later:
Grades - a grade conversion example with interesting input checking
Back to top