CSCD-255
2015-01-23


Toolbox Time

Now that we have if/else and loops available, we can examine some uses.  You are responsible for knowing the algorithms covered today for purposes of programming and examinations.

Simple check for even and odd integers, and for zero, even, and odd integers:  nested if where (num%2 == 0) as either zero (0 itself) or even
EvenOdd.txt   EvenOdd.c   EvenOdd.exe

Expanding on constrained input range code found in 16 Jan and 22 Jan:
Range.txt   Range.c   Range.exe

Examining an integer one digit at a time.  If val holds an integer, val%10 will be the lowest order digit in the decimal number, and val/10 will discard that digit.  The little program reports the digits one at a time, and then reports the number of digits in the number.  It is constrained to report one digit for zero — the logic is in a do loop, not a while loop.
CountDigits.txt   CountDigits.c   CountDigits.exe

Checking for prime numbers, those that can only be divided by 1 and themselves.  So 1 is excluded, and division by zero excludes 0.  Hence positive numbers from 2 up.  Again there are two cases where  (num%2 == 0):   only 2 itself is prime, and all other even numbers are not prime (composite numbers).
PrimeRealEstate.txt   PrimeRealEstate.c   PrimeRealEstate.exe

New material:   rand() and srand(int) to obtain a sequence of random numbers.  C provides a pair of functions:  rand() and srand(int) rand() returns values between zero and RAND_MAX (a constant define in stdlib.h along with the prototypes for rand() and srand(int)).  To get different sequences of random number, the user calls srand(int) and passes a "seed", an integer that initializes the sequence that rand() returns.  The sequence will be identical if the same seed is used.  If srand(int) is not called, rand() will start with a seed of 1.
TinyDemo.txt   TinyDemo.c   TinyDemo.exe

The specimen program generates one throw of a pair of dice — which could be the start of your generating your own craps program.
diceroll.txt   diceroll.c   diceroll.exe

Program to drive student drill:  the students count off from 1 up to N.  The program then identifies all of the numbers in random order, then starts another sequence.  The program terminates when the user enters the word "quit" before pressing enter.
DrillSeq.txt    DrillSeq.c    DrillSeq.exe   

Enrichment material:
Advanced example (using elements of C that we have not yet covered):  explicitly measuring the probability of the 11 possible results.
CrapsProbability.txt   CrapsProbability.c   CrapsProbability.exe

100 million rolls (which took about four seconds) generated this table:

Seed for srand:  19430407
How many throws are being accumulated?  100000000
 2:   2.780%, exact result is  2.778%
 3:   5.558%, exact result is  5.556%
 4:   8.334%, exact result is  8.333%
 5:  11.111%, exact result is 11.111%
 6:  13.892%, exact result is 13.889%
 7:  16.661%, exact result is 16.667%
 8:  13.893%, exact result is 13.889%
 9:  11.107%, exact result is 11.111%
10:   8.335%, exact result is  8.333%
11:   5.554%, exact result is  5.556%
12:   2.775%, exact result is  2.778%