Division, quotient, and remainder

  1. byzero1.C. Warning: do not divide by zero. Attempts to perform double and int division by zero behave differently. On storm.cis.fordham.edu, a C++ program that attempts to perform int division by zero is assassinated by the operating system with a little bullet called a signal. An assassinated program never lives long enough to return EXIT_SUCCESS.
    jsmith@storm:~$ cd
    jsmith@storm:~$ pwd
    
    jsmith@storm:~$ wget https://markmeretzky.com/fordham/1600/src/division/byzero1.C
    jsmith@storm:~$ ls -l        (Make sure there is a new file named byzero1.C)
    
    jsmith@storm:~$ c++ byzero1.C
    byzero1.C: In function ‘int main()’:
    byzero1.C:10:20: warning: division by zero
    jsmith@storm:~$ ls -l        (Make sure there is a new file named a.out)
    
    jsmith@storm:~$ ./a.out
    inf
    The above quotient was infinity.
    
    Floating point exception (core dumped)
    jsmith@storm:~$ echo $?
    136     (This number is not EXIT_SUCCESS.  Subtract 128 to find out the cause of death.)
    
    There are many types of signals, each with its own identifying number. 136 − 128 = 8. Signal number 8 is SIGFPE, the “floating point exception” signal. It means that the C++ program was assassinated while attempting arithmetic.
  2. byzero2.C. Use an if statement to guard against attempted division by zero.
  3. division.C. Another warning: int division “truncates” (get chopped down to the next integer).
  4. remainder.C. Introducing the remainder operarator %. Even though int division truncates, you might still want to do it together with int remainder.
  5. Quotient and remainder exercises.
    1. 1613 pennies is 16 dollars and 13 cents.
      (1 dollar = 100 cents.)
    2. 27 bottles is 4 sixpacks and 3 bottles.
      (1 sixpack = 6 bottles.)
    3. 400 minutes is 6 hours and 40 minutes.
      (1 hour = 160 minutes.)
    4. 35 ounces is 2 pounds and 3 ounces.
      (1 pound = 16 ounces.)
    5. 7 quarts is 1 gallon and 3 quarts.
      (1 gallon = 4 quarts.)