Problems

Problem

Find a recursive implementation for the following function:

perm(n) = the number of ways to order n items

where

perm(0) = 1 (by convention)

Can you find a non-recursive solution?

Problem

Find a recursive implementation for the following function:

choose(n, m) = the number of ways to choose m items from n items

where

choose(n, n) = choose(0, n) = 1
choose(n, m) = 0 if n < m
choose(n, 1) = choose(n, n - 1) = n

Can you find a non-recursive implementation of this function? (Hint: the solution involves perm.)

Problem

Using the control loop developed earlier, write a program that responds to the following commands:

fib n = nth Fibonacci number
tri n = nth triangle number
fact n = n factorial (= product of all ints from 1 to n)

Problem

Using the control loop developed earlier, write a program that responds to the following commands:

bin N = binary representation of N
comp N = twos complement representation of N
oct N = octal representation of N
dec N = decimal representation of N
hex N = hexadecimal representation of N

Problem: Turtle Graphics

Using the control loop developed earlier, write a program that responds to the following commands:

move N = move turtle N paces forward
heading D = turn turtle to heading D = N,E,W,S
PU = pen up
PD = pen down
display = draw the canvas
clear = erase the canvas

The turtle is an imaginary creature who holds a pen and moves around a rectangular canvas. When the turtle's pen is down, he draws a line across the canvas as he moves:

-------------------------------------
|                                   |
|                                   |
|                      t            |
|                      .            |
|                      .            |
|                      .            |
|              .........            |
|                                   |
|                                   |
|                                   |
-------------------------------------

Problem

Using Top-Down Design, finish the pipe example. Hint: The area of a sphere of radius r is:

A = 4πr3/2

Advanced Problems

Problem

Create a stack calculator that uses the control loop discussed above. The basic commands are:

push N // push N onto the stack
pop N  // remove top element from stack
top  // display top of stack
add  // replace top two elements on stack by their sum
sub  // replace top two elements on stack by their difference
mul  // replace top two elements on stack by their product
div  // replace top two elements on stack by their quotient

Throw an exception to the control loop if there aren't enough numbers on the stack.

Problem

Create and test a generic search function that takes as input a pointer to the start of an array, a pointer to the end of an array, and an item to look for. The search function returns the index of the first occurrence, or -1 if the element is not found:

int search(T* start, T* end, T item) { ??? }

Don't use standard library algorithms.

Problem

Create and test a second variant of the search function that works with vectors and iterators.

Problem

Assume points are instances of the type:

struct Point {double xc, yc; };

Create types representing triangles, rectangles, and circles. For example, a triangle might be a struct consisting of the three vertices of the triangle. A rectangle might be a struct consisting of the upper left corner, height, and width; etc.

Create area and perimeter functions for each type. Take advantage of overloading by giving them all the same name.

Problem

Represent bank accounts as structs.

struct Account
{
   double balance;
   ??? statement;
   bool internal; // true => balance >= 0
}

Implement global deposit and withdraw functions. Also provide a print function that prints the balance and statement of an account parameter.

Each account should have a statement, which is a dynamic array (or vector) containing the date and amount of all deposits and withdrawals.

Add a flag indicating if the account is internal or external. Internal accounts must never have negative balances. External accounts can have negative balances. Initially, the balance of an account is $0.

Problem

An account management system allows users to create accounts and to transfer funds from one account to another. Here are the commands:

internal NAME // creates a new internal account named NAME
external NAME // creates a new external account named NAME
transfer FROM TO AMT // transfer AMT from FROM account to TO account
display NAME // display balance and statement of account NAME
// save NAME // save all accounts to a file called NAME
// load NAME // load all accounts from a file called NAME

Your account manager should guarantee the sum of all balances is always $0