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.

For later:

  • Grades - a grade conversion example with interesting input checking
  • Sound - fun with sound

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

* - if without else, † - if with else, ‡ - multiple if, § - nested if

Booleans, Boolean operators

* - uses booleans, † - uses boolean or, ‡ - uses boolean not, § - uses boolean and

While, For, Do-While Loops, Input Validation

* - while loop, † - for loop, ‡ - do-while loop, § - nested loops, ‖ - cin.fail(),

Common Loop Algorithms

* - 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.
  • Generating random doubles† - Demonstrates simply how to generate a random floating-point number between two bounds.
  • 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

* - 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
  • Missionary Referral Manager*†‡§‖¶ - Add to, sort, import, and export a list of referral names

* - 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

  • Managing a movie collection - Build a Movie class, with a title, a duration, and a rating; then test it
  • 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
  • Airplane Seating and Checking - use vector and 2D array of Passenger* to view and edit passenger's seating and checked baggage information

Inheritance and Polymorphism

* - 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.

For later:

  • Grades - a grade conversion example with interesting input checking
cs-142/old-examples.txt · Last modified: 2016/07/22 08:42 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0