├── 10 ├── exercises │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ └── 03 │ │ └── README.md └── projects │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── 4.c │ └── README.md │ ├── 05 │ ├── 5.c │ └── README.md │ ├── 06 │ ├── 6.c │ └── README.md │ └── 07 │ ├── 7.c │ └── README.md ├── 11 ├── exercises │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ └── 08 │ │ └── README.md └── projects │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ └── 04 │ ├── 4.c │ └── README.md ├── 12 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 12 │ │ └── README.md │ ├── 13 │ │ └── README.md │ ├── 14 │ │ └── README.md │ ├── 15 │ │ └── README.md │ ├── 16 │ │ └── README.md │ ├── 17 │ │ └── README.md │ ├── 18 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── 4.c │ └── README.md │ ├── 05 │ ├── 5.c │ └── README.md │ ├── 06 │ ├── 6.c │ └── README.md │ └── 07 │ ├── 7.c │ └── README.md ├── 13 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 12 │ │ └── README.md │ ├── 13 │ │ └── README.md │ ├── 14 │ │ └── README.md │ ├── 15 │ │ └── README.md │ ├── 16 │ │ └── README.md │ ├── 17 │ │ └── README.md │ ├── 18 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 10 │ ├── 10.c │ └── README.md │ ├── 11 │ ├── 11.c │ └── README.md │ ├── 12 │ ├── 12.c │ └── README.md │ ├── 13 │ ├── 13.c │ └── README.md │ ├── 14 │ ├── 14.c │ └── README.md │ ├── 15 │ ├── 15.c │ └── README.md │ ├── 16 │ ├── 16.c │ └── README.md │ ├── 17 │ ├── 17.c │ └── README.md │ ├── 18 │ ├── 18.c │ └── README.md │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── README.md │ └── reverse.c │ ├── 05 │ ├── README.md │ └── sum.c │ ├── 06 │ ├── 6.c │ └── README.md │ ├── 07 │ ├── 7.c │ └── README.md │ ├── 08 │ ├── 8.c │ └── README.md │ └── 09 │ ├── 9.c │ └── README.md ├── 14 └── exercises │ ├── 10 │ └── README.md │ ├── 11 │ └── README.md │ ├── 12 │ └── README.md │ ├── 13 │ └── README.md │ ├── 14 │ └── README.md │ ├── 15 │ └── README.md │ ├── 16 │ └── README.md │ ├── 01 │ └── README.md │ ├── 02 │ └── README.md │ ├── 03 │ └── README.md │ ├── 04 │ └── README.md │ ├── 05 │ └── README.md │ ├── 06 │ └── README.md │ ├── 07 │ └── README.md │ ├── 08 │ └── README.md │ └── 09 │ └── README.md ├── 15 ├── exercises │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ ├── README.md │ │ └── makefile │ └── 06 │ │ └── README.md └── projects │ ├── 01 │ ├── Makefile │ ├── README.md │ ├── justify.c │ ├── line.c │ ├── line.h │ ├── new_output.txt │ ├── old_output.txt │ ├── quote.txt │ ├── word.c │ └── word.h │ ├── 02 │ ├── Makefile │ ├── README.md │ ├── justify.c │ ├── line.c │ ├── line.h │ ├── new_output.txt │ ├── old_output.txt │ ├── quote.txt │ ├── word.c │ └── word.h │ ├── 03 │ ├── Makefile │ ├── README.md │ ├── qsort.c │ ├── quicksort.c │ └── quicksort.h │ ├── 04 │ ├── Makefile │ ├── README.md │ ├── readline.c │ ├── readline.h │ └── remind.c │ └── 05 │ ├── Makefile │ ├── README.md │ ├── calc.c │ ├── stack.c │ └── stack.h ├── 16 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 12 │ │ └── README.md │ ├── 13 │ │ └── README.md │ ├── 14 │ │ └── README.md │ ├── 15 │ │ └── README.md │ ├── 16 │ │ └── README.md │ ├── 17 │ │ └── README.md │ ├── 18 │ │ └── README.md │ ├── 19 │ │ └── README.md │ ├── 20 │ │ └── README.md │ ├── 21 │ │ └── README.md │ ├── 22 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── Makefile │ ├── README.md │ ├── inventory.c │ ├── inventory.h │ ├── quicksort.c │ ├── quicksort.h │ ├── readline.c │ └── readline.h │ ├── 03 │ ├── Makefile │ ├── README.md │ ├── inventory.c │ ├── inventory.h │ ├── quicksort.c │ ├── quicksort.h │ ├── readline.c │ └── readline.h │ ├── 04 │ ├── Makefile │ ├── README.md │ ├── inventory.c │ ├── inventory.h │ ├── quicksort.c │ ├── quicksort.h │ ├── readline.c │ └── readline.h │ ├── 05 │ ├── 5.c │ └── README.md │ └── 06 │ ├── 6.c │ └── README.md ├── 17 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 12 │ │ └── README.md │ ├── 13 │ │ └── README.md │ ├── 14 │ │ └── README.md │ ├── 15 │ │ └── README.md │ ├── 16 │ │ └── README.md │ ├── 17 │ │ └── README.md │ ├── 18 │ │ └── README.md │ ├── 19 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ ├── README.md │ │ └── stack.c │ └── 09 │ │ └── README.md └── projects │ ├── 01 │ ├── Makefile │ ├── README.md │ ├── inventory.c │ ├── readline.c │ └── readline.h │ ├── 02 │ ├── Makefile │ ├── README.md │ ├── inventory.c │ ├── inventory.h │ ├── readline.c │ └── readline.h │ ├── 03 │ ├── Makefile │ ├── README.md │ ├── inventory2.c │ ├── readline.c │ └── readline.h │ ├── 04 │ ├── Makefile │ ├── README.md │ ├── justify.c │ ├── line.c │ ├── line.h │ ├── newquote.txt │ ├── quote.txt │ ├── word.c │ └── word.h │ ├── 05 │ ├── Makefile │ ├── README.md │ └── sortwords.c │ ├── 06 │ ├── Makefile │ ├── README.md │ └── sortwords.c │ └── 07 │ ├── Makefile │ ├── README.md │ └── remind2.c ├── 18 └── exercises │ ├── 10 │ └── README.md │ ├── 11 │ └── README.md │ ├── 12 │ └── README.md │ ├── 13 │ └── README.md │ ├── 14 │ └── README.md │ ├── 15 │ └── README.md │ ├── 01 │ └── README.md │ ├── 02 │ └── README.md │ ├── 03 │ └── README.md │ ├── 04 │ └── README.md │ ├── 05 │ └── README.md │ ├── 06 │ └── README.md │ ├── 07 │ └── README.md │ ├── 08 │ └── README.md │ └── 09 │ └── README.md ├── 19 ├── exercises │ ├── 01 │ │ ├── README.md │ │ └── queue.h │ ├── 02 │ │ ├── README.md │ │ └── stack2.c │ ├── 03 │ │ ├── README.md │ │ ├── queue.c │ │ └── queue2.c │ ├── 04 │ │ ├── README.md │ │ ├── stack.c │ │ ├── stack.h │ │ └── stack2.c │ ├── 05 │ │ ├── README.md │ │ └── queue.h │ ├── 06 │ │ ├── README.md │ │ ├── stackADT.c │ │ ├── stackADT2.c │ │ └── stackADT3.c │ └── 07 │ │ ├── README.md │ │ └── stackADT2.c └── projects │ ├── 01 │ ├── Makefile │ ├── README.md │ ├── stack.c │ ├── stack.h │ └── stackclient.c │ ├── 02 │ ├── Makefile │ ├── README.md │ ├── stack.c │ ├── stack.h │ └── stackclient.c │ ├── 03 │ ├── Makefile │ ├── README.md │ ├── stackADT.h │ ├── stackADT3.c │ └── stackclient.c │ ├── 04 │ ├── Makefile │ ├── README.md │ ├── stackADT.h │ ├── stackADT3.c │ └── stackclient.c │ ├── 05 │ ├── Makefile │ ├── README.md │ ├── queue.h │ ├── queueADT.c │ └── queueclient.c │ ├── 06 │ ├── Makefile │ ├── README.md │ ├── queue.h │ ├── queueADT.c │ └── queueclient.c │ └── 07 │ ├── Makefile │ ├── README.md │ ├── queue.h │ ├── queueADT.c │ └── queueclient.c ├── 20 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 12 │ │ └── README.md │ ├── 13 │ │ └── README.md │ ├── 14 │ │ └── README.md │ ├── 15 │ │ └── README.md │ ├── 16 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ ├── README.md │ │ └── swap_bytes.c │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ └── 01 │ ├── 1.c │ └── README.md ├── 21 ├── exercises │ ├── 01 │ │ ├── README.md │ │ └── include.txt │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ └── 07 │ │ └── README.md └── projects │ └── 01 │ ├── 1.c │ └── README.md ├── 22 ├── exercises │ ├── 10 │ │ ├── README.md │ │ └── fcopy.c │ ├── 11 │ │ └── README.md │ ├── 12 │ │ └── README.md │ ├── 13 │ │ └── README.md │ ├── 14 │ │ └── README.md │ ├── 15 │ │ └── README.md │ ├── 16 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 10 │ ├── Makefile │ ├── README.md │ ├── inventory2.c │ ├── readline.c │ └── readline.h │ ├── 11 │ ├── 11.c │ └── README.md │ ├── 12 │ ├── Makefile │ ├── README.md │ ├── items.c │ ├── readline.c │ └── readline.h │ ├── 13 │ ├── Makefile │ ├── README.md │ ├── flights.dat │ ├── flighttimes.c │ ├── readline.c │ └── readline.h │ ├── 14 │ ├── README.md │ └── encrypt.c │ ├── 15 │ ├── Makefile │ ├── README.md │ ├── justify.c │ ├── line.c │ ├── line.h │ ├── outquote.txt │ ├── quote.txt │ ├── word.c │ └── word.h │ ├── 16 │ ├── README.md │ └── fcopy.c │ ├── 17 │ ├── 17.c │ ├── README.md │ └── input.txt │ ├── 18 │ ├── 18.c │ ├── README.md │ └── input.txt │ ├── 19 │ ├── README.md │ ├── unix2win.c │ └── win2unix.c │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── README.md │ ├── cover.jpg │ └── fcat.c │ ├── 04 │ ├── README.md │ ├── fchar.c │ ├── fline.c │ └── fword.c │ ├── 05 │ ├── README.md │ └── xor.c │ ├── 06 │ ├── 6.c │ └── README.md │ ├── 07 │ ├── README.md │ ├── compress_file.c │ └── uncompress_file.c │ ├── 08 │ ├── Makefile │ ├── README.md │ ├── inventory.c │ ├── readline.c │ └── readline.h │ └── 09 │ ├── 9.c │ └── README.md ├── 23 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 12 │ │ └── README.md │ ├── 13 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 01 │ ├── 1.c │ ├── Makefile │ ├── README.md │ └── quadratic-formula.png │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── 4.c │ └── README.md │ ├── 05 │ ├── 5.c │ ├── Makefile │ └── README.md │ ├── 06 │ ├── 6.c │ └── README.md │ └── 07 │ ├── 7.c │ └── README.md ├── 24 └── exercises │ ├── 01 │ └── README.md │ ├── 02 │ └── README.md │ ├── 03 │ ├── README.md │ └── stackADT2.c │ ├── 04 │ └── README.md │ └── 05 │ ├── README.md │ └── inventory.c ├── 25 ├── exercises │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ └── 07 │ │ └── README.md └── projects │ ├── 01 │ ├── 1.c │ └── README.md │ └── 02 │ ├── 2.c │ └── README.md ├── 26 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 12 │ │ └── README.md │ ├── 13 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── 4.c │ └── README.md │ ├── 05 │ ├── 5.c │ └── README.md │ └── 06 │ ├── 6a.c │ ├── 6b.c │ ├── 6c.c │ └── README.md ├── 27 ├── exercises │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ └── 04 │ │ └── README.md └── projects │ ├── 01 │ ├── Makefile │ ├── README.md │ └── quadratic.c │ ├── 02 │ ├── 2.c │ ├── Makefile │ └── README.md │ ├── 03 │ ├── 3.c │ ├── Makefile │ └── README.md │ └── 04 │ ├── 4.c │ ├── Makefile │ └── README.md ├── 02 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 01 │ │ ├── 1.c │ │ ├── 1b.c │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ ├── README.md │ │ └── dweight.c │ ├── 04 │ │ ├── 4.c │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── 4.c │ └── README.md │ ├── 05 │ ├── 5.c │ └── README.md │ ├── 06 │ ├── 6.c │ └── README.md │ ├── 07 │ ├── 7.c │ └── README.md │ └── 08 │ ├── 8.c │ └── README.md ├── 03 ├── exercises │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ └── 06 │ │ ├── README.md │ │ └── addfrac.c └── projects │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── 4.c │ └── README.md │ ├── 05 │ ├── 5.c │ └── README.md │ └── 06 │ ├── 6.c │ └── README.md ├── 04 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 12 │ │ └── README.md │ ├── 13 │ │ └── README.md │ ├── 14 │ │ └── README.md │ ├── 15 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── 4.c │ └── README.md │ ├── 05 │ ├── 5.c │ └── README.md │ └── 06 │ ├── 6.c │ └── README.md ├── 05 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 10 │ ├── 10.c │ └── README.md │ ├── 11 │ ├── 11.c │ └── README.md │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── 4.c │ └── README.md │ ├── 05 │ ├── 5.c │ └── README.md │ ├── 06 │ ├── 6.c │ └── README.md │ ├── 07 │ ├── 7.c │ └── README.md │ ├── 08 │ ├── 8.c │ └── README.md │ └── 09 │ ├── 9.c │ └── README.md ├── 06 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 12 │ │ └── README.md │ ├── 13 │ │ └── README.md │ ├── 14 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 10 │ ├── 10.c │ └── README.md │ ├── 11 │ ├── 11.c │ └── README.md │ ├── 12 │ ├── 12.c │ └── README.md │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── 4.c │ └── README.md │ ├── 05 │ ├── 5.c │ └── README.md │ ├── 06 │ ├── 6.c │ └── README.md │ ├── 07 │ ├── 7.c │ └── README.md │ ├── 08 │ ├── 8.c │ └── README.md │ └── 09 │ ├── 9.c │ └── README.md ├── 07 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 12 │ │ └── README.md │ ├── 13 │ │ └── README.md │ ├── 14 │ │ └── README.md │ ├── 15 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 10 │ ├── 10.c │ └── README.md │ ├── 11 │ ├── 11.c │ └── README.md │ ├── 12 │ ├── 12.c │ └── README.md │ ├── 13 │ ├── 13.c │ └── README.md │ ├── 14 │ ├── 14.c │ └── README.md │ ├── 15 │ ├── 15.c │ └── README.md │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── 4.c │ └── README.md │ ├── 05 │ ├── 5.c │ └── README.md │ ├── 06 │ ├── 6.c │ └── README.md │ ├── 07 │ ├── 7.c │ └── README.md │ ├── 08 │ ├── 8.c │ └── README.md │ └── 09 │ ├── 9.c │ └── README.md ├── 08 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ ├── .README.md.swp │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 10 │ ├── 10.c │ └── README.md │ ├── 11 │ ├── 11.c │ └── README.md │ ├── 12 │ ├── 12.c │ └── README.md │ ├── 13 │ ├── 13.c │ └── README.md │ ├── 14 │ ├── 14.c │ └── README.md │ ├── 15 │ ├── 15.c │ └── README.md │ ├── 16 │ ├── 16.c │ └── README.md │ ├── 17 │ ├── 17.c │ └── README.md │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── 4.c │ └── README.md │ ├── 05 │ ├── 5.c │ └── README.md │ ├── 06 │ ├── 6.c │ └── README.md │ ├── 07 │ ├── 7.c │ └── README.md │ ├── 08 │ ├── 8.c │ └── README.md │ └── 09 │ ├── 9.c │ └── README.md ├── 09 ├── exercises │ ├── 10 │ │ └── README.md │ ├── 11 │ │ └── README.md │ ├── 12 │ │ └── README.md │ ├── 13 │ │ └── README.md │ ├── 14 │ │ └── README.md │ ├── 15 │ │ └── README.md │ ├── 16 │ │ └── README.md │ ├── 17 │ │ └── README.md │ ├── 18 │ │ └── README.md │ ├── 19 │ │ ├── README.md │ │ └── pb.c │ ├── 01 │ │ └── README.md │ ├── 02 │ │ └── README.md │ ├── 03 │ │ └── README.md │ ├── 04 │ │ └── README.md │ ├── 05 │ │ └── README.md │ ├── 06 │ │ └── README.md │ ├── 07 │ │ └── README.md │ ├── 08 │ │ └── README.md │ └── 09 │ │ └── README.md └── projects │ ├── 01 │ ├── 1.c │ └── README.md │ ├── 02 │ ├── 2.c │ └── README.md │ ├── 03 │ ├── 3.c │ └── README.md │ ├── 04 │ ├── 4.c │ └── README.md │ ├── 05 │ ├── 5.c │ └── README.md │ ├── 06 │ ├── 6.c │ └── README.md │ ├── 07 │ ├── 7.c │ └── README.md │ └── 08 │ ├── 8.c │ └── README.md ├── README.md ├── book-notice.txt └── cover.jpg /02/exercises/01/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | printf("hello, world\n"); 5 | } 6 | -------------------------------------------------------------------------------- /02/exercises/01/1b.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | printf("hello, world\n"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /02/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.03 2 | Condense the `dweight.c` program by (1) replacing the assignments to `height`, 3 | `length`, and `width` with initializers and (2) removing the `weight` variable, 4 | instead calculating `(volume + 165) / 166` within the last `printf`. 5 | 6 | ### Solution 7 | See `dweight.c`. 8 | -------------------------------------------------------------------------------- /02/exercises/03/dweight.c: -------------------------------------------------------------------------------- 1 | /* Computes the dimensional weight of a 12" x 10" x 8" box */ 2 | 3 | #include 4 | 5 | int main(void) { 6 | 7 | int height = 8, 8 | length = 12, 9 | width = 10, 10 | volume = height * length * width; 11 | 12 | printf("Dimensions: %dx%dx%d\n", length, width, height); 13 | printf("Volume (cbic inches): %d\n", volume); 14 | printf("Dimensional weight (pounds): %d\n", (volume + 165) / 166); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /02/exercises/04/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int a, b, c, d, e; 6 | float f, g, h, i, j; 7 | 8 | printf("%d\n%d\n%d\n%d\n%d\n%f\n%f\n%f\n%f\n%f\n", 9 | a, b, c, d, e, f, g, h, i, j); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /02/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.04 2 | Write a program that declares several `int` and `float` variables--without 3 | initializing them--and then prints their values. Is there any pattern to the 4 | values? (Usually there isn't.) 5 | 6 | ### Solution 7 | See `4.c` for the complete program. 8 | 9 | There is no pattern for the first few integers produced, however, upon repeated 10 | compilations and repeated tests, the last two integers and all floats are 11 | usually `0` or `0.000000`, with occasional deviation to "random" garbage values. 12 | -------------------------------------------------------------------------------- /02/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.05 2 | Which of the following are not legal C identifiers? 3 | 4 | (a) `100_bottles` 5 | (b) `_100_bottles` 6 | (c) `one_hundred_bottles` 7 | (d) `bottles_by_the_hundred` 8 | 9 | ### Solution 10 | (a) is not a legal identifier, because identifiers must start with a letter or 11 | underscore. 12 | -------------------------------------------------------------------------------- /02/exercises/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.06 2 | Why is it not a good idea for an identifier to contain more than one adjacent 3 | underscore (as in `current___balance`, for example)? 4 | 5 | ### Solution 6 | An identifier with multiple consecutive underscores are difficult to read. It is 7 | hard to determine exactly how many underscore characters there are in the 8 | identifier, and the use of multiple underscores serves no purpose. 9 | -------------------------------------------------------------------------------- /02/exercises/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.07 2 | Which of the following are keywords in C? 3 | 4 | (a) `for` 5 | (b) `If` 6 | (c) `main` 7 | (d) `printf` 8 | (e) `while` 9 | 10 | ### Solution 11 | (a) and (e) are keywords. (b) would be a keyword if `If` was written as `if`. 12 | -------------------------------------------------------------------------------- /02/exercises/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.08 2 | How many tokens are there in the following statemnt? 3 | ```c 4 | answer=(3*q-p*p)/3; 5 | ``` 6 | 7 | ### Solution 8 | There are thirteen tokens in the statement, six of which are non-unique. 9 | -------------------------------------------------------------------------------- /02/exercises/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.09 2 | Insert spaces betwee nthe tokens in Exercise 8 to make the statement easier to 3 | read. 4 | 5 | ### Solution 6 | ```c 7 | answer = (3 * q - p * p) / 3; 8 | ``` 9 | -------------------------------------------------------------------------------- /02/projects/01/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | printf(" *\n"); 6 | printf(" * \n"); 7 | printf(" * \n"); 8 | printf("* * \n"); 9 | printf(" * * \n"); 10 | printf(" * \n"); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /02/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 2.01 2 | Write a program that uses `prinf` to display the following picture on the 3 | screen: 4 | ``` 5 | * 6 | * 7 | * 8 | * * 9 | * * 10 | * 11 | ``` 12 | ### Solution 13 | See `1.c` 14 | -------------------------------------------------------------------------------- /02/projects/02/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | printf("Sphere volume: %.2f cubic meters\n", 4.0f/3.0f * 3.14f * 1000); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /02/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 1.02 2 | Write a program that computes the volume of a sphere with a 10-meter radius, 3 | using the formula v = 4/3πr³. Write the fraction 4/3 as `4.0f/3.0f`. (Try 4 | writing it as `4/3`. What happens?) Hint: C doesn't have an exponentiation 5 | operator, so you'll need to multiply r by itself twice to compute r³. 6 | 7 | ### Solution 8 | See `2.c`. 9 | 10 | Writing the fraction in integer form will result in a integer rounding error, 11 | returning `1`. 12 | -------------------------------------------------------------------------------- /02/projects/03/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int r = 0; 6 | 7 | printf("Enter radius: "); 8 | scanf("%d", &r); 9 | 10 | printf("\nSphere volume: %.2f cubic meters\n", 11 | 4.0f/3.0f * 3.14f * r * r * r); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /02/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 2.03 2 | Modify the program of Programming Project 2 so that it prompts the user to eter 3 | the radius of the sphere. 4 | 5 | ### Solution 6 | See `3.c`. 7 | -------------------------------------------------------------------------------- /02/projects/04/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | float money = 0.0f; 6 | printf("Enter an amount: "); 7 | scanf("%f", &money); 8 | printf("With tax added: $%.2f\n", money * 1.05f); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /02/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 2.04 2 | Write a program that asks the user to enter a dollars-and-cents amount, then 3 | displays the amount with a 5% tax added: 4 | ``` 5 | Enter an amount: 100.00 6 | With tax added: $105.00 7 | ``` 8 | 9 | ### Solution 10 | See `4.c`. 11 | -------------------------------------------------------------------------------- /02/projects/05/5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int x = 0; 6 | 7 | printf("Enter value for x: "); 8 | scanf("%d", &x); 9 | printf("Result: %d\n", 10 | (3 * x * x * x * x * x) + (2 * x * x * x * x) - (5 * x * x * x) 11 | - (x * x) + (7 * x) - 6); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /02/projects/05/README.md: -------------------------------------------------------------------------------- 1 | ### Project 2.05 2 | Write a program that asks the user to enter a value for x and then displays the 3 | value of the following polynomial: 4 | 5 | 3x5 + 2x4 - 5x3 - x2 + 7x - 6 6 | 7 | *Hint*: C doesn't have an exponentiation operator, so you'll need to multiply x 8 | by itself repeatedly in order to compute the powers of x. (For example, `x * x * 9 | x` is `x` cubed.) 10 | 11 | ### Solution 12 | See `5.c`. 13 | -------------------------------------------------------------------------------- /02/projects/06/6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int x = 0; 6 | 7 | printf("Enter value for x: "); 8 | scanf("%d", &x); 9 | printf("Result: %d\n", 10 | ((((3 * x + 2) * x - 5) * x - 1) * x + 7) * x - 6); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /02/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 2.06 2 | Modify the program of Programming Project 5 so that the polynomial is evaluated 3 | using the following formula: 4 | 5 | ((((3x + 2)x-5)x-1)x+7)x-6 6 | 7 | Note that the modified program performs fewer multiplications. This technique 8 | for evaluating polynomials is knows as ***Horner's Rule***. 9 | 10 | ### Solution 11 | See `6.c`. 12 | -------------------------------------------------------------------------------- /02/projects/07/7.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int money = 0; 6 | 7 | printf("Enter a dollar amount: "); 8 | scanf("%d", &money); 9 | 10 | printf("$20 bills: %d\n", money/20); 11 | money -= 20 * (money/20); 12 | 13 | printf("$10 bills: %d\n", money/10); 14 | money -= 10 * (money/10); 15 | 16 | printf(" $5 bills: %d\n", money/5); 17 | money -= 5 * (money/5); 18 | 19 | printf(" $1 bills: %d\n", money); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /03/exercises/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.01 2 | What output do the following calls of `printf` produce? 3 | 4 | (a) `printf("%6d,%4d", 86, 1040);` 5 | (b) `printf("%12.5e", 30.253);` 6 | (c) `printf("%.4f", 83.162);` 7 | (d) `printf("%-6.2g", .0000009979);` 8 | 9 | ### Solution 10 | (a) `86,1040` 11 | (b) `3.02530e+01` 12 | (c) `83.1620` 13 | (d) `1e-06` 14 | -------------------------------------------------------------------------------- /03/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.04 2 | Suppose that we call `scanf` as follows: 3 | 4 | ```c 5 | scanf("%d%f%d", &i, &x, &j); 6 | ``` 7 | 8 | If the user enters 9 | 10 | ``` 11 | 10.3 5 6 12 | ``` 13 | 14 | what will be the values of `i`, `x` and `j` after the call? (Assume that `i` and 15 | `j` are `int` variables and `x` is a `float` variable.) 16 | 17 | ### Solution 18 | ``` 19 | i = 10 20 | x = 0.3 21 | j = 5 22 | ``` 23 | 24 | The decimal point will end the input for `i` and begin `x`, and the next input, 25 | 5, will be assigned to `j`. 26 | -------------------------------------------------------------------------------- /03/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.05 2 | Suppose that we call `scanf` as follows: 3 | 4 | ```c 5 | scanf("%f%d%f", &x, &i, &y); 6 | ``` 7 | 8 | If the user enters 9 | 10 | ``` 11 | 12.3 45.6 789 12 | ``` 13 | 14 | what will be the values of `x`, `i` and `y` after the call? (Assume that `x` and 15 | `y` are `float` variables and `i` is an `int` variable.) 16 | 17 | ### Solution 18 | ``` 19 | x = 12.3 20 | i = 45 21 | y = 0.6 22 | ``` 23 | 24 | `scanf` will read the decimal in `45.6` as the beginning of `y`, enter `0.6`, 25 | and will not enter `789` until the next scan. 26 | -------------------------------------------------------------------------------- /03/exercises/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.06 2 | Show how to modify the `addfrac.c` program of Section 3.2 so that the user is 3 | allowed to enter fractions that contain spaces before and after each `/` 4 | character. 5 | 6 | ### Solution 7 | Add a space after the first `%d` for each `scanf` statement. This will allow 8 | pattern matching for whitespace between the two `%d`s and the `/` character. 9 | 10 | See `addfrac.c`. 11 | -------------------------------------------------------------------------------- /03/exercises/06/addfrac.c: -------------------------------------------------------------------------------- 1 | /* Adds two fractions */ 2 | 3 | #include 4 | 5 | int main(void) { 6 | int num1, denom1, num2, denom2, result_num, result_denom; 7 | 8 | printf("Enter first fraction: "); 9 | scanf("%d /%d", &num1, &denom1); 10 | 11 | printf("Enter second fraction: "); 12 | scanf("%d /%d", &num2, &denom2); 13 | 14 | result_num = num1 * denom2 + num2 * denom1; 15 | result_denom = denom1 * denom2; 16 | printf("The sum is %d/%d\n", result_num, result_denom); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /03/projects/01/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int year, month, day; 6 | 7 | printf("Enter a date (mm/dd/yyyy): "); 8 | scanf("%d /%d /%d", &month, &day, &year); 9 | 10 | printf("You entered the date %.4d%.2d%.2d\n", year, month, day); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /03/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 3.01 2 | Write a program that accepts a date from the user in the form *mm/dd/yyy* and 3 | then displays it in the form *yyymmdd*: 4 | 5 | ``` 6 | Enter a date (mm/dd/yyyy): 2/17/2011 7 | You entered the date 20110217 8 | ``` 9 | 10 | ### Solution 11 | See `1.c` 12 | -------------------------------------------------------------------------------- /03/projects/02/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int item, year, month, day; 6 | float price; 7 | 8 | printf("Enter item number: "); 9 | scanf("%d", &item); 10 | 11 | printf("Enter unit price: "); 12 | scanf("%f", &price); 13 | 14 | printf("Enter purchase date (mm/dd/yyyy): "); 15 | scanf("%d /%d /%d", &month, &day, &year); 16 | 17 | printf("Item\t\tUnit\t\tPurchase\n\t\tPrice\t\tDate\n"); 18 | printf("%d\t\t$%8.2f\t%.2d/%.2d/%.4d\n", item, price, month, day, year); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /03/projects/03/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int gsi, group_id, pub_code, item_num, check_digit; 6 | 7 | printf("Enter ISBN: "); 8 | scanf("%d -%d -%d -%d -%d", 9 | &gsi, &group_id, &pub_code, &item_num, &check_digit); 10 | printf("GSI prefix: %d\nGroup identifier: %d\n", gsi, group_id); 11 | printf("Publisher code: %d\nItem number: %d\nCheck digit: %d\n", 12 | pub_code, item_num, check_digit); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /03/projects/04/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int beg, mid, end; 5 | 6 | printf("Enter phone number [(xxx) xxx-xxxx]: "); 7 | scanf(" (%d ) %d- %d", &beg, &mid, &end); 8 | printf("You entered %.3d.%3d.%.4d\n", beg, mid, end); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /03/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 3.04 2 | Write a program that prompts the user to enter a telephone number in the form 3 | (xxx) xxx-xxxx and then displays the number in the form xxx.xxx.xxxx: 4 | 5 | ``` 6 | Enter phone number [(xxx) xxx-xxxx]: (404) 817-6900 7 | You entered 404.817.6900 8 | ``` 9 | 10 | ### Solution 11 | See `4.c`. 12 | -------------------------------------------------------------------------------- /03/projects/06/6.c: -------------------------------------------------------------------------------- 1 | /* Adds two fractions */ 2 | 3 | #include 4 | 5 | int main(void) { 6 | int num1, denom1, num2, denom2, result_num, result_denom; 7 | 8 | printf("Enter two fractions separated by a plus sign: "); 9 | scanf("%d /%d +%d /%d", &num1, &denom1, &num2, &denom2); 10 | 11 | result_num = num1 * denom2 + num2 * denom1; 12 | result_denom = denom1 * denom2; 13 | printf("The sum is %d/%d\n", result_num, result_denom); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /03/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 3.06 2 | Modify the `addfrac.c` program of Section 3.2 so that the user enters both 3 | fractions at the same time, separated by a plus sign: 4 | 5 | ``` 6 | Enter two fractions separated by a plus sign: 5/6+3/4 7 | The sum is 38/24 8 | ``` 9 | 10 | ### Solution 11 | See `6.c`. 12 | -------------------------------------------------------------------------------- /04/exercises/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.01 2 | Show the output produced by each of the following program fragments. Assume that 3 | `i`, `j`, and `k` are `int` variables. 4 | 5 | ``` 6 | (a) i = 5; j = 3; 7 | printf("%d %d", i / j, i % j); 8 | (b) i = 2; j = 3; 9 | printf("%d", (i + 10) % j); 10 | (c) i = 7; j = 8; k = 9; 11 | printf("%d", (i + 10) % k / j); 12 | (d) i = 1; j = 2; k = 3; 13 | printf("%d", (i + 5) % (j + 2) / k); 14 | ``` 15 | 16 | ### Solution 17 | 18 | (a) 1 2 19 | (b) 0 20 | (c) 1 21 | (d) 0 22 | -------------------------------------------------------------------------------- /04/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.02 2 | If `i` and `j` are positive integers, does `(-i)/j` always have the same value 3 | as `-(i/j)`? Justify your answer. 4 | 5 | ### Solution 6 | No. The C89 and C99 standards implement division of negative numbers 7 | differently: `(-9)/7` can produce -1 or -2 in C89, while `-(9/7)` will always 8 | produce -1. C99 will always truncate the remainder towards zero, however, so the 9 | answers produced by `(-i)/j` and `-(i/j)` will be equivalent. 10 | -------------------------------------------------------------------------------- /04/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.03 2 | What is the value of each of the following expressions in C89? (Give all 3 | possible values if an expression may have more than one value.) 4 | 5 | (a) 8 / 5 6 | (b) -8 / 5 7 | (c) 8 / -5 8 | (d) -8 / -5 9 | 10 | ### Solution 11 | 12 | (a) 1 13 | (b) -1 or -2 14 | (c) -1 or -2 15 | (d) 1 or 2 16 | -------------------------------------------------------------------------------- /04/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.04 2 | Repeat Exercise 3 for C99. 3 | 4 | ### Solution 5 | 6 | (a) 1 7 | (b) -1 8 | (c) -1 9 | (d) 1 10 | -------------------------------------------------------------------------------- /04/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.05 2 | What is the value of each of the following expressions in C89? (Give all 3 | possible values if an expression may have more than one value.) 4 | 5 | (a) 8 % 5 6 | (b) -8 % 5 7 | (c) 8 % -5 8 | (d) -8 % -5 9 | 10 | ### Solution 11 | 12 | (a) 3 13 | (b) -3 or 5 14 | (c) 3 or -5 15 | (d) 3 or 5 16 | -------------------------------------------------------------------------------- /04/exercises/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.06 2 | Repeat Exercise 5 for C99. 3 | 4 | ### Solution 5 | 6 | (a) 1 7 | (b) -3 8 | (c) 3 9 | (d) -3 10 | -------------------------------------------------------------------------------- /04/exercises/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.08 2 | Would the `upc.c` program still work if the expression `9 - ((total - 1) % 10)` 3 | were replaced by `(10 - (total % 10)) % 10`? 4 | 5 | ### Solution 6 | Yes, both expressions would produce equal answers. 7 | -------------------------------------------------------------------------------- /04/exercises/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.09 2 | Show the output produced by the following program fragments. Assume that `i`, 3 | `j`, and `k` are `int` variables. 4 | 5 | ``` 6 | (a) i = 7; j = 8; 7 | i *= j + 1; 8 | printf("%d %d", i, j); 9 | (b) i = j = k = 1; 10 | i += j += k; 11 | printf("%d %d %d", i, j, k); 12 | (c) i = 1; j = 2; k = 3; 13 | i -= j -= k; 14 | printf("%d %d %d", i, j, k); 15 | (d) i = 2; j = 1; k = 0; 16 | i *= j *= k; 17 | printf("%d %d %d", i, j, k); 18 | ``` 19 | 20 | ### Solution 21 | 22 | (a) 63 8 23 | (b) 3 2 1 24 | (c) 2 -1 3 25 | (d) 0 0 0 26 | -------------------------------------------------------------------------------- /04/exercises/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.10 2 | Show the output produced by each of the following program fragments. Assume that 3 | `i` and `j` are `int` variables. 4 | 5 | ``` 6 | (a) i = 6; 7 | j = i += i; 8 | printf("%d %d", i, j); 9 | (b) i = 5; 10 | j = (i -= 2) + 1; 11 | printf("%d %d", i, j); 12 | (c) i = 7; 13 | j = 6 + (i = 2.5); 14 | printf("%d %d", i, j); 15 | (d) i = 2; j = 8; 16 | j = (i = 6) + (j = 3); 17 | printf("%d %d", i, j); 18 | ``` 19 | 20 | ### Solution 21 | 22 | (a) 12 12 23 | (b) 3 4 24 | (c) 2 9 25 | (d) 6 9 26 | -------------------------------------------------------------------------------- /04/exercises/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.12 2 | Show the output produced by each of the following program fragments. Assume that 3 | `i` and `j` are `int` variables. 4 | 5 | ``` 6 | (a) i = 5; 7 | j = ++i * 3 - 2; 8 | printf("%d %d", i, j); 9 | (b) i = 5; 10 | j = 3 - 2 * i++; 11 | printf("%d %d", i, j); 12 | (c) i = 7; 13 | j = 3 * i-- + 2; 14 | printf("%d %d", i, j); 15 | (d) i = 7; 16 | j = 3 + --i * 2; 17 | printf("%d %d", i, j); 18 | ``` 19 | 20 | ### Solution 21 | 22 | (a) 6 16 23 | (b) 6 -7 24 | (c) 6 23 25 | (d) 6 15 26 | -------------------------------------------------------------------------------- /04/exercises/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.13 2 | Only one of the expressions `++i` and `i++` is exactly the same as `(i += 1)`; 3 | which is it? Justify your answer. 4 | 5 | ### Solution 6 | The prefix increment operator `++i` is equivalent to `(i += 1)` because the 7 | value is immediately read as `i + 1`, unlike the postfix operator, which applies 8 | the assignment but does not read the value immediately as `i + 1` but as `i`. 9 | -------------------------------------------------------------------------------- /04/exercises/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.14 2 | Supply parentheses to show how a C compiler would interpret each of the 3 | following expressions. 4 | 5 | ``` 6 | (a) a * b - c * d + e 7 | (b) a / b % c / d 8 | (c) - a - b + c - + d 9 | (d) a * - b / c - d 10 | ``` 11 | 12 | ### Solution 13 | 14 | ``` 15 | (a) (((a * b) - (c * d)) + e) 16 | (b) (((a / b) % c) / d) 17 | (c) ((((- a) - b) + c) - (+ d)) 18 | (d) (((a * (- b)) / c) - d) 19 | ``` 20 | -------------------------------------------------------------------------------- /04/exercises/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.15 2 | Give the values of `i` and `j` after each of the following expression statements 3 | has been executed. (Assume that `i` has the value 1 initially and `j` has the 4 | value 2.) 5 | 6 | (a) `i += j;` 7 | (b) `i--;` 8 | (c) `i * j / i;` 9 | (d) `i % ++j;` 10 | 11 | ### Solution 12 | 13 | (a) 3, 2 14 | (b) 0, 2 15 | (c) 1, 2 16 | (d) 1, 3 17 | -------------------------------------------------------------------------------- /04/projects/01/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int n = 0; 6 | 7 | printf("Enter a two-digit number: "); 8 | scanf("%d", &n); 9 | 10 | printf("The reversal is: %d%d\n", n % 10, n / 10); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /04/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 4.01 2 | Write a program that asks the user to enter a two-digit number, then prints the 3 | number with its digits reversed. A session with the program should have the 4 | following appearance: 5 | 6 | ``` 7 | Enter a two-digit number: 28 8 | The reversal is: 82 9 | ``` 10 | 11 | Read the number using `%d`, then break it into two digits. *Hint*: If `n` is an 12 | integer, then `n % 10` is the last digit in `n` and `n / 10` is `n` with the 13 | last digit removed. 14 | 15 | ### Solution 16 | See `1.c`. 17 | -------------------------------------------------------------------------------- /04/projects/02/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int n = 0; 6 | 7 | printf("Enter a three-digit number: "); 8 | scanf("%d", &n); 9 | 10 | printf("The reversal is: %d%d%d\n", n % 10, n / 10 % 10, n / 100); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /04/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 4.02 2 | Extend the program in Programming Project 1 to handle *three*-digit numbers. 3 | 4 | ### Solution 5 | See `2.c`. 6 | -------------------------------------------------------------------------------- /04/projects/03/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int n1, n2, n3; 6 | 7 | printf("Enter a three-digit number: "); 8 | scanf("%1d%1d%1d", &n1, &n2, &n3); 9 | 10 | printf("The reversal is: %d%d%d\n", n3, n2, n1); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /04/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 4.03 2 | Rewrite the program in Programming Project 2 so that it prints the reversal of a 3 | three-digit number without using arithmeic to split the number into digits. 4 | *Hint*: See the `upc.c` program of Section 4.1. 5 | 6 | ### Solution 7 | See `3.c`. 8 | -------------------------------------------------------------------------------- /04/projects/04/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | 7 | printf("Enter a number between 0 and 32767: "); 8 | scanf("%d", &n); 9 | 10 | printf("In octal, your number is: %d%d%d%d%d\n", 11 | (n/4096)%8, 12 | (n/512)%8, 13 | (n/64)%8, 14 | (n/8)%8, 15 | n%8); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /04/projects/05/README.md: -------------------------------------------------------------------------------- 1 | ### Project 4.05 2 | Rewrite the `upc.c` program of Section 4.1 so that the user enters 11 digits at 3 | one time, instead of entering one digit, then five digits, and then another five 4 | digits. 5 | 6 | ``` 7 | Enter the first 11 digits of a UPC: 01380015173 8 | Check digit: 5 9 | ``` 10 | 11 | ### Solution 12 | See `5.c`. 13 | -------------------------------------------------------------------------------- /04/projects/06/6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, sum1, sum2, total; 6 | 7 | printf("Enter the first 12 digits of an EAN: "); 8 | scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", 9 | &n1, &n2, &n3, &n4, &n5, &n6, &n7, &n8, &n9, &n10, &n11, &n12); 10 | 11 | sum1 = n2 + n4 + n6 + n8 + n10 + n12; 12 | sum2 = n1 + n3 + n5 + n7 + n9 + n11; 13 | total = 3 * sum1 + sum2; 14 | 15 | printf("Check digit: %d\n", 9 - ((total - 1) % 10)); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /05/exercises/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.01 2 | The following program fragments illustrate the relational and equality 3 | operators. Show the output produced by each, assuming that `i`, `j`, and `k` are 4 | `int` variables. 5 | 6 | ``` 7 | (a) i = 2; j = 3; 8 | k = i * j == 6; 9 | printf("%d", k); 10 | (b) i = 5; j = 10; k = 1; 11 | printf("%d", k > i < j); 12 | (c) i = 3; j = 2; k = 1; 13 | printf("%d", i < j == j < k); 14 | (d) i = 3; j = 4; k = 5; 15 | printf("%d", i % j + i < k); 16 | ``` 17 | 18 | ### Solution 19 | 20 | (a) 1 21 | (b) 1 22 | (c) 1 23 | (d) 0 24 | -------------------------------------------------------------------------------- /05/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.02 2 | The following program fragments illustrate the logical operators. Show the 3 | output produced by each, assuming that `i`, `j`, and `k` are `int` variables. 4 | 5 | ``` 6 | (a) i = 10; j = 5; 7 | printf("%d", !i < j); 8 | (b) i = 2; j = 1; 9 | printf("%d", !!i + !j); 10 | (c) i = 5; j = 0; k = -5; 11 | printf("%d", i && j || k); 12 | (d) i = 1; j = 2; k = 3; 13 | printf("%d", i < j || k); 14 | ``` 15 | 16 | ### Solution 17 | 18 | (a) 1 19 | (b) 1 20 | (c) 1 21 | (d) 1 22 | -------------------------------------------------------------------------------- /05/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.04 2 | Write a single expression whose value is either -1, 0 or +1, depending on 3 | whether `i` is less than, equal to, or greater than `j`, respectively. 4 | 5 | ### Solution 6 | ```c 7 | (i > j) - (i < j); 8 | ``` 9 | -------------------------------------------------------------------------------- /05/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.05 2 | Is the following `if` statement legal? 3 | 4 | ```c 5 | if (n >= 1 <= 10) 6 | printf("n is between 1 and 10\n"); 7 | ``` 8 | 9 | If so, what does it do when `n` is equal to 0? 10 | 11 | ### Solution 12 | The statement is legal, but confusing. When `n` equals 0, the `printf` statement 13 | will still be run. This is because the left-associativity of the equality test 14 | operators: `n >= 1` returns 0, and `0 <= 10` returns 1 (a true value). 15 | -------------------------------------------------------------------------------- /05/exercises/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.06 2 | Is the following `if` statement legal? 3 | 4 | ```c 5 | if (n == 1-10) 6 | printf("n is between 1 and 10\n"); 7 | ``` 8 | 9 | If so, what does it do when `n` is equal to 5? 10 | 11 | ### Solution 12 | The statement is legal, but confusing. When `n` is equal to anything besides -9, 13 | the expression returns 0 (a false value), and the `printf` statement is never 14 | run. This is because the `1-10` expression is run first, yielding -9. 15 | -------------------------------------------------------------------------------- /05/exercises/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.07 2 | What does the following statement print if `i` has the value 17? What does it 3 | print if `i` has the value -17? 4 | 5 | ```c 6 | printf("%d\n", i >= 0 ? i : -i); 7 | ``` 8 | 9 | ### Solution 10 | The statement will print 17 in both cases. 11 | -------------------------------------------------------------------------------- /05/exercises/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.08 2 | The following `if` statement is unnecessarily complicated. Simplify it as much 3 | as possible. (*Hint*: The entire statement can be replaced by a single 4 | assignment.) 5 | 6 | ```c 7 | if (age >= 13) 8 | if (age <= 19) 9 | teenager = true; 10 | else 11 | teenager = false; 12 | else if (age < 13) 13 | teenager = false; 14 | ``` 15 | 16 | ### Solution 17 | 18 | ```c 19 | teenager = age >= 13 && age <= 19; 20 | ``` 21 | -------------------------------------------------------------------------------- /05/exercises/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.10 2 | What output does the following program fragment produce? (Assume that `i` is an 3 | integer variable.) 4 | 5 | ```c 6 | i = 1; 7 | switch (i % 3) { 8 | case 0: printf("zero"); 9 | case 1: printf("one"); 10 | case 2: printf("two"); 11 | } 12 | ``` 13 | 14 | ### Solution 15 | The program will printf `zeroonetwo` if `i % 3` is 0, `onetwo` if `i % 3` is 1, 16 | and `two` if `i % 3` is 2, because of the missing `break` statements. 17 | -------------------------------------------------------------------------------- /05/projects/01/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int n, d; 6 | 7 | printf("Enter a number: "); 8 | scanf("%d", &n); 9 | 10 | if (n >= 0 && n <= 9) 11 | d = 1; 12 | else if (n >= 10 && n <= 99) 13 | d = 2; 14 | else if (n >= 100 && n <= 999) 15 | d = 3; 16 | else if (n >= 1000 && n <= 9999) 17 | d = 4; 18 | 19 | printf("The number %d has %d digits\n", n, d); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /05/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 5.01 2 | Write a program that calculates how many digits a number contains: 3 | 4 | ``` 5 | Enter a number: 374 6 | The number 374 has 3 digits 7 | ``` 8 | 9 | You many assume that the number has no more than four digits. *Hint*: Use `if` 10 | statements to test the number. For example, if the number is between 0 and 9, it 11 | has one digit. If the number is between 10 and 99, it has two digits. 12 | 13 | ### Solution 14 | See `1.c`. 15 | -------------------------------------------------------------------------------- /05/projects/02/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int hour, minute; 6 | 7 | printf("Enter a 24-hour time: "); 8 | scanf("%d :%d", &hour, &minute); 9 | 10 | if (hour < 11) 11 | printf("Equivalent 12-hour time: %d:%d AM\n", 12 | hour == 0 ? 12 : hour, minute); 13 | else 14 | printf("Equivalent 12-hour time: %d:%d PM\n", 15 | hour == 12 ? 12 : hour - 12, minute); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /05/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 5.02 2 | Write a program that asks the user for a 24-hour time, then displays the time in 3 | 12-hour form: 4 | 5 | ``` 6 | Enter a 24-hour time: 21:11 7 | Equivalent 12-hour time: 9:11 PM 8 | ``` 9 | 10 | Be careful not to display 12:00 as 0:00. 11 | 12 | ### Solution 13 | See `2.c`. 14 | -------------------------------------------------------------------------------- /05/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 5.03 2 | Modify the `broker.c` program of Section 5.2 by making both of the following 3 | changes: 4 | 5 | (a) Ask the user to enter the number of shares and the price per share, instead 6 | of the value of the trade. 7 | (b) Add statements that compute the commission charged by a rival broker ($33 8 | plus 3 cents per share fewer than 2000 shares; $33 plus 2 cents per share for 9 | 2000 shares or more). Display the rival's commission as well as the commission 10 | charged by the original broker. 11 | 12 | ### Solution 13 | See `3.c`. 14 | -------------------------------------------------------------------------------- /05/projects/04/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int speed; 6 | 7 | printf("Enter a wind speed in knots: "); 8 | scanf("%d", &speed); 9 | printf("Wind description: "); 10 | 11 | if (speed < 1) 12 | printf("Calm\n"); 13 | else if (speed <= 3) 14 | printf("Light air\n"); 15 | else if (speed <= 27) 16 | printf("Breeze\n"); 17 | else if (speed <= 47) 18 | printf("Gale\n"); 19 | else if (speed <= 63) 20 | printf("Storm\n"); 21 | else 22 | printf("Hurricane\n"); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /05/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 5.04 2 | Here's a simplified version of the Beaufort scale, which is used to estimate 3 | wind force: 4 | 5 | | Speed (knots) | Description 6 | | --- | --- | 7 | | Less than 1 | Calm | 8 | | 1-3 | Light air | 9 | | 4-27 | Breeze | 10 | | 28-47 | Gale | 11 | | 48-63 | Storm | 12 | | Above 63 | Hurricane | 13 | 14 | Write a program that asks the user to enter a wind speed (in knots), then 15 | displays the corresponding description. 16 | 17 | ### Solution 18 | See `4.c`. 19 | -------------------------------------------------------------------------------- /05/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 5.06 2 | Modify the `upc.c` program of Section 4.1 so that it checks whether a UPC is 3 | valid. After the users enters a UPC, the program will display either `VALID` or 4 | `NOT VALID`. 5 | 6 | ### Solution 7 | See `6.c`. 8 | -------------------------------------------------------------------------------- /05/projects/07/README.md: -------------------------------------------------------------------------------- 1 | ### Project 5.07 2 | Write a program that finds the largest and smallest of four integers entered by 3 | the user: 4 | 5 | ``` 6 | Enter four integers: 21 42 10 35 7 | Largest: 43 8 | Smallest: 10 9 | ``` 10 | 11 | Use as few `if` statements as possible: *Hint*: Four `if` statements are 12 | sufficient. 13 | 14 | ### Solution 15 | See `7.c`. 16 | -------------------------------------------------------------------------------- /05/projects/09/README.md: -------------------------------------------------------------------------------- 1 | ### Project 5.09 2 | Write a program that prompts the user to enter two dates and then indicates 3 | which date comes earlier on the calandar: 4 | 5 | ``` 6 | Enter first date (mm/dd/yyyy): 3/6/08 7 | Enter second date (mm/dd/yyyy): 5/17/07 8 | 5/17/07 is earlier than 3/6/08 9 | ``` 10 | 11 | ### Solution 12 | See `9.c`. 13 | -------------------------------------------------------------------------------- /05/projects/10/README.md: -------------------------------------------------------------------------------- 1 | ### Project 5.10 2 | Using the `switch` statement, write a program that converts a numerical grade 3 | into a letter grade: 4 | 5 | ``` 6 | Enter numberical grade: 84 7 | Letter grade: B 8 | ``` 9 | 10 | Use the following grading scale: A = 90-100, B = 80-89, C = 70-79, D = 60-69, 11 | F = 0-59. Print an error message if the grade is larger than 100 or less than 0. 12 | *Hint*: Break the grade into two digits, then use a `switch` statement to test 13 | the ten's digit. 14 | 15 | ### Solution 16 | See `10.c`. 17 | -------------------------------------------------------------------------------- /05/projects/11/README.md: -------------------------------------------------------------------------------- 1 | ### Project 5.11 2 | Write a program that asks the user for a two-digit number, then prints the 3 | English word for the number: 4 | 5 | ``` 6 | Enter a two-digit number: 45 7 | You entered the number forty-five. 8 | ``` 9 | 10 | *Hint*: Break the number into two digits. Use one `switch` statement to print 11 | the word for the first digit ("twenty," "thirty," and so forth). Use a second 12 | `switch` statement to print the word for the second digit. Don't forget that the 13 | numbers between 11 and 19 require special treatment. 14 | 15 | ### Solution 16 | See `11.c`. 17 | -------------------------------------------------------------------------------- /06/exercises/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.01 2 | What output does the following program fragment produce? 3 | 4 | ```c 5 | i = 1; 6 | while (i <= 128) { 7 | printf("%d ", i); 8 | i *= 2; 9 | } 10 | ``` 11 | 12 | ### Solution 13 | 14 | `1 2 4 8 16 32 64 128 ` 15 | -------------------------------------------------------------------------------- /06/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.02 2 | What output does the following program fragment produce? 3 | 4 | ```c 5 | i = 9384; 6 | do { 7 | printf("%d ", i); 8 | i /= 10; 9 | } while (i > 0); 10 | ``` 11 | 12 | ### Solution 13 | 14 | `9384 938 93 9 ` 15 | -------------------------------------------------------------------------------- /06/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.03 2 | What output does the following `for` statement produce? 3 | 4 | ```c 5 | for (i = 5, j = i - 1; i > 0, j > 0; --i, j = i - 1) 6 | printf("%d ", i); 7 | ``` 8 | 9 | ### Solution 10 | 11 | `5 4 3 2 ` 12 | 13 | The program does not print the expected `1 ` because of the misuse of the comma 14 | operator separating the conditional expressions in the for loop: the first 15 | expression `i > 0` is always discarded. 16 | -------------------------------------------------------------------------------- /06/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.04 2 | Which one of the following statements is not equivalent to the other two 3 | (assuming that the loop bodies are the same)? 4 | 5 | (a) `for (i = 0; i < 10; i++)` ... 6 | (b) `for (i = 0; i < 10; ++i)` ... 7 | (c) `for (i = 0; i++ < 10; )` ... 8 | 9 | ### Solution 10 | The third statement (c) is not equivalent to the other two, because the postfix 11 | increment operator will only be applied after the conditional expression is 12 | evaluated, unlike the other two in which the two expressions are separate. 13 | -------------------------------------------------------------------------------- /06/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.05 2 | Which one of the following statements is not equivalent to the other two 3 | (assuming that the loop bodies are the same)? 4 | 5 | (a) `while (i < 10) {`...`}` 6 | (b) `for (; i < 10;) {`...`}` 7 | (c) `do {`...`} while (i < 10);` 8 | 9 | ### Solution 10 | The third statement (c) is not equivalent to the other two, because the `do` 11 | statement will always run at least once, then check if the condition in the 12 | `while` statment is true, unlike the other two statements, which always check 13 | the condition first. 14 | -------------------------------------------------------------------------------- /06/exercises/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.06 2 | Translate the program fragment of Exercise 1 into a single `for` statement. 3 | 4 | ### Solution 5 | 6 | ```c 7 | for (i = 1; i <= 128; i *= 2) 8 | printf("%d ", i); 9 | ``` 10 | -------------------------------------------------------------------------------- /06/exercises/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.07 2 | Translate the program fragment of Exercise 2 into a single `for` statement. 3 | 4 | ### Solution 5 | 6 | ```c 7 | for (i = 9384; i > 0; i /= 10) 8 | printf("%d ", i); 9 | ``` 10 | -------------------------------------------------------------------------------- /06/exercises/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.08 2 | What output does the following `for` statement produce? 3 | 4 | ```c 5 | for (i = 10; i >= 1; i /= 2) 6 | printf("%d ", i++); 7 | ``` 8 | 9 | ### Solution 10 | 11 | The program will print `10 5 3 2 ` then print an endless stream of `1 `s. The 12 | postfix increment operator in the print statement will always cause the 13 | conditional in the `for` loop to return true. 14 | -------------------------------------------------------------------------------- /06/exercises/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.09 2 | Translate the `for` statement of Exercise 8 into an equivalent `while` 3 | statement. You will need one statement in addition to the `while` loop itself. 4 | 5 | ### Solution 6 | 7 | ```c 8 | i = 10; 9 | while (i >= 1) { 10 | printf("%d ", i++); 11 | i /= 2; 12 | } 13 | ``` 14 | -------------------------------------------------------------------------------- /06/exercises/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.10 2 | Show how to replace a `continue` statement by an equivalent `goto` statement. 3 | 4 | ### Solution 5 | Label the end of the loop and `goto` that label: 6 | 7 | ```c 8 | for (i = 0; i <= 10; i++) { 9 | if (i % 2 == 1) 10 | continue; 11 | printf("%d ", i); 12 | } 13 | ``` 14 | 15 | is the same as 16 | 17 | ```c 18 | for (i = 0; i <= 10; i++) { 19 | if (i % 2 == 1) 20 | goto end; 21 | printf("%d ", i); 22 | end: 23 | } 24 | ``` 25 | -------------------------------------------------------------------------------- /06/exercises/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.11 2 | What output does the following program fragment produce? 3 | 4 | ```c 5 | sum = 0; 6 | for (i = 0; i < 10; i++) { 7 | if (i % 2) 8 | continue; 9 | sum += i; 10 | } 11 | printf("%d\n", sum); 12 | ``` 13 | 14 | ### Solution 15 | 16 | `20` 17 | -------------------------------------------------------------------------------- /06/exercises/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.13 2 | Rewrite the following loop so that its body is empty: 3 | 4 | ```c 5 | for (n = 0; m > 0; n++) 6 | m /= 2; 7 | ``` 8 | 9 | ### Solution 10 | 11 | ```c 12 | for (n = 0; m > 0; n++, m /= 2) 13 | ; 14 | ``` 15 | -------------------------------------------------------------------------------- /06/exercises/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.14 2 | Find the error in the following program fragment and fix it. 3 | 4 | ```c 5 | if (n % 2 == 0); 6 | printf("n is even\n"); 7 | ``` 8 | 9 | ### Solution 10 | 11 | ```c 12 | if (n % 2 == 0) /* removed semicolon */ 13 | printf("n is even\n"); 14 | ``` 15 | -------------------------------------------------------------------------------- /06/projects/01/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | float largest = 0.0f; 6 | float current; 7 | 8 | do { 9 | printf("Enter a number: "); 10 | scanf("%f", ¤t); 11 | 12 | if (current > largest) 13 | largest = current; 14 | } while (current > 0); 15 | 16 | printf("\nThe largest number entered was %f\n", largest); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /06/projects/02/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int m, n, r; 6 | 7 | printf("Enter two integers: "); 8 | scanf("%d%d", &m, &n); 9 | 10 | while (n != 0) { 11 | r = m % n; 12 | m = n; 13 | n = r; 14 | } 15 | 16 | printf("Greatest common divisor: %d\n", m); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /06/projects/03/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int num, denom, n, m, r; 6 | 7 | printf("Enter a fraction: "); 8 | scanf("%d /%d", &num, &denom); 9 | 10 | m = num; 11 | n = denom; 12 | 13 | while (n != 0) { 14 | r = m % n; 15 | m = n; 16 | n = r; 17 | } 18 | 19 | printf("In lowest terms: %d/%d\n", num / m, denom / m); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /06/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 6.03 2 | Write a program that asks the user to enter a fraction, then reduces the 3 | fraction to lowest terms: 4 | 5 | ``` 6 | Enter a fraction: 6/12 7 | In lowest terms: 1/2 8 | ``` 9 | 10 | *Hint*: To reduce a fraction to lowest terms, first compute the GCD of the 11 | numberator and denominator. Then divide both the numerator and denominator by 12 | the GCD. 13 | 14 | ### Solution 15 | See `3.c`. 16 | -------------------------------------------------------------------------------- /06/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 6.04 2 | Add a loop to the `broker.c` program of Section 5.2 so that the user can enter 3 | more than one trade and the program will caluclate the commission on each. The 4 | program should terminate when the user enters `0` as the trade value: 5 | 6 | ``` 7 | Enter value of trade: 30000 8 | Commission: $166.00 9 | 10 | Enter value of trade: 20000 11 | Commission: $144.00 12 | 13 | Enter value of trade: 0 14 | ``` 15 | 16 | ### Solution 17 | See `4.c`. 18 | -------------------------------------------------------------------------------- /06/projects/05/5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int n; 6 | 7 | printf("Enter an integer: "); 8 | scanf("%d", &n); 9 | 10 | printf("Digits reversed: "); 11 | 12 | do { 13 | printf("%d", n % 10); 14 | n /= 10; 15 | } while (n != 0); 16 | 17 | printf("\n"); 18 | 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /06/projects/05/README.md: -------------------------------------------------------------------------------- 1 | ### Project 6.05 2 | Programming Project 1 in Chapter 4 asked you to write a program that displays a 3 | two-digit number with its digits reversed. Generalize the program so that the 4 | number can have one, two, three, or more digits. *Hint*: Use a `do` loop that 5 | repeatedly divides the number by 10, stopping when it reaches 0. 6 | 7 | ### Solution 8 | See `5.c`. 9 | -------------------------------------------------------------------------------- /06/projects/06/6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int n, i; 6 | 7 | printf("Enter a number: "); 8 | scanf("%d", &n); 9 | 10 | for (i = 1; i * i <= n; i++) { 11 | if ((i * i) % 2 != 0) 12 | continue; 13 | printf("%d\n", i * i); 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /06/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 6.06 2 | Write a program that prompts the user to enter a number n, then prints all even 3 | squares between 1 and n. For example, if the user enters 100, the program should 4 | print the following: 5 | 6 | ``` 7 | 4 8 | 16 9 | 36 10 | 64 11 | 100 12 | ``` 13 | 14 | ### Solution 15 | See `6.c`. 16 | -------------------------------------------------------------------------------- /06/projects/07/7.c: -------------------------------------------------------------------------------- 1 | /* Prints a table of squares using an odd method */ 2 | 3 | #include 4 | 5 | int main(void) { 6 | 7 | int i, n, odd, square; 8 | 9 | printf("This program prints a table of squares.\n"); 10 | printf("Enter number of entries in table: "); 11 | scanf("%d", &n); 12 | 13 | odd = 3; 14 | for (i = 1, square = 1; i <= n; odd += 2, ++i) { 15 | printf("%10d%10d\n", i, square); 16 | square += odd; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /06/projects/07/README.md: -------------------------------------------------------------------------------- 1 | ### Project 6.07 2 | Rearrange the `square3.c` program so that the `for` loop initializes `i`, tests 3 | `i`, and increments `i`. Don't rewrite the program; in particular, don't use any 4 | multiplications. 5 | 6 | ### Solution 7 | See `7.c`. 8 | -------------------------------------------------------------------------------- /06/projects/09/README.md: -------------------------------------------------------------------------------- 1 | ### Project 6.09 2 | Programming Project 8 in Chapter 2 asked you to write a program that calculates 3 | the remaining balance on a loan after the first, second, and third monthly 4 | payments. Modify the program so that it also asks the user to enter the number 5 | of payments and then displays the balance remaining after each of these 6 | payments. 7 | 8 | ### Solution 9 | See `9.c`. 10 | -------------------------------------------------------------------------------- /06/projects/10/README.md: -------------------------------------------------------------------------------- 1 | ### Project 6.10 2 | Programming Project 9 in Chapter 5 asked you to write a program that determines 3 | which of two dates comes earlier on the calendar. Generalize the program so that 4 | the user may enter any number of dates. The user will enter 0/0/0 to indicate 5 | that no more dates will be entered: 6 | 7 | ``` 8 | Enter a date (mm/dd/yy): 3/6/08 9 | Enter a date (mm/dd/yy): 5/17/07 10 | Enter a date (mm/dd/yy): 6/3/07 11 | Enter a date (mm/dd/yy): 0/0/0 12 | 5/17/07 is the earliest date 13 | ``` 14 | 15 | ### Solution 16 | See `10.c`. 17 | -------------------------------------------------------------------------------- /06/projects/11/11.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int i, n, denom; 6 | float e; 7 | 8 | printf("Enter integer n: "); 9 | scanf("%d", &n); 10 | 11 | for (i = 1, denom = 1, e = 1.0f; i <= n; i++) { 12 | e += 1.0f / (denom *= i); 13 | } 14 | printf("Approximation of e: %f\n", e); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /06/projects/11/README.md: -------------------------------------------------------------------------------- 1 | ### Project 6.11 2 | The value of the mathematical constant e can be expressed as an infinite series: 3 | 4 | e = 1 + 1/1! + 1/2! + 1/3! + ... 5 | 6 | Write a program that approximates e by computing the value of 7 | 8 | 1 + 1/1! + 1/2! + 1/3! + ... + 1/n! 9 | 10 | where n is an integer entered by the user. 11 | 12 | ### Solution 13 | See `11.c`. 14 | -------------------------------------------------------------------------------- /06/projects/12/12.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int i, denom; 6 | float e, epsilon, term; 7 | 8 | printf("Enter epsilon: "); 9 | scanf("%f", &epsilon); 10 | 11 | for (i = 1, denom = 1, e = 1.0f, term = 1.0f; term > epsilon; i++) { 12 | term = (1.0f / (denom *= i)); 13 | e += term; 14 | } 15 | printf("Approximation of e: %f\n", e); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /06/projects/12/README.md: -------------------------------------------------------------------------------- 1 | ### Project 6.12 2 | Modify Programming Project 11 so that the program continues adding terms until 3 | the current term becomes less than epsilon, where epsilon is a small 4 | (floating-point) number entered by the user. 5 | 6 | ### Solution 7 | See `12.c`. 8 | -------------------------------------------------------------------------------- /07/exercises/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.01 2 | Give the decimal value of each of the following integer constants. 3 | 4 | (a) `077` 5 | (b) `0x77` 6 | (c) `0XABC` 7 | 8 | ### Solution 9 | 10 | (a) 63 11 | (b) 119 12 | (c) 2748 13 | -------------------------------------------------------------------------------- /07/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.02 2 | Which of the following are not legal constants in C? Classify each legal 3 | constant as either integer or floating-point. 4 | 5 | (a) `010E2` 6 | (b) `32.1E+5` 7 | (c) `0790` 8 | (d) `100_000` 9 | (e) `3.987e-2` 10 | 11 | ### Solution 12 | 13 | (c) and (d) are illegal constants, because of undefined numeral `9` in octal 14 | number `0790`, and underscore in `100_000`. 15 | 16 | (a), (b) and (e) are legal, floating point constants. 17 | -------------------------------------------------------------------------------- /07/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.03 2 | Which of the following are not legal types in C? 3 | 4 | (a) `short unsigned int` 5 | (b) `short float` 6 | (c) `long double` 7 | (d) `unsigned long` 8 | 9 | ### Solution 10 | 11 | (b) `short float` is not a legal type. The only available floating point types 12 | are `float`, `double` and `long double`. 13 | -------------------------------------------------------------------------------- /07/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.04 2 | If `c` is a variable of type `char`, which one of the following statements is 3 | illegal? 4 | 5 | (a) `i += c; /* i has type int */` 6 | (b) `c = 2 * c - 1;` 7 | (c) `putchar(c);` 8 | (d) `printf(c);` 9 | 10 | ### Solution 11 | 12 | (d) `printf(c);` is illegal, because the `printf` function prints strings, not 13 | individual (numerical representations of) characters. 14 | -------------------------------------------------------------------------------- /07/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.05 2 | Which one of the following is not a legal way to write the number 65? (Assume 3 | that the character set is ASCII.) 4 | 5 | (a) `'A'` 6 | (b) `0b1000001` 7 | (c) `0101` 8 | (d) `0x41` 9 | 10 | ### Solution 11 | 12 | (b) `0b1000001` is illegal, as C does not allow binary represented numbers. 13 | -------------------------------------------------------------------------------- /07/exercises/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.06 2 | For each of the following items of data, specify which one of the types `char`, 3 | `short`, `int`, or `long` is the smallest one guaranteed to be large enough to 4 | store the item. 5 | 6 | (a) Days in a month 7 | (b) Days in a year 8 | (c) Minutes in a day 9 | (d) Seconds in a day 10 | 11 | ### Solution 12 | 13 | (a) Max. 31: `char` 14 | (b) Max. 366: `short` 15 | (c) Max. 1500: `short` 16 | (d) Max. 86760: `long` 17 | -------------------------------------------------------------------------------- /07/exercises/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.07 2 | For each of the following character escapes, give the equivalent octal escape. 3 | (Assume that the character set is ASCII.) You may which to consult Appendix E, 4 | which lists the numerical codes for ASCII characters. 5 | 6 | (a) `\b` 7 | (b) `\n` 8 | (c) `\r` 9 | (d) `\t` 10 | 11 | ### Solution 12 | 13 | (a) `\10` 14 | (b) `\12` 15 | (c) `\15` 16 | (d) `\11` 17 | -------------------------------------------------------------------------------- /07/exercises/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.08 2 | Repeat Exercise 7, but give the equivalent hexadecimal escape. 3 | 4 | ### Solution 5 | 6 | (a) `\x08` 7 | (b) `\x0a` 8 | (c) `\x0d` 9 | (d) `\x09` 10 | -------------------------------------------------------------------------------- /07/exercises/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.09 2 | Suppose that `i` and `j` are variables of type `int`. What is the type of the 3 | expression `i / j + 'a'`? 4 | 5 | ### Solution 6 | 7 | `int`. The expression automatically promotes the `char` value `'a'` to an `int`. 8 | -------------------------------------------------------------------------------- /07/exercises/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.10 2 | Suppose that `i` is a variable of type `int`, `j` is a variable of type `long`, 3 | and `k` is a variable of type `unsigned int`. What is the type of the expression 4 | `i + (int) j * k`? 5 | 6 | ### Solution 7 | `unsigned int`. `j` casts to `int`, and `k` promotes the expression to `unsigned 8 | int`. 9 | -------------------------------------------------------------------------------- /07/exercises/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.11 2 | Suppose that `i` is a variable of type `int`, `f` is a variable of type `float`, 3 | and `d` is a variable of type `double`. What is the type of the expression `i * 4 | f / d`? 5 | 6 | ### Solution 7 | 8 | `double`, as it will promote the other types automatically to `double`. 9 | -------------------------------------------------------------------------------- /07/exercises/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.12 2 | Suppose that `i` is a variable of type `int`, `f` is a variable of type `float`, 3 | and `d` is a varaible of type `double`. Explain what conversions take place 4 | during the execution of the statement: 5 | 6 | `d = i + f;` 7 | 8 | ### Solution 9 | 10 | The addition operation will require conversion of the value of `i` to `float`, 11 | and the assignment operation will require conversion of the summed value to 12 | `double`. 13 | -------------------------------------------------------------------------------- /07/exercises/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.13 2 | Assume that a program contains the following declarations: 3 | 4 | ```c 5 | char c = '\1'; 6 | short s = 2; 7 | int i = -3; 8 | long m = 5; 9 | float f = 6.5f; 10 | double d = 7.5; 11 | ``` 12 | 13 | Give the value and the type of each expression listed below. 14 | 15 | (a) `c * i` 16 | (b) `s + m` 17 | (c) `f / c` 18 | (d) `d / s` 19 | (e) `f - d` 20 | (f) `(int) f` 21 | 22 | ### Solution 23 | 24 | (a) `-3`, `int` 25 | (b) `7`, `long` 26 | (c) `6.5f`, `float` 27 | (d) `3.75`, `double` 28 | (e) `-1.0`, `double` 29 | (f) `6`, `int` 30 | -------------------------------------------------------------------------------- /07/exercises/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.14 2 | Does the following statement always compute the fractional part of `f` correctly 3 | (assuming that `f` and `frac_part` are `float` variables)? 4 | 5 | ```c 6 | frac_part = f - (int) f; 7 | ``` 8 | 9 | If not, what's the problem? 10 | 11 | ### Solution 12 | 13 | No, the statement could fail if the value of `f` is larger than the largest 14 | value of `int`. 15 | -------------------------------------------------------------------------------- /07/exercises/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.15 2 | Use `typedef` to create types names `Int8`, `Int16`, and `Int32`. Define the 3 | types so that they represent 8-bit, 16-bit and 32-bit integers on your machine. 4 | 5 | ### Solution 6 | 7 | ```c 8 | typedef Int8 short; 9 | typedef Int16 int; 10 | typedef Int32 long; 11 | ``` 12 | -------------------------------------------------------------------------------- /07/projects/01/1.c: -------------------------------------------------------------------------------- 1 | /* Prints a table of squares using a for statement */ 2 | 3 | #include 4 | 5 | int main(void) { 6 | 7 | short i, n; 8 | 9 | printf("This program prints a table of squares.\n"); 10 | printf("Enter number of entries in table: "); 11 | scanf("%d", &n); 12 | 13 | for (i = 1; i <= n; i++) 14 | printf("%20d%20d\n", i, i * i); 15 | 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /07/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 7.02 2 | Modify the `square2.c` program of Section 6.3 so that it pauses after every 24 3 | squares and displays the following message: 4 | 5 | `Press Enter to continue...` 6 | 7 | After displaying the message, the program should use `getchar` to read a 8 | character. `getchar` won't allow the program to continue until the user pressed 9 | the Enter key. 10 | 11 | ### Solution 12 | See `2.c`. 13 | -------------------------------------------------------------------------------- /07/projects/03/3.c: -------------------------------------------------------------------------------- 1 | /* Sums a series of numbers (using double variables) */ 2 | 3 | #include 4 | 5 | int main(void) { 6 | 7 | double n, sum = 0.0; 8 | 9 | printf("This program sums a series of floating-point numbers.\n"); 10 | printf("Enter numbers (0 to terminate): "); 11 | 12 | scanf("%lf", &n); 13 | while (n != 0.0) { 14 | sum += n; 15 | scanf("%lf", &n); 16 | } 17 | printf("The sum is: %f\n", sum); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /07/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 7.03 2 | Modify the `sum2.c` program of Section 7.1 to sum a series of `double` values. 3 | 4 | ### Solution 5 | See `3.c`. 6 | -------------------------------------------------------------------------------- /07/projects/06/6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | printf("%lu, %lu, %lu, %lu, %lu, %lu\n", 6 | (unsigned long) sizeof(int), (unsigned long) sizeof(short), 7 | (unsigned long) sizeof(long), (unsigned long) sizeof(float), 8 | (unsigned long) sizeof(double), (unsigned long) sizeof(long double)); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /07/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 7.06 2 | Write a program that prints the values of `sizeof(int)`, `sizeof(short)`, 3 | `sizeof(long)`, `sizeof(float)`, `sizeof(double)` and `sizeof(long double)`. 4 | 5 | ### Solution 6 | See `6.c`. 7 | -------------------------------------------------------------------------------- /07/projects/07/README.md: -------------------------------------------------------------------------------- 1 | ### Project 7.07 2 | Modify Programming Project 6 from Chapter 3 so that the user may add, subtract, 3 | multiply or divide two fractions (by entering either +, -, * or / between the 4 | fractions). 5 | 6 | ### Solution 7 | See `7.c`. 8 | -------------------------------------------------------------------------------- /07/projects/09/9.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | 6 | 7 | int hour, minute; 8 | char c; 9 | 10 | printf("Enter a 12-hour time: "); 11 | scanf("%d :%d %c", &hour, &minute, &c); 12 | 13 | hour = (hour == 12 ? 0 : hour); 14 | if (toupper(c) == 'P') 15 | hour += 12; 16 | 17 | printf("Equivalent 24-hour time: %.2d:%.2d\n", hour, minute); 18 | 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /07/projects/09/README.md: -------------------------------------------------------------------------------- 1 | ### Project 7.09 2 | Write a program that asks the user for a 12-hour time, then displays the time in 3 | 24-hour form: 4 | 5 | ``` 6 | Enter a 12-hour time: 9:11 PM 7 | Equivalent 24-hour time: 21:11 8 | ``` 9 | 10 | See Programming Project 8 for a description of the input format. 11 | 12 | ### Solution 13 | See `9.c`. 14 | -------------------------------------------------------------------------------- /07/projects/10/10.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | 6 | char c; 7 | int vowels = 0; 8 | 9 | printf("Enter a sentence: "); 10 | 11 | while ((c = getchar()) != '\n') { 12 | switch (toupper(c)) { 13 | case 'A': case 'E': case 'I': case 'O': case 'U': 14 | vowels++; 15 | break; 16 | default: 17 | break; 18 | } 19 | } 20 | printf("Your sentence contains %d vowels.\n", vowels); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /07/projects/10/README.md: -------------------------------------------------------------------------------- 1 | ### Project 7.10 2 | Write a program that counts the number of vowels (a, e, i, o and u) in a 3 | sentence: 4 | 5 | ``` 6 | Enter a sentence: And that's the way it is. 7 | Your sentence contains 6 vowels. 8 | ``` 9 | 10 | ### Solution 11 | See `10.c`. 12 | -------------------------------------------------------------------------------- /07/projects/11/11.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | char c, initial; 6 | 7 | printf("Enter a first and last name: "); 8 | 9 | while ((c = getchar()) == ' ') 10 | ; 11 | initial = c; 12 | while ((c = getchar()) != ' ') 13 | ; 14 | while ((c = getchar()) != '\n') 15 | putchar(c); 16 | printf(", %c.\n", initial); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /07/projects/11/README.md: -------------------------------------------------------------------------------- 1 | ### Project 7.11 2 | Write a program that takes a first name and a last name entered by the user and 3 | displays the last name, a comma, and the first initial, followed by a period: 4 | 5 | ``` 6 | Enter a first and last name: Lloyd Fosdick 7 | Fodsick, L. 8 | ``` 9 | 10 | The user's input may contain extre spaces before the first name, between the 11 | first and last names, and after the last name. 12 | 13 | ### Solution 14 | See `11.c`. 15 | -------------------------------------------------------------------------------- /07/projects/12/README.md: -------------------------------------------------------------------------------- 1 | ### Project 7.12 2 | Write a program that evaluates an expression: 3 | 4 | ``` 5 | Enter an expression: 1+2.5*3 6 | Value of expression: 10.5 7 | ``` 8 | 9 | The operands in the expression are floating-point numbers; the operators are 10 | `+`, `-`, `*` and `/`. The expression is evalued from left to right (no operator 11 | taes precedence over any other operator). 12 | 13 | ### Solution 14 | See `12.c`. 15 | -------------------------------------------------------------------------------- /07/projects/13/13.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | char c; 6 | double length = 0.0; 7 | int words = 1; 8 | 9 | printf("Enter a sentence: "); 10 | 11 | while ((c = getchar()) != '\n') { 12 | if (c == ' ') 13 | words++; 14 | else 15 | length++; 16 | } 17 | 18 | printf("Average word length: %.1f\n", length / words); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /07/projects/13/README.md: -------------------------------------------------------------------------------- 1 | ### Project 7.13 2 | Write a program that calculates the average word length for a sentence: 3 | 4 | ``` 5 | Enter a sentence: It was deja vu all over again. 6 | Average word length: 3.4 7 | ``` 8 | 9 | For simplicity, your program should consider a punctuation mark to be part of 10 | the word to which it is attached. Display the average word length to one decimal 11 | place. 12 | 13 | ### Solution 14 | See `13.c`. 15 | -------------------------------------------------------------------------------- /07/projects/14/14.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | 6 | double x, y = 1.0; 7 | 8 | printf("Enter a positive number: "); 9 | scanf("%lf", &x); 10 | 11 | while (fabs((y + x / y) / 2 - y) > .00001 * y) 12 | y = (y + x / y) / 2; 13 | 14 | printf("Square root: %f\n", y); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /07/projects/15/15.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int i, n; 6 | long double fact = 1.0; 7 | 8 | printf("Enter a positive integer: "); 9 | scanf("%d", &n); 10 | 11 | for (i = n; i > 1; i--) 12 | fact *= i; 13 | 14 | printf("Factorial of %d: %Lf\n", n, fact); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /08/exercises/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.01 2 | We discussed using the expression `sizeof(a) / sizeof(a[0])` to calculate the 3 | number of elements in an array. The expression `sizeof(a) / sizeof(`*t*`)`, 4 | where *t* is the type of `a`'s elements, would also work, but it's considered an 5 | inferior technique. Why? 6 | 7 | ### Solution 8 | 9 | The second expression may not necessarily reflect the type of a value in `a`. 10 | For example, if `a` was changed from an array of `int`s to an array of `long`s, 11 | then each instance of `sizeof(`*t*`)` would have to be changed. 12 | -------------------------------------------------------------------------------- /08/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.02 2 | The Q&A section shows how to use a *letter* as an array subscript. Describe how 3 | to use a *digit* (in character form) as a subscript. 4 | 5 | ### Solution 6 | If `c` is a `char` containing a digit character `'5'`: 7 | 8 | `digit_count[c - '0'] == digit_count[5]; /* true */` 9 | -------------------------------------------------------------------------------- /08/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.03 2 | Write a declaration of an array named `weekend` containing seven `bool` values. 3 | Include an initializer that makes the first and last values `true`; all other 4 | values should be `false`; 5 | 6 | ### Solution 7 | 8 | ```c 9 | int weekend[7] = {1, 0, 0, 0, 0, 0, 1}; 10 | ``` 11 | -------------------------------------------------------------------------------- /08/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.04 2 | (C99) Repeat Exercise 3, but this time use a designated initializer. Make the 3 | initializer as short as possible. 4 | 5 | ### Solution 6 | 7 | ```c 8 | bool weekend[7] = {[0] = true, [6] = true}; 9 | ``` 10 | -------------------------------------------------------------------------------- /08/exercises/07/.README.md.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zydchina818/C-/c858e108f04d788be752353ed578c4d09328ef15/08/exercises/07/.README.md.swp -------------------------------------------------------------------------------- /08/exercises/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.08 2 | Write a declaration for a two-dimensional array named `temperature_readings` 3 | that stores one month of hourly temperature readings. (For simplicity, assume 4 | that a month has 30 days.) The rows of the array should represent days of the 5 | month; the colums should represent hours of the day. 6 | 7 | ### Solution 8 | 9 | ```c 10 | double temperature_readings[30][24] = {0}; 11 | ``` 12 | -------------------------------------------------------------------------------- /08/exercises/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.09 2 | Using the array of Exercise 8, write a program fragment that computes the 3 | average temperature for a month (averaged over all days of the month and all 4 | hours of the day). 5 | 6 | ### Solution 7 | 8 | ```c 9 | #define DAYS 30 10 | #define HOURS 24 11 | int i, j; 12 | double average = 0.0, 13 | temperature_readings[DAYS][HOURS]; 14 | 15 | for (i = 0; i < DAYS; i++) { 16 | for (j = 0; j < HOURS; j++) { 17 | average += temperature_readings[i][j]; 18 | } 19 | } 20 | average /= DAYS * HOURS; 21 | ``` 22 | -------------------------------------------------------------------------------- /08/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 8.01 2 | Modify the `repdigit.c` program of Section 8.1 so that it shows which digits (if 3 | any) were repeated: 4 | 5 | ``` 6 | Enter a number: 939577 7 | Repeated digit(s): 7 9 8 | ``` 9 | 10 | ### Solution 11 | See `1.c`. 12 | -------------------------------------------------------------------------------- /08/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 8.02 2 | Modify the `repdigit.c` program of Section 8.1 so that it prints a table showing 3 | how many times each digit appears in the number: 4 | 5 | ``` 6 | Enter a number: 41271092 7 | Digit: 0 1 2 3 4 5 6 7 8 9 8 | Occurrences: 1 2 2 0 1 0 0 1 0 1 9 | ``` 10 | 11 | ### Solution 12 | See `2.c`. 13 | -------------------------------------------------------------------------------- /08/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 8.03 2 | Modift the `repdigit.c` program of Section 8.1 so that the user can enter more 3 | than one number to be tested for repeated digits. The program should terminate 4 | when the user enters a number that's less than or equal to 0. 5 | 6 | ### Solution 7 | See `3.c`. 8 | -------------------------------------------------------------------------------- /08/projects/04/4.c: -------------------------------------------------------------------------------- 1 | /* Reverse a series of numbers */ 2 | 3 | #include 4 | 5 | #define SIZE (int) (sizeof(a) / sizeof(a[0])) 6 | 7 | int main(void) { 8 | 9 | int a[10], i; 10 | 11 | printf("Enter %d numbers: ", SIZE); 12 | for (i = 0; i < SIZE; i++) 13 | scanf("%d", &a[i]); 14 | 15 | printf("In reverse order:"); 16 | for (i = SIZE - 1; i >= 0; i--) 17 | printf(" %d", a[i]); 18 | printf("\n"); 19 | 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /08/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 8.04 2 | Modify the `reverse.c` program of Section 8.1 to use the expression `(int) 3 | (sizeof(a) / sizeof(a[0]))` (or a macro with this value for the array length. 4 | 5 | ### Solution 6 | See `4.c`. 7 | -------------------------------------------------------------------------------- /08/projects/05/README.md: -------------------------------------------------------------------------------- 1 | ### Project 8.05 2 | Modify the `interest.c` program of Section 8.1 so that it compound interest 3 | monthly instead of annually. The form of the output shouldn't change: the 4 | balance should still be shown at annual intervals. 5 | 6 | ### Solution 7 | See `5.c`. 8 | -------------------------------------------------------------------------------- /08/projects/07/README.md: -------------------------------------------------------------------------------- 1 | ### Project 8.07 2 | Write a program that reads a 5 x 5 array of integers and then prints the row 3 | sums and the column sums: 4 | 5 | ``` 6 | Enter row 1: 8 3 9 0 10 7 | Enter row 2: 3 5 17 1 1 8 | Enter row 3: 2 8 6 23 1 9 | Enter row 4: 15 7 3 2 9 10 | Enter row 5: 6 14 2 6 0 11 | 12 | Row totals: 30 27 40 36 28 13 | Column totals: 34 37 37 32 21 14 | ``` 15 | 16 | ### Solution 17 | See `7.c`. 18 | -------------------------------------------------------------------------------- /08/projects/08/README.md: -------------------------------------------------------------------------------- 1 | ### Project 8.08 2 | Modify Programming Project 7 so that it prompts for five quiz grades for each of 3 | five students, then computes the total score and average score for each student, 4 | and the average score, high score, and low score for each quiz. 5 | 6 | ### Solution 7 | See `8.c`. 8 | -------------------------------------------------------------------------------- /08/projects/10/README.md: -------------------------------------------------------------------------------- 1 | ### Project 8.10 2 | Modify Programming Project 8 from Chapter 5 so that the departure times are 3 | stored in an array and the arrival times are stored in a second array. (The 4 | times are integers, representing the number of minutes since midnight.) The 5 | program will use a loop to search the array of departure times for the one 6 | closest to the time entered by the user. 7 | 8 | ### Solution 9 | See `10.c`. 10 | -------------------------------------------------------------------------------- /08/projects/11/README.md: -------------------------------------------------------------------------------- 1 | ### Project 8.11 2 | Modify Programming Project 4 from Chapter 7 so that the program labels its 3 | output: 4 | 5 | ``` 6 | Enter phone number: 1-800-COL-LECT 7 | In numeric form: 1-800-265-5328 8 | ``` 9 | 10 | The program will need to store the phone number (either in its original form or 11 | in its numeric form) in an array of character until it can be printed. You may 12 | assume that the phone number is no more than 15 characters long. 13 | 14 | ### Solution 15 | See `11.c`. 16 | -------------------------------------------------------------------------------- /08/projects/12/12.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | 6 | char c; 7 | int sum, 8 | scrabble[26] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 9 | 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 10 | 1, 4, 4, 8, 4, 10}; 11 | 12 | printf("Enter a word: "); 13 | 14 | while ((c = getchar()) != '\n') { 15 | sum += scrabble[toupper(c) - 'A']; 16 | } 17 | printf("Scrabble value: %d\n", sum); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /08/projects/12/README.md: -------------------------------------------------------------------------------- 1 | ### Project 8.12 2 | Modify Programming Project 5 from Chapter 7 so that the SCRABBLE values of the 3 | letters are stored in an array. The array wil have 26 elements, corresponding to 4 | the 26 letters of the alphabet. For example, element 0 of the array will store 1 5 | (because the SCRABBLE value of the letter B is 3), and so forth. As each 6 | character of the input word is read, the program will use the array to determine 7 | the SCRABBLE value of that character. Use an array initializer to set up the 8 | array. 9 | 10 | ### Solution 11 | See `12.c`. 12 | -------------------------------------------------------------------------------- /08/projects/13/13.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int i; 6 | char c, initial, last[20] = {0}; 7 | 8 | printf("Enter a first and last name: "); 9 | 10 | while ((c = getchar()) == ' ') 11 | ; 12 | initial = c; 13 | while ((getchar() != ' ')) 14 | ; 15 | for (i = 0; (c = getchar()) != '\n'; i++) 16 | last[i] = c; 17 | 18 | printf("You entered the name: "); 19 | for (i = 0; last[i] != '\0'; i++) 20 | putchar(last[i]); 21 | printf(", %c.\n", initial); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /08/projects/13/README.md: -------------------------------------------------------------------------------- 1 | ### Project 8.13 2 | Modify Programming Project 11 from Chapter 7 so that the program labels its 3 | output: 4 | 5 | ``` 6 | Enter a first and last name: Lloyd Fosdick 7 | You entered the name: Fosdick, L. 8 | ``` 9 | 10 | The program will need to store the last name (but not the first name) in an 11 | array of characters until it can be printed. You may assume that the last name 12 | is no more than 20 characters long. 13 | 14 | ### Solution 15 | See `13.c`. 16 | -------------------------------------------------------------------------------- /09/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.02 2 | Write a function `check(x, y, n)` that returns 1 if both `x` and `y` fall 3 | between 0 and `n` - 1, inclusive. The function should return 0 otherwise. Assume 4 | that `x`, `y` and `n` are all of type `int`. 5 | 6 | ### Solution 7 | 8 | ```c 9 | int check(int x, int y, int n) { 10 | if (x >= 0 && x <= n - 1 && y >= 0 && y <= n - 1) 11 | return 1; 12 | return 0; 13 | } 14 | ``` 15 | -------------------------------------------------------------------------------- /09/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.03 2 | Write a function `gcd(m, n)` that calculates the greatest common divisor of the 3 | integers `m` and `n`. (Programming Project 2 in Chapter 6 describes Euclid's 4 | algorithm for computing the GCD.) 5 | 6 | ### Solution 7 | 8 | ```c 9 | int gcd(int m, int n) { 10 | 11 | int r; 12 | while (n > 0) { 13 | r = m % n; 14 | m = n; 15 | n = r; 16 | } 17 | 18 | return m; 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /09/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.05 2 | Write a function `num_digits(n)` that returns the number of digits in `n` (a 3 | positive integer). *Hint*: To determine the number of digits in a number *n*, 4 | divide it by 10 repeatedly. When *n* reaches 0, the number of divisions 5 | indicates how many digits *n* originally had. 6 | 7 | ### Solution 8 | 9 | ```c 10 | int num_digits(int n) { 11 | 12 | int i; 13 | while (n / 10 != 0) 14 | i++; 15 | return i; 16 | } 17 | ``` 18 | -------------------------------------------------------------------------------- /09/exercises/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.06 2 | Write a function `digit(n, k)` that returns the `k`th digit (from the 3 | right) in `n` (a positive integer). For example `digit(829, 1)` returns 9, 4 | `digit(829, 2)` returns 2 and `digit(829, 3)` returns 8. if `k` is greater than 5 | the number of digits in `n`, have the function return 0. 6 | 7 | ### Solution 8 | 9 | ```c 10 | int digit(int n, int k) { 11 | 12 | while (k-- > 1) { 13 | if (n /= 10 <= 0) 14 | return 0; 15 | } 16 | return n % 10; 17 | } 18 | ``` 19 | -------------------------------------------------------------------------------- /09/exercises/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.07 2 | Suppose that the function `f` has the following definition: 3 | 4 | `int f(int a, int b) {` ... `}` 5 | 6 | Which of the following statements are legal? Assume that `i` has type `int` and 7 | `x` has type `double`. 8 | 9 | (a) `i = f(83, 12);` 10 | (b) `x = f(83, 12);` 11 | (c) `i = f(3.15, 9.28);` 12 | (d) `x = f(3.15, 9.28);` 13 | (e) `f(83, 12);` 14 | 15 | ### Solution 16 | 17 | All of the statements are legal. 18 | -------------------------------------------------------------------------------- /09/exercises/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.08 2 | Which of the following would be valid prototypes for a function that returns 3 | nothing and has one `double` parameter? 4 | 5 | (a) `void f(double x);` 6 | (b) `void f(double);` 7 | (c) `void f(x);` 8 | (d) `f(double x);` 9 | 10 | ### Solution 11 | (a) and (b) are the only valid prototypes which meet the requirements. In (c), 12 | `x` has no provided type, causing an error. In (d), the function `f` has no 13 | provided type, causing an error in C99 and defaulting to `int` in C89. 14 | -------------------------------------------------------------------------------- /09/exercises/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.12 2 | Write the following function: 3 | 4 | ```c 5 | double inner_product(double a[], double b[], int n); 6 | ``` 7 | 8 | The function should return `a[0] * b[0] + a[1] * b[1] + ` ... `+ a[n-1] * 9 | b[n-1]`. 10 | 11 | ### Solution 12 | 13 | ```c 14 | double inner_product(double a[], double b[], int n) { 15 | 16 | int i; 17 | double result; 18 | 19 | for (i = 0; i < n; i++) 20 | result += (a[i] * b[i]); 21 | 22 | return result; 23 | } 24 | ``` 25 | -------------------------------------------------------------------------------- /09/exercises/16/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.16 2 | Condense the `fact` function in the same way we condensed `power`. 3 | 4 | ### Solution 5 | 6 | Here is the `fact` function as written in Section 9.6: 7 | 8 | ```c 9 | int fact(int n) 10 | { 11 | if (n <= 1) 12 | return 1; 13 | else 14 | return n * fact(n - 1); 15 | } 16 | ``` 17 | 18 | And the condensed result: 19 | 20 | ```c 21 | int fact(int n) { 22 | return n <= 1 ? 1 : n * fact(n - 1); 23 | } 24 | ``` 25 | -------------------------------------------------------------------------------- /09/exercises/17/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.17 2 | Rewrite the `fact` function so that it's no longer recursive. 3 | 4 | ### Solution 5 | 6 | The original `fact` function from Section 9.6: 7 | 8 | ```c 9 | int fact(int n) 10 | { 11 | if (n <= 1) 12 | return 1; 13 | else 14 | return n * fact(n - 1); 15 | } 16 | ``` 17 | 18 | And the iterative `fact` function: 19 | 20 | ```c 21 | int fact(int n) { 22 | 23 | int result = 1; 24 | 25 | while (n-- > 1) 26 | result *= n; 27 | 28 | return result; 29 | } 30 | ``` 31 | -------------------------------------------------------------------------------- /09/exercises/18/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.18 2 | Write a recursive version of the `gcd` function (see Exercise 3). Here's the 3 | strategy to use for computing `gcd(m, n)`: If `n` is 0, return `m`. Otherwise, 4 | call `gcd` recursively, passing `n` as the first argument and `m % n` as the 5 | second. 6 | 7 | ### Solution 8 | 9 | ```c 10 | int gcd(int m, int n) { 11 | 12 | return n == 0 ? m : gcd(n, m % n); 13 | } 14 | ``` 15 | -------------------------------------------------------------------------------- /09/exercises/19/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.19 2 | Consider the following "mystery" function: 3 | 4 | ```c 5 | void pb(int n) 6 | { 7 | if (n != 0) { 8 | pb(n / 2); 9 | putchar('0' + n % 2); 10 | } 11 | } 12 | ``` 13 | 14 | Trace the execution of the function by hand. Then write a program that calls the 15 | function, passing it a number entered by the user. What does the function do? 16 | 17 | ### Solution 18 | 19 | The function prints the binary representation of the integer passed to it. See 20 | `pb.c` for a complete program implementation. 21 | -------------------------------------------------------------------------------- /09/exercises/19/pb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void pb(int n); 4 | 5 | int main(void) { 6 | 7 | int n; 8 | printf("Enter an integer to be translated to binary: "); 9 | scanf("%d", &n); 10 | 11 | pb(n); 12 | printf("\n"); 13 | return 0; 14 | } 15 | 16 | void pb(int n) { 17 | 18 | if (n != 0) { 19 | pb(n / 2); 20 | putchar('0' + n % 2); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /09/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 9.01 2 | Write a program that asks the user to enter a series of integers (which it 3 | stores in an array), then sorts the integers by calling the function 4 | `selection_sort`. When given an array with *n* elements, `selection_sort` must 5 | do the following: 6 | 1. Search the array to find the largest element, then move it to the last 7 | position in the array. 8 | 2. Call itself recursively to sort the first *n* - 1 elements of the array. 9 | 10 | ### Solution 11 | 12 | See `1.c`. 13 | -------------------------------------------------------------------------------- /09/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 9.02 2 | Modify Programming Project 5 from Chapter 5 so that it uses a function to 3 | compute the amount of income tax. When passed an amount of taxable income, the 4 | function will return the tax due. 5 | 6 | ### Solution 7 | See `2.c`. 8 | -------------------------------------------------------------------------------- /09/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 9.03 2 | Modify Programming Project 9 from Chapter 8 so that it includes the following 3 | functions: 4 | 5 | ```c 6 | void generate_random_walk(char walk[10][10]); 7 | void print_array(char walk[10][10]); 8 | ``` 9 | 10 | `main` first calls `generate_random_walk`, which initializes the array to 11 | contain `'.'` characters and then replaces some of these characters by the 12 | letters `A` through `Z`, as described in the original project. `main` then calls 13 | `print_array` to display the array on the screen. 14 | 15 | ### Solution 16 | See `3.c`. 17 | -------------------------------------------------------------------------------- /09/projects/06/6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int polynomial(int x); 4 | 5 | int main(void) { 6 | 7 | int x; 8 | 9 | printf("Enter an integer: "); 10 | scanf("%d", &x); 11 | printf("Result: %d\n", polynomial(x)); 12 | 13 | return 0; 14 | } 15 | 16 | int polynomial(int x) { 17 | 18 | return 3 * x * x * x * x * x + 2 * x * x * x * x - 5 * x * x * x - x * x 19 | + 7 * x - 6; 20 | } 21 | -------------------------------------------------------------------------------- /09/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 9.06 2 | Write a function that computes the value of the following polynomial: 3 | 4 | 3*x*5 + 2*x*4 - 5*x*3 - *x*² + 7x - 6. 5 | 6 | Write a program that asks the user to enter a value for *x*, calls the function 7 | to compute the value of the polynomial, and then displays the value returned by 8 | the function. 9 | 10 | ### Solution 11 | See `6.c`. 12 | -------------------------------------------------------------------------------- /09/projects/07/7.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int power(int x, int n); 4 | 5 | int main(void) { 6 | 7 | int x, n; 8 | 9 | printf("For x^n, enter x: "); 10 | scanf("%d", &x); 11 | printf("Enter n: "); 12 | scanf("%d", &n); 13 | 14 | printf("%d^%d = %d\n", x, n, power(x, n)); 15 | return 0; 16 | } 17 | 18 | int power(int x, int n) { 19 | 20 | int i; 21 | 22 | if (n == 0) 23 | return 1; 24 | if (n % 2 == 0) { 25 | i = power(x, n / 2); 26 | return i * i; 27 | } else 28 | return x * power(x, n - 1); 29 | } 30 | -------------------------------------------------------------------------------- /10/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.03 2 | Suppose that a program has only one function (`main`). How many different 3 | variables names `i` could this program contain? 4 | 5 | ### Solution 6 | There is no limit to the number of blocks that can be declared within `main`, so 7 | there is no limmit to the number of `i` variables that the program could 8 | contain. Otherwise, there can be one external variable and one internal variable 9 | named `i`. 10 | -------------------------------------------------------------------------------- /10/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 10.02 2 | Modify the `poker.c` program of Section 10.5 by moving the `num_in_rank` and 3 | `num_in_suit` arrays into `main`, which will pass them as arguments to 4 | `read_cards` and `analyze_hand`. 5 | 6 | ### Solution 7 | See `2.c`. 8 | -------------------------------------------------------------------------------- /10/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 10.03 2 | Remove the `num_in_rank`, `num_in_suit` and `card_exists` arrays from the 3 | `poker.c` program of Section 10.5. Have the program store the cards in a 5 x 2 4 | array instead. Each row of the array will represent a card. For example, if the 5 | array is named `hand`, then `hand[0][0]` will store the rank of the first card 6 | and `hand[0][1]` will store the suit of the first card. 7 | 8 | ### Solution 9 | See `3.c`. 10 | -------------------------------------------------------------------------------- /10/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 10.04 2 | Modify the `poker.c` program of Section 10.5 by having it recognize an 3 | additional category, "royal flush" (ace, king, queen, jack, ten of the same 4 | suit). A royal flush ranks higher than all other ranks. 5 | 6 | ### Solution 7 | See `4.c`. 8 | -------------------------------------------------------------------------------- /10/projects/05/README.md: -------------------------------------------------------------------------------- 1 | ### Project 10.05 2 | Modify the `poker.c` program of Section 10.5 by allowing "ace-low" straights 3 | (ace, two, three, four, five). 4 | 5 | ### Solution 6 | See `5.c`. 7 | -------------------------------------------------------------------------------- /11/exercises/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.01 2 | If `i` is a variable and `p` points to `i`, which of the following expressions 3 | are aliases for `i`? 4 | 5 | (a) `*p` (c) `*&p` (e) `*i` (g) `*&i` 6 | (b) `&p` (d) `&*p` (f) `&i` (h) `&*i` 7 | 8 | ### Solution 9 | (a) and (g) are aliases for `i`. 10 | -------------------------------------------------------------------------------- /11/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.02 2 | If `i` is an `int` variable and `p` and `q` are pointers to `int`, which of the 3 | following assignments are legal? 4 | 5 | (a) `p = i;` (d) `p = &q;` (g) `p = *q;` 6 | (b) `*p = &i;` (e) `p = *&q;` (h) `*p = q;` 7 | (c) `&p = q;` (f) `p = q;` (i) `*p = *q;` 8 | 9 | ### Solution 10 | (e), (f) and (i) are legal statements. 11 | -------------------------------------------------------------------------------- /11/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.04 2 | Write the following function: 3 | 4 | ```c 5 | void swap(int *p, int *q); 6 | ``` 7 | 8 | When passed the addresses of two variables, `swap` should exchange the values of 9 | the variables: 10 | 11 | ```c 12 | swap(&i, &j); /* exchanges values of i and j */ 13 | ``` 14 | 15 | ### Solution 16 | 17 | ```c 18 | void swap(int *p, int *q) { 19 | 20 | int temp = *q; 21 | *q = *p; 22 | *p = temp; 23 | } 24 | ``` 25 | -------------------------------------------------------------------------------- /11/exercises/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.08 2 | Write the following function: 3 | 4 | ```c 5 | int *find_largest(int a[], int n); 6 | ``` 7 | 8 | When passed an array `a` of length `n`, the function will return a pointer to 9 | the array's largest element. 10 | 11 | ### Solution 12 | 13 | ```c 14 | int *find_largest(int a[], int n) { 15 | 16 | int i, *largest = &a[0]; 17 | 18 | for (i = 1; i < n; i++) { 19 | if (a[i] > *largest) 20 | largest = &a[i]; 21 | } 22 | return largest; 23 | } 24 | -------------------------------------------------------------------------------- /11/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 11.01 2 | Modify Programming Project 7 from Chapter 2 so that it includes the following 3 | function: 4 | 5 | ```c 6 | void pay_amount(int dollars, int *twenties, int *tens, int *fives, int *ones); 7 | ``` 8 | 9 | The function determines the smallest number of $20, $10, $5 and $1 bills 10 | necessary to pay the amount represented by the `dollars` parameter. The 11 | `twenties` parameter points to a variable in which the function will store the 12 | number of $20 bills required. The `tens`, `fives` and `ones` parameters are 13 | similar. 14 | 15 | ### Solution 16 | See `1.c`. 17 | -------------------------------------------------------------------------------- /11/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 11.03 2 | Modify Programming Project 3 from Chapter 6 so that it includes the following 3 | function: 4 | 5 | ```c 6 | void reduce(int numerator, int denominator, 7 | int *reduced_numerator, int *reduced_denominator); 8 | ``` 9 | 10 | `numerator` and `denominator` are the numerator and denominator of a fraction. 11 | `reduced_numerator` and `reduced_denominator` are pointers to variables in which 12 | the function will store the numerator and denominator of the fraction once it 13 | has been reduced to lowest terms. 14 | 15 | ### Solution 16 | See `3.c`. 17 | -------------------------------------------------------------------------------- /11/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 11.04 2 | Modify the `poker.c` program of Section 10.5 by moving all external variables 3 | into `main` and modifying functions so that they communicate by passing 4 | arguments. The `analyze_hand` function needs to change the `straight`, `flush`, 5 | `four`, `three` and `pairs` variables, so it will have to be passed pointers to 6 | those variables. 7 | 8 | ### Solution 9 | See `4.c`. 10 | -------------------------------------------------------------------------------- /12/exercises/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.01 2 | Suppose that the following declarations are in effect: 3 | 4 | ```c 5 | int a[] = {5, 15, 34, 54, 14, 2, 52, 72}; 6 | int *p = &a[1], *q = &a[5]; 7 | ``` 8 | 9 | (a) What is the value of `*(p+3)`? 10 | (b) What is the value of `*(q-3)`? 11 | (c) What is the value of `q - p`? 12 | (d) Is the condition `p < q` true or false? 13 | (e) Is the condition `*p < *q` true or false? 14 | 15 | ### Solution 16 | 17 | (a) `14` 18 | (b) `34` 19 | (c) `4` 20 | (d) `true` 21 | (e) `false` 22 | -------------------------------------------------------------------------------- /12/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.02 2 | Suppose that `high`, `low` and `middle` are all pointer variables of the same 3 | type, and that `low` and `high` point to elements of an array. What is the 4 | following statement illegal, and how could it be fixed? 5 | 6 | ```c 7 | middle = (low + high) / 2; 8 | ``` 9 | 10 | ### Solution 11 | 12 | The statement is illegal because pointers cannot be added together. The 13 | statement can be fixed by subtracting the pointers, which is legal: 14 | 15 | ```c 16 | middle = (high - low) / 2 + low; 17 | ``` 18 | -------------------------------------------------------------------------------- /12/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.03 2 | What will be the contents of the `a` array after the following statements are 3 | executed? 4 | 5 | ```c 6 | #define N 10 7 | 8 | int a[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 9 | int *p = &a[0], *q = &a[N-1], temp; 10 | 11 | while (p < q) { 12 | temp = *p; 13 | *p++ = *q; 14 | *q-- = temp; 15 | } 16 | ``` 17 | 18 | ### Solution 19 | 20 | ```c 21 | {10, 9, 8, 7, 6, 5, 4, 3, 2, 1} 22 | ``` 23 | -------------------------------------------------------------------------------- /12/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.04 2 | Rewrite the `make_empty`, `is_empty` and `is_full` functions of Section 10.2 to 3 | use the pointer variable `top_ptr` instead of the integer variable `top`. 4 | 5 | ### Solution 6 | ```c 7 | void make_empty(void) { 8 | 9 | top_ptr = &contents[0]; 10 | } 11 | 12 | bool is_empty(void) { 13 | 14 | return top_ptr == &contents[0]; 15 | } 16 | 17 | bool is_full(void) { 18 | 19 | return top_ptr = &contents[STACK_SIZE]; 20 | } 21 | ``` 22 | -------------------------------------------------------------------------------- /12/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.05 2 | Suppose that `a` is a one-dimensional array and `p` is a pointer variable. 3 | Assuming that the assignment `p = a` has just been performed, which of the 4 | following expressions are illegal because of mismatched types? Of the remaining 5 | expressions, which are true (have a nonzero value)? 6 | 7 | (a) `p == a[0]` 8 | (b) `p == &a[0]` 9 | (c) `*p == a[0]` 10 | (d) `p[0] == a[0]` 11 | 12 | ### Solution 13 | (a) is illegal. (b), (c) and (d) all return true values. 14 | -------------------------------------------------------------------------------- /12/exercises/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.08 2 | Rewrite the following function to use pointer arithmetic instead of array 3 | subscripting. (In other words, eliminate the variable `i` and all uses of the 4 | `[]` operator.) Make as few changes as possible. 5 | 6 | ```c 7 | void store_zeros(int a[], int n) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < n; i++) 12 | a[i] = 0; 13 | } 14 | ``` 15 | 16 | ### Solution 17 | 18 | ```c 19 | void store_zeros(int a[], int n) { 20 | 21 | int *p; 22 | 23 | for (p = a; p < a + n; p++) 24 | *p = 0; 25 | } 26 | ``` 27 | -------------------------------------------------------------------------------- /12/exercises/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.10 2 | Modify the `find_middle` function of Section 11.5 so that it uses pointer 3 | arithmetic to calculate the return value. 4 | 5 | ### Solution 6 | 7 | ```c 8 | int *find_middle(int a[], int n) { 9 | 10 | return a + n / 2; 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /12/exercises/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.11 2 | Modify the `find_largest` function so that it uses pointer arithmetic -- not 3 | subscripting -- to visit array elements. 4 | 5 | ### Solution 6 | 7 | ```c 8 | int find_largest(int a[], int n) { 9 | 10 | int *p = a; 11 | int largest = *p++; 12 | 13 | while (p++ < a + n) { 14 | if (*p > largest) { 15 | largest = *p; 16 | } 17 | } 18 | return largest; 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /12/exercises/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.14 2 | Assume that the following array contains a week's worth of hourly temperature 3 | readings, with each row containing the readings for one day: 4 | 5 | ```c 6 | int temperatures[7][24]; 7 | ``` 8 | 9 | Write a statement that uses the `search` function (see Exercise 7) to search the 10 | entire `temperatures` array for the value 32. 11 | 12 | ### Solution 13 | 14 | ```c 15 | bool has32 = search(temperatures, 7 * 24, 32); 16 | ``` 17 | -------------------------------------------------------------------------------- /12/exercises/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.15 2 | Write a loop that prints all temperature readings stored in row `i` of the 3 | `temperatures` array (see Exercise 14). Use a pointer to visit each element of 4 | the row. 5 | 6 | ### Solution 7 | 8 | ```c 9 | int *p = temperatures[i]; 10 | while (p < temperatures[i + 1]) 11 | printf("%d ", *p++); 12 | ``` 13 | -------------------------------------------------------------------------------- /12/exercises/16/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.16 2 | Write a loop that prints the highest temperature in the `temperatures` array 3 | (see Exercise 14) for each day of the week. The loop body should call the 4 | `find_largest` function, passing it one row of the array at a time. 5 | 6 | ### Solution 7 | 8 | ```c 9 | int i; 10 | for (i = 0; i < 7; i++) 11 | printf("Day %d: %d\n", i + 1, find_largest(temperatures[i], 24)); 12 | ``` 13 | -------------------------------------------------------------------------------- /12/projects/01/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_LEN 100 4 | 5 | int main(void) { 6 | 7 | char message[MAX_LEN]; 8 | char c, *p = message; 9 | 10 | printf("Enter a message: "); 11 | 12 | while ((c = getchar()) != '\n' && p < message + MAX_LEN) 13 | *p++ = c; 14 | 15 | p--; 16 | printf("Reversal is: "); 17 | 18 | while (p >= message) 19 | putchar(*p--); 20 | 21 | printf("\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /12/projects/03/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_LEN 100 4 | 5 | int main(void) { 6 | 7 | char message[MAX_LEN]; 8 | char c, *p = message; 9 | 10 | printf("Enter a message: "); 11 | 12 | while ((c = getchar()) != '\n' && p < message + MAX_LEN) 13 | *p++ = c; 14 | 15 | p--; 16 | printf("Reversal is: "); 17 | 18 | while (p >= message) 19 | putchar(*p--); 20 | 21 | printf("\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /12/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 12.03 2 | Simplify Programming Project 1(b) by taking advantage of the fact that an array 3 | name can be used as a pointer. 4 | 5 | ### Solution 6 | See `3.c`. NB: My original solution to 1(b) is identical to this one. 7 | -------------------------------------------------------------------------------- /12/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 12.04 2 | Simplify Programming Project 2(b) by taking advantage of that fact that an array 3 | name can be used as a pointer. 4 | 5 | ### Solution 6 | See `4.c`. NB: My original solution to 2(b) is identical to this one. 7 | -------------------------------------------------------------------------------- /12/projects/05/README.md: -------------------------------------------------------------------------------- 1 | ### Project 12.05 2 | Modify Programming Project 14 from Chapter 8 so that it uses a pointer instead 3 | of an integer to keep track of the current position in the array that contains 4 | the sentence. 5 | 6 | ### Solution 7 | See `5.c`. 8 | -------------------------------------------------------------------------------- /12/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 12.06 2 | Modify the `qsort.c` program of Section 9.6 so that `low`, `high` and `middle` 3 | are pointers to array elements rather than integers. The `split` function will 4 | need to return a pointer, not an integer. 5 | 6 | ### Solution 7 | See `6.c`. 8 | -------------------------------------------------------------------------------- /12/projects/07/README.md: -------------------------------------------------------------------------------- 1 | ### Project 12.07 2 | Modify the `maxmin.c` program of Section 11.4 so that the `max_min` function 3 | uses a pointer instead of an integer to keep track of the current position in 4 | the array. 5 | 6 | ### Solution 7 | See `7.c`. 8 | -------------------------------------------------------------------------------- /13/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.02 2 | Suppose that `p` has been declared as follows: 3 | 4 | ```c 5 | char *p = "abc"; 6 | ``` 7 | 8 | Which of the followin function calls are legal? Show the output produced by each 9 | legal call, and explain why the others are illegal. 10 | 11 | (a) `putchar(p);` 12 | (b) `putchar(*p);` 13 | (c) `puts(p);` 14 | (d) `puts(*p);` 15 | 16 | (a) Illegal: `putchar` expects a `char`, receives a pointer to `char`. 17 | (b) `a` 18 | (c) `abc\n` 19 | (d) Illegal: `puts` expects a `char *` and receives a `char`. 20 | -------------------------------------------------------------------------------- /13/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.03 2 | Suppose that we call `scanf` as follows: 3 | 4 | ```c 5 | scanf("%d%s%d", %i, s, &j); 6 | ``` 7 | 8 | If the user enters `12abc34 56def78`, what will be the values of `i`, `s` and 9 | `j` after the call? (Assume that `i` and `j` are `int` variables and `s` is an 10 | array of characters.) 11 | 12 | ### Solution 13 | `i`: `12` 14 | `s`: `abc34` 15 | `j`: `56` 16 | -------------------------------------------------------------------------------- /13/exercises/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.06 2 | Write a function named `censor` that modifies a string by replacing every 3 | occurrence of `foo` by `xxx`. For example, the string `"food fool"` would become 4 | `"xxxd xxxl"`. Make the function as short as possible without sacrificing 5 | clarity. 6 | 7 | ### Solution 8 | 9 | ```c 10 | void censor(char *str) { 11 | char *c = str; 12 | while (c+2 != '\0') { 13 | if (*c == 'f' && *(c+1) == 'o' && *(c+2) == 'o') 14 | *c = *(c+1) = *(c+2) = 'x'; 15 | c++; 16 | } 17 | } 18 | ``` 19 | -------------------------------------------------------------------------------- /13/exercises/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.07 2 | Suppose that `str` is an array of characters. Which one of the following 3 | statements is not equivalent to the other three? 4 | 5 | (a) `*str = 0;` 6 | (b) `str[0] = '\0';` 7 | (c) `strcpy(str, "");` 8 | (d) `strcat(str, "");` 9 | 10 | ### Solution 11 | (d) is not equivalent to the other three options, as `strcat` appends the null 12 | character to the end of `str`, while the other three insert the value at the 13 | first character of `str`. 14 | -------------------------------------------------------------------------------- /13/exercises/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.08 2 | What will be the value of the string `str` after the following statements have 3 | been executed? 4 | 5 | ```c 6 | strcpy(str, "tire-bouchon"); 7 | strcpy(&str[4], "d-or-wi"); 8 | strcat(str, "red?"); 9 | ``` 10 | 11 | ### Solution 12 | 13 | `"tired-or-wired?\0"` 14 | -------------------------------------------------------------------------------- /13/exercises/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.09 2 | What will be the value of the string `s1` after the following statements have 3 | been executed? 4 | 5 | ```c 6 | strcpy(s1, "computer"); 7 | strcpy(s2, "science"); 8 | if (strcmp(s1, s2) < 0) 9 | strcat(s1, s2); 10 | else 11 | strcat(s2, s1); 12 | s1[strlen(s1)-6] = '\0'; 13 | ``` 14 | 15 | ### Solution 16 | `"computers\0"` 17 | -------------------------------------------------------------------------------- /13/exercises/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.10 2 | The following function supposedly creates an identical copy of a string. What's 3 | wrong with the function? 4 | 5 | ```c 6 | char *duplicate(const char *p) 7 | { 8 | char *q; 9 | 10 | strcpy(q, p); 11 | return q; 12 | } 13 | ``` 14 | 15 | ### Solution 16 | The string `q` points to will not be accessible outside the scope of the 17 | function. 18 | -------------------------------------------------------------------------------- /13/exercises/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.11 2 | The Q&A section at the end of this chapter shows how the `strcmp` function might 3 | be written using array subscripting. Modify the function to use pointer 4 | arithmetic instead. 5 | 6 | ### Solution 7 | 8 | ```c 9 | int strcmp(char *s, char *t) { 10 | 11 | while(*s++ == *t++) 12 | if (s[i] == '\0') 13 | return 0; 14 | return *s - *t; 15 | } 16 | ``` 17 | -------------------------------------------------------------------------------- /13/exercises/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.14 2 | What does the following program print? 3 | 4 | ```c 5 | #include 6 | 7 | int main(void) 8 | { 9 | char s[] = "Hsjodi", *p; 10 | 11 | for (p = s; *p; p++) 12 | --*p; 13 | puts(s); 14 | return 0; 15 | } 16 | ``` 17 | 18 | ### Solution 19 | 20 | `Grinch`, followed by a newline. 21 | -------------------------------------------------------------------------------- /13/exercises/16/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.16 2 | 3 | Use the techniques of Section 13.6 to condense the `count_spaces` function of 4 | Section 13.4. In particular, replace the `for` statement by a `while` loop. 5 | 6 | ### Solution 7 | 8 | ```c 9 | int count_spaces(const char *s) 10 | { 11 | int count = 0; 12 | 13 | while (*s++) 14 | if (*s == ' ') 15 | count++; 16 | return count; 17 | } 18 | ``` 19 | -------------------------------------------------------------------------------- /13/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.03 2 | 3 | Modify the `deal.c` program of Section 8.2 so that it prints the full names of 4 | the cards it deals: 5 | 6 | ``` 7 | Enter number of cards in hand: 5 8 | You hand: 9 | Seven of clubs 10 | Two of spades 11 | Five of diamonds 12 | Ace of spades 13 | Two of hearts 14 | ``` 15 | 16 | *Hint*: Replace `rank_code` and `suit_code` by arrays containing pointers to 17 | strings. 18 | 19 | ### Solution 20 | See `3.c`. 21 | -------------------------------------------------------------------------------- /13/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.04 2 | 3 | Write a program named `reverse.c` that echoes its command-line arguments in 4 | reverse order. Running the program by typing 5 | 6 | ``` 7 | reverse void and null 8 | ``` 9 | 10 | should produce the following output: 11 | 12 | ``` 13 | null and void 14 | ``` 15 | 16 | ### Solution 17 | 18 | See `reverse.c`. 19 | -------------------------------------------------------------------------------- /13/projects/04/reverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]) { 4 | 5 | int i; 6 | 7 | for (i = argc - 1; i > 0; i--) 8 | printf("%s ", argv[i]); 9 | printf("\n"); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /13/projects/05/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.05 2 | 3 | Write a program named `sum.c` that adds up its command-line arguments, which are 4 | assumed to be integers. Running the program by typing 5 | 6 | ``` 7 | sum 8 24 62 8 | ``` 9 | 10 | should produce the following output: 11 | 12 | ``` 13 | Total: 94 14 | ``` 15 | 16 | *Hint*: Use the `atoi` function to convert each command-line argument from 17 | string form to integer form. 18 | 19 | ### Solution 20 | 21 | See `sum.c`. 22 | -------------------------------------------------------------------------------- /13/projects/05/sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char *argv[]) { 5 | 6 | int i, sum = 0; 7 | for (i = 1; i < argc; i++) 8 | sum += atoi(argv[i]); 9 | printf("Total: %d\n", sum); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /13/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.06 2 | 3 | Improve the `planet.c` program of Section 13.7 by having it ignore case when 4 | comparing command-line arguments with strings in the `planets` array. 5 | 6 | ### Solution 7 | 8 | See `6.c`. 9 | -------------------------------------------------------------------------------- /13/projects/07/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.07 2 | 3 | Modify Programming Project 11 from Chapter 5 so that it uses arrays containing 4 | pointers to strings instead of `switch` tatements. For example, instead of using 5 | a `switch` statement to print the words for the first digit, use the digit as an 6 | index into an array that contains the strings `"twenty"`, `"thirty"` and so 7 | forth. 8 | 9 | ### Solution 10 | 11 | See `7.c`. 12 | -------------------------------------------------------------------------------- /13/projects/08/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.08 2 | 3 | Modify Programming Project 5 from Chapter 7 so that it includes the following 4 | function: 5 | 6 | ```c 7 | int compute_scrabble_value(const char *word); 8 | ``` 9 | 10 | The function returns the SCRABBLE value of the string pointed to by `word`. 11 | 12 | ### Solution 13 | 14 | See `8.c`. 15 | -------------------------------------------------------------------------------- /13/projects/09/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.09 2 | 3 | Modify Programming Project 10 from Chapter 7 so that it includes the following 4 | function: 5 | 6 | ```c 7 | int compute_vowel_count(const char *sentence); 8 | ``` 9 | 10 | The function returns the number of vowels in the string pointed to by the 11 | `sentence` parameter. 12 | 13 | ### Solution 14 | See `9.c`. 15 | -------------------------------------------------------------------------------- /13/projects/10/10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void reverse_name(char *name); 4 | 5 | int main(void) { 6 | 7 | char name[81]; 8 | 9 | printf("Enter a first and last name: "); 10 | fgets(name, sizeof(name), stdin); 11 | reverse_name(name); 12 | 13 | return 0; 14 | } 15 | 16 | void reverse_name(char *name) { 17 | 18 | char *p = name, initial; 19 | 20 | while (*p == ' ') 21 | p++; 22 | initial = *p++; 23 | 24 | while (*p && *p++ != ' ') 25 | ; 26 | 27 | while (*p && *p != '\n') 28 | putchar(*p++); 29 | printf(", %c.\n", initial); 30 | } 31 | -------------------------------------------------------------------------------- /13/projects/11/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.11 2 | 3 | Modify Programming Project 13 from Chapter 7 so that it includes the following 4 | function: 5 | 6 | ```c 7 | double compute_average_word_length(const char *sentence); 8 | ``` 9 | 10 | The function returns the average length of the words in the string pointed to by 11 | `sentence`. 12 | 13 | ### Solution 14 | 15 | See `11.c`. 16 | -------------------------------------------------------------------------------- /13/projects/12/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.12 2 | 3 | Modify Programming Project 14 from Chapter 8 so that it stores the words in a 4 | two-dimensional `char` array as it reads the sentence, with each row of the 5 | array storing a single word. Assume that the sentence contains no more than 30 6 | words and no word is more than 20 characters long. Be sure to store a null 7 | character at the end of each word so that it can be treated as a string. 8 | 9 | ### Solution 10 | 11 | See `12.c`. 12 | -------------------------------------------------------------------------------- /13/projects/13/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.13 2 | 3 | Modify Programming Project 15 from Chapter 8 so that it includes the following 4 | function: 5 | 6 | ```c 7 | void encrypt(char *message, int shift); 8 | ``` 9 | 10 | The function expects `message` to point to a string containing the message to be 11 | encrypted; `shift` represents the amount by which each letter in the message is 12 | to be shifted. 13 | 14 | ### Solution 15 | 16 | See `13.c`. 17 | -------------------------------------------------------------------------------- /13/projects/14/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.14 2 | 3 | Modify Programming Project 16 from Chapter 8 so that it includes the following 4 | function: 5 | 6 | ```c 7 | bool are_anagrams(const char *word1, const char *word2); 8 | ``` 9 | 10 | The function returns `true` if the strings pointed to by `word1` and `word2` are 11 | anagrams. 12 | 13 | ### Solution 14 | 15 | See `14.c`. 16 | -------------------------------------------------------------------------------- /13/projects/15/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.15 2 | 3 | Modify Programming Project 6 from Chapter 10 so that it includes the following 4 | function: 5 | 6 | ```c 7 | int evaluate_RPN_expression(const char *expression); 8 | ``` 9 | 10 | The function returns the value of the RPN expression pointed to by `expression`. 11 | 12 | ### Solution 13 | 14 | See `15.c`. 15 | -------------------------------------------------------------------------------- /13/projects/16/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.16 2 | 3 | Modify Programming Project 1 from Chapter 12 so that it includes the following 4 | function: 5 | 6 | ```c 7 | void reverse(char *message); 8 | ``` 9 | 10 | The function reverses the string pointed to by message. *Hint:* Use two 11 | pointers, one initially pointing to the first character of the string and the 12 | other initially pointing to the last character. Have the function reverse these 13 | characters and then move the pointers toward each other, repeating the process 14 | until the pointers meet. 15 | 16 | ### Solution 17 | 18 | See `16.c`. 19 | -------------------------------------------------------------------------------- /13/projects/17/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.17 2 | 3 | Modify Programming Project 2 from Chapter 12 so that it includes the following 4 | function: 5 | 6 | ```c 7 | bool is_palindrome(const char *message); 8 | ``` 9 | 10 | The function returns `true` if the string pointed to by `message` is a 11 | palindrome. 12 | 13 | ### Solution 14 | 15 | See `17.c`. 16 | -------------------------------------------------------------------------------- /13/projects/18/18.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int m, d, y; 6 | 7 | char *months[12] = {"January", "February", "March", "April", "May", "June", 8 | "July", "August", "September", "October", "November", 9 | "December"}; 10 | 11 | printf("Enter a date (mm/dd/yyyy): "); 12 | scanf("%d / %d / %d", &m, &d, &y); 13 | printf("You entered the date %s %.2d, %d\n", months[m-1], d, y); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /13/projects/18/README.md: -------------------------------------------------------------------------------- 1 | ### Project 13.18 2 | 3 | Write a program that accepts a date from the user in the form *mm/dd/yyyy* and 4 | then displays it in the form *month dd, yyyy* where *month* is the name of the 5 | month: 6 | 7 | ``` 8 | Enter a date (mm/dd/yyyy): 2/17/2011 9 | You entered the date February 17, 2011 10 | ``` 11 | 12 | Store the month names in an array that contains pointers to strings. 13 | 14 | ### Solution 15 | 16 | See `18.c`. 17 | -------------------------------------------------------------------------------- /14/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.02 2 | 3 | Write a macro `NELEMS(a)` that computes the number of elements in a 4 | one-dimensional array `a`. *Hint:* See the discussion of the `sizeof` operator 5 | in Section 8.1 6 | 7 | ### Solution 8 | 9 | ```c 10 | #define NELEMS(a) (sizeof(a) / sizeof(a[0])) 11 | ``` 12 | -------------------------------------------------------------------------------- /14/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.03 2 | 3 | Let `DOUBLE` be the following macro: 4 | 5 | ```c 6 | #define DOUBLE(x) 2*x 7 | ``` 8 | 9 | (a) What is the value of `DOUBLE(1+2)`? 10 | (b) What is the value of `4/DOUBLE(2)`? 11 | (c) Fix the definition of `DOUBLE`. 12 | 13 | ### Solution 14 | 15 | #### (a) 16 | 17 | 4. 18 | 19 | #### (b) 20 | 21 | 4. 22 | 23 | #### (c) 24 | 25 | ```c 26 | #define DOUBLE(x) (2*(x)) 27 | ``` 28 | -------------------------------------------------------------------------------- /14/exercises/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.10 2 | 3 | Functions can often -- but not always -- be written as parameterized macros. 4 | Discuss what characteristics of a function would make it unsuitable as a macro. 5 | 6 | ### Solution 7 | 8 | Recursive functions and functions with arguments which have side effects have 9 | different behaviour as a macro, making a function more suitable. 10 | -------------------------------------------------------------------------------- /14/exercises/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.12 2 | 3 | Suppose that the macro `M` has been defined as follows: 4 | 5 | ```c 6 | #define M 10 7 | ``` 8 | 9 | Which of the following tests will fail? 10 | 11 | (a) `#if M` 12 | (b) `#ifdef M` 13 | (c) `#ifndef M` 14 | (d) `#if defined(M)` 15 | (e) `#if !defined(M)` 16 | 17 | ### Solution 18 | 19 | (c) and (e) will fail their tests, as `M` is defined. 20 | -------------------------------------------------------------------------------- /14/exercises/16/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.16 2 | 3 | (C99) Assume that the following macro definitions are in effect: 4 | 5 | ```c 6 | #define IDENT(x) PRAGMA(ident #x) 7 | #define PRAGMA(x) _Pragma(#x) 8 | ``` 9 | 10 | What will the following line look like after macro expansion? 11 | 12 | ```c 13 | IDENT(foo) 14 | ``` 15 | 16 | ### Solution 17 | 18 | ```c 19 | #pragma ident "foo" 20 | ``` 21 | -------------------------------------------------------------------------------- /15/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.02 2 | 3 | Which of the following should *not* be put into a header file? Why not? 4 | 5 | (a) Function prototypes 6 | (b) Function definitions 7 | (c) Macro definitions 8 | (d) Type definitions 9 | 10 | ### Solution 11 | 12 | Function definitions should not be included in a header file because it would 13 | potentially define individual functions multiple times. 14 | -------------------------------------------------------------------------------- /15/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.05 2 | 3 | Suppose that a program consists of three source files -- `main.c`, `f1.c` and 4 | `f2.c` -- plus two header files, `f1.h` and `f2.h`. All three source files 5 | include `f1.h`, but only `f1.c` and `f2.c` include `f2.h`. Write a makefile for 6 | this program, assuming that the compiler is `gcc` and that the executable file 7 | is to be named `demo`. 8 | 9 | ### Solution 10 | 11 | See `makefile`. 12 | -------------------------------------------------------------------------------- /15/exercises/05/makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | demo: main.o f1.o f2.o 5 | $(CC) -o demo main.o f1.o f2.o $(CFLAGS) 6 | 7 | main.o: main.c f1.h 8 | $(CC) -c main.c $(CFLAGS) 9 | 10 | f1.o: f1.c f1.h f2.h 11 | $(CC) -c f1.c $(CFLAGS) 12 | 13 | f2.o: f2.c f1.h f2.h 14 | $(CC) -c f2.c $(CFLAGS) 15 | -------------------------------------------------------------------------------- /15/projects/01/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | justify: justify.o word.o line.o 5 | $(CC) -o justify justify.o word.o line.o $(CFLAGS) 6 | 7 | justify.o: justify.c word.h line.h 8 | $(CC) -c justify.c $(CFLAGS) 9 | 10 | word.o: word.c word.h 11 | $(CC) -c word.c $(CFLAGS) 12 | 13 | line.o: line.c line.h 14 | $(CC) -c line.c $(CFLAGS) 15 | -------------------------------------------------------------------------------- /15/projects/01/new_output.txt: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an enormous success. Although 2 | accidents of history surely helped, it evidently satisfied a 3 | need for a system implementation language efficient enough 4 | to displace assembly language, yet sufficiently abstract and 5 | fluent to describe algorithms and interactions in a wide 6 | variety of environments. -- Dennis M. Ritchie 7 | -------------------------------------------------------------------------------- /15/projects/01/old_output.txt: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an enormous success. Although 2 | accidents of history surely helped, it evidently satisfied a 3 | need for a system implementation language efficient enough 4 | to displace assembly language, yet sufficiently abstract and 5 | fluent to describe algorithms and interactions in a wide 6 | variety of environments. -- Dennis M. Ritchie 7 | -------------------------------------------------------------------------------- /15/projects/01/quote.txt: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an 2 | enormous success. Although accidents of history 3 | surely helped, it evidently satisfied a need 4 | 5 | for a system implementation language efficient 6 | enough to displace assembly language, 7 | yet sufficiently abstract and fluent to describe 8 | algorithms and interactions in a wide variety 9 | of environments. 10 | -- Dennis M. Ritchie 11 | -------------------------------------------------------------------------------- /15/projects/01/word.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "word.h" 3 | 4 | int read_char(void) { 5 | int ch = getchar(); 6 | 7 | if (ch == '\n' || ch == '\t') 8 | return ' '; 9 | return ch; 10 | } 11 | 12 | void read_word(char *word, int len) { 13 | 14 | int ch, pos = 0; 15 | 16 | while ((ch = read_char()) == ' ') 17 | ; 18 | while (ch != ' ' && ch != EOF) { 19 | if (pos < len) 20 | word[pos++] = ch; 21 | ch = read_char(); 22 | } 23 | word[pos] = '\0'; 24 | } 25 | -------------------------------------------------------------------------------- /15/projects/01/word.h: -------------------------------------------------------------------------------- 1 | #ifndef WORD_H 2 | #define WORD_H 3 | 4 | /* 5 | * read_word: Reads the next word from the input and stores it in word. Makes 6 | * word empty if no word could be read because of the end-of-file. 7 | * Truncates the word if its length exceeds len. 8 | */ 9 | void read_word(char *word, int len); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /15/projects/02/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | justify: justify.o word.o line.o 5 | $(CC) -o justify justify.o word.o line.o $(CFLAGS) 6 | 7 | justify.o: justify.c word.h line.h 8 | $(CC) -c justify.c $(CFLAGS) 9 | 10 | word.o: word.c word.h 11 | $(CC) -c word.c $(CFLAGS) 12 | 13 | line.o: line.c line.h 14 | $(CC) -c line.c $(CFLAGS) 15 | -------------------------------------------------------------------------------- /15/projects/02/new_output.txt: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an enormous success. Although 2 | accidents of history surely helped, it evidently satisfied a 3 | need for a system implementation language efficient enough 4 | to displace assembly language, yet sufficiently abstract and 5 | fluent to describe algorithms and interactions in a wide 6 | variety of environments. -- Dennis M. Ritchie 7 | -------------------------------------------------------------------------------- /15/projects/02/old_output.txt: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an enormous success. Although 2 | accidents of history surely helped, it evidently satisfied a 3 | need for a system implementation language efficient enough 4 | to displace assembly language, yet sufficiently abstract and 5 | fluent to describe algorithms and interactions in a wide 6 | variety of environments. -- Dennis M. Ritchie 7 | -------------------------------------------------------------------------------- /15/projects/02/quote.txt: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an 2 | enormous success. Although accidents of history 3 | surely helped, it evidently satisfied a need 4 | 5 | for a system implementation language efficient 6 | enough to displace assembly language, 7 | yet sufficiently abstract and fluent to describe 8 | algorithms and interactions in a wide variety 9 | of environments. 10 | -- Dennis M. Ritchie 11 | -------------------------------------------------------------------------------- /15/projects/02/word.h: -------------------------------------------------------------------------------- 1 | #ifndef WORD_H 2 | #define WORD_H 3 | 4 | /* 5 | * read_word: Reads the next word from the input and stores it in word. Makes 6 | * word empty if no word could be read because of the end-of-file. 7 | * Truncates the word if its length exceeds len. 8 | */ 9 | void read_word(char *word, int len); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /15/projects/03/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | qsort: qsort.o quicksort.o 5 | $(CC) -o qsort qsort.o quicksort.o $(CFLAGS) 6 | 7 | qsort.o: qsort.c quicksort.h 8 | $(CC) -c qsort.c $(CFLAGS) 9 | 10 | quicksort.o: quicksort.c quicksort.h 11 | $(CC) -c quicksort.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /15/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 15.03 2 | 3 | Modify the `qsort.c` program of Section 9.6 so that the `quicksort` and `split` 4 | functions are in a separate file named `quicksort.c`. Create a header file named 5 | `quicksort.h` that contains prototypes for the two functions and have both 6 | `qsort.c` and `quicksort.c` include this file. 7 | 8 | ### Solution 9 | 10 | See program files. 11 | -------------------------------------------------------------------------------- /15/projects/03/qsort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "quicksort.h" 3 | 4 | #define N 10 5 | 6 | int main(void) { 7 | 8 | int a[N], i; 9 | 10 | printf("Enter %d numbers to be sorted: ", N); 11 | for (i = 0; i < N; i++) 12 | scanf("%d", &a[i]); 13 | quicksort(a, 0, N - 1); 14 | 15 | printf("In sorted order: "); 16 | for (i = 0; i < N; i++) 17 | printf("%d ", a[i]); 18 | printf("\n"); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /15/projects/03/quicksort.h: -------------------------------------------------------------------------------- 1 | #ifndef QUICKSORT_H 2 | #define QUICKSORT_H 3 | 4 | void quicksort(int a[], int low, int high); 5 | int split(int a[], int low, int high); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /15/projects/04/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | remind: remind.o readline.o 5 | $(CC) -o remind remind.o readline.o $(CFLAGS) 6 | 7 | remind.o: remind.c readline.h 8 | $(CC) -c remind.c $(CFLAGS) 9 | 10 | readline.o: readline.c readline.h 11 | $(CC) -c readline.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /15/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 15.04 2 | 3 | Modify the `remind.c` program of Section 13.5 so that the `read_line` function 4 | is in a separate file named `readline.c`. Create a header file named 5 | `readline.h` that contains a prototype for the function and have both `remind.c` 6 | and `readline.c` include this file. 7 | 8 | ### Solution 9 | 10 | See project files. 11 | -------------------------------------------------------------------------------- /15/projects/04/readline.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "readline.h" 3 | 4 | int read_line(char str[], int n) { 5 | 6 | int ch, i = 0; 7 | 8 | while ((ch = getchar()) != '\n') 9 | if (i < n) 10 | str[i++] = ch; 11 | str[i] = '\0'; 12 | return i; 13 | } 14 | -------------------------------------------------------------------------------- /15/projects/04/readline.h: -------------------------------------------------------------------------------- 1 | #ifndef READLINE_H 2 | #define READLINE_H 3 | 4 | int read_line(char str[], int n); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /15/projects/05/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | calc: calc.o stack.o 5 | $(CC) -o calc calc.o stack.o $(CFLAGS) 6 | 7 | calc.o: calc.c stack.h 8 | $(CC) -c calc.c $(CFLAGS) 9 | 10 | stack.o: stack.c stack.h 11 | $(CC) -c stack.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /15/projects/05/README.md: -------------------------------------------------------------------------------- 1 | ### Project 15.05 2 | 3 | Modify Programming Project 6 from Chapter 10 so that it has separate `stack.h` 4 | and `stack.c` files, as described in Section 15.2. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /15/projects/05/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_H 2 | #define STACK_H 3 | 4 | void make_empty(void); 5 | bool is_empty(void); 6 | bool is_full(void); 7 | void push(char i); 8 | char pop(void); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /16/exercises/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 16.01 2 | 3 | In the following declarations, the `x` and `y` structures have members named `x` 4 | and `y`: 5 | 6 | ```c 7 | struct { int x, y;} x; 8 | struct { int x, y;} y; 9 | ``` 10 | 11 | Are these declarations legal on an individual basis? Could both declarations 12 | appear as shown in a program? Justify your answer. 13 | 14 | ### Solution 15 | 16 | Both declarations are legal individually as well as together: the structures' 17 | member variables are only accessible with the `.` operator, and their scope is 18 | limited to their structure. 19 | -------------------------------------------------------------------------------- /16/exercises/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 16.15 2 | 3 | (a) Declare a tag for an enumerataion whose values represent the seven days of 4 | the week. 5 | (b) Use `typedef` to define a name for the enumeration of part (a). 6 | 7 | ### Solution 8 | 9 | #### (a) 10 | 11 | ```c 12 | enum weekdays = {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, 13 | FRIDAY, SATURDAY, SUNDAY}; 14 | ``` 15 | 16 | #### (b) 17 | 18 | ```c 19 | typedef enum {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, 20 | FRIDAY, SATURDAY, SUNDAY} Day; 21 | ``` 22 | -------------------------------------------------------------------------------- /16/exercises/19/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 16.19 2 | 3 | Declare a structure with the following members whose tag is `pinball_machine`: 4 | 5 | `name` -- a string of up to 40 characters 6 | `year` -- an integer (representing the year of manufacture) 7 | `type` -- an enumeration with the values `EM` (electromechanical) and `SS` 8 | (solid state) 9 | `players` -- an integer (representing the maximum number of players) 10 | 11 | ### Solution 12 | 13 | ```c 14 | struct pinball_machine { 15 | char[40] name; 16 | int year; 17 | enum { EM, SS } type; 18 | int players; 19 | }; 20 | ``` 21 | -------------------------------------------------------------------------------- /16/exercises/21/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 16.21 2 | 3 | What are the integer values of the enumeration constants in each of the 4 | following declarations? 5 | 6 | (a) `enum {NUL, SOH, STX, ETX};` 7 | (b) `enum {VT = 11, FF, CR};` 8 | (c) `enum {SO = 14, ST, DLE, CAN = 24, EM};` 9 | (d) `enum {ENQ = 45, ACK, BEL, LF = 37, ETB, ESC};` 10 | 11 | ### Solution 12 | 13 | #### (a) 14 | 15 | 0, 1, 2 and 3. 16 | 17 | #### (b) 18 | 19 | 11, 12 and 13. 20 | 21 | #### (c) 22 | 23 | 14, 15, 16, 24 and 25. 24 | 25 | #### (d) 26 | 27 | 45, 46, 47, 37, 38 and 39. 28 | -------------------------------------------------------------------------------- /16/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 16.01 2 | 3 | Write a program that asks the user to enter an international dialing code and 4 | then looks it up in the `country_codes` array (see Section 16.3). If it finds 5 | the code, the program should display the name of the corresponding country; if 6 | not, the program should print an error message. 7 | 8 | ### Solution 9 | 10 | See `1.c`. 11 | -------------------------------------------------------------------------------- /16/projects/02/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | inventory: inventory.o readline.o quicksort.o 5 | $(CC) -o inventory inventory.o readline.o quicksort.o $(CFLAGS) 6 | 7 | inventory.o: inventory.c inventory.h readline.h quicksort.h 8 | $(CC) -c inventory.c $(CFLAGS) 9 | 10 | readline.o: readline.c readline.h 11 | $(CC) -c readline.c $(CFLAGS) 12 | 13 | quicksort.o: quicksort.c quicksort.h inventory.h 14 | $(CC) -c quicksort.c $(CFLAGS) 15 | -------------------------------------------------------------------------------- /16/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 16.02 2 | 3 | Modify the `inventory.c` program of Section 16.3 so that the `p` (print) 4 | operation displays the parts sorted by part number. 5 | 6 | ### Solution 7 | 8 | See program files. NB: `quicksort.c` and `quicksort.h` are copied from 9 | Programming Project 15.03, altered to accept `struct part` instead of `int`. 10 | -------------------------------------------------------------------------------- /16/projects/02/inventory.h: -------------------------------------------------------------------------------- 1 | #ifndef INVENTORY_H 2 | #define INVENTORY_H 3 | 4 | #define NAME_LEN 25 5 | #define MAX_PARTS 100 6 | 7 | struct part { 8 | int number; 9 | char name[NAME_LEN+1]; 10 | int on_hand; 11 | } inventory[MAX_PARTS]; 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /16/projects/02/quicksort.h: -------------------------------------------------------------------------------- 1 | /* quicksort, edited for struct part */ 2 | 3 | #ifndef QUICKSORT_H 4 | #define QUICKSORT_H 5 | 6 | #include "inventory.h" 7 | 8 | void quicksort(struct part a[], int low, int high); 9 | int split(struct part a[], int low, int high); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /16/projects/02/readline.c: -------------------------------------------------------------------------------- 1 | #include /* isspace */ 2 | #include 3 | #include "readline.h" 4 | 5 | int read_line(char str[], int n) { 6 | int c, i = 0; 7 | 8 | while (isspace(c = getchar())) 9 | ; 10 | while (c != '\n' && c != EOF) { 11 | if (i < n) 12 | str[i++] = c; 13 | c = getchar(); 14 | } 15 | str[i] = '\0'; 16 | return i; 17 | } 18 | -------------------------------------------------------------------------------- /16/projects/02/readline.h: -------------------------------------------------------------------------------- 1 | #ifndef READLINE_H 2 | #define READLINE_H 3 | 4 | /* read_line: Skips leading whitespace characters, then reads the remainder of 5 | * the input line and stores it in str. Truncates the line if its 6 | * length exceeds n. Returns the number of characters stores. 7 | */ 8 | int read_line(char str[], int n); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /16/projects/03/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | inventory: inventory.o readline.o quicksort.o 5 | $(CC) -o inventory inventory.o readline.o quicksort.o $(CFLAGS) 6 | 7 | inventory.o: inventory.c inventory.h readline.h quicksort.h 8 | $(CC) -c inventory.c $(CFLAGS) 9 | 10 | readline.o: readline.c readline.h 11 | $(CC) -c readline.c $(CFLAGS) 12 | 13 | quicksort.o: quicksort.c quicksort.h inventory.h 14 | $(CC) -c quicksort.c $(CFLAGS) 15 | -------------------------------------------------------------------------------- /16/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 16.03 2 | 3 | Modify the `inventory.c` program of Section 16.3 by making `inventory` and 4 | `num_parts` local to the `main` function. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /16/projects/03/inventory.h: -------------------------------------------------------------------------------- 1 | #ifndef INVENTORY_H 2 | #define INVENTORY_H 3 | 4 | #define NAME_LEN 25 5 | #define MAX_PARTS 100 6 | 7 | struct part { 8 | int number; 9 | char name[NAME_LEN+1]; 10 | int on_hand; 11 | }; 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /16/projects/03/quicksort.h: -------------------------------------------------------------------------------- 1 | /* quicksort, edited for struct part */ 2 | 3 | #ifndef QUICKSORT_H 4 | #define QUICKSORT_H 5 | 6 | #include "inventory.h" 7 | 8 | void quicksort(struct part a[], int low, int high); 9 | int split(struct part a[], int low, int high); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /16/projects/03/readline.c: -------------------------------------------------------------------------------- 1 | #include /* isspace */ 2 | #include 3 | #include "readline.h" 4 | 5 | int read_line(char str[], int n) { 6 | int c, i = 0; 7 | 8 | while (isspace(c = getchar())) 9 | ; 10 | while (c != '\n' && c != EOF) { 11 | if (i < n) 12 | str[i++] = c; 13 | c = getchar(); 14 | } 15 | str[i] = '\0'; 16 | return i; 17 | } 18 | -------------------------------------------------------------------------------- /16/projects/03/readline.h: -------------------------------------------------------------------------------- 1 | #ifndef READLINE_H 2 | #define READLINE_H 3 | 4 | /* read_line: Skips leading whitespace characters, then reads the remainder of 5 | * the input line and stores it in str. Truncates the line if its 6 | * length exceeds n. Returns the number of characters stores. 7 | */ 8 | int read_line(char str[], int n); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /16/projects/04/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | inventory: inventory.o readline.o quicksort.o 5 | $(CC) -o inventory inventory.o readline.o quicksort.o $(CFLAGS) 6 | 7 | inventory.o: inventory.c inventory.h readline.h quicksort.h 8 | $(CC) -c inventory.c $(CFLAGS) 9 | 10 | readline.o: readline.c readline.h 11 | $(CC) -c readline.c $(CFLAGS) 12 | 13 | quicksort.o: quicksort.c quicksort.h inventory.h 14 | $(CC) -c quicksort.c $(CFLAGS) 15 | -------------------------------------------------------------------------------- /16/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 16.04 2 | 3 | Modify the `inventory.c` program of Section 16.3 by adding a `price` member to 4 | the `part` structure. The `insert` function should ask the user for the price of 5 | a new item. The `search` and `print` functions should display the price. Add a 6 | new command that allows the user to change the price of a part. 7 | 8 | ### Solution 9 | 10 | See program files. 11 | -------------------------------------------------------------------------------- /16/projects/04/inventory.h: -------------------------------------------------------------------------------- 1 | #ifndef INVENTORY_H 2 | #define INVENTORY_H 3 | 4 | #define NAME_LEN 25 5 | #define MAX_PARTS 100 6 | 7 | struct part { 8 | int number; 9 | char name[NAME_LEN+1]; 10 | int on_hand; 11 | double price; 12 | }; 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /16/projects/04/quicksort.h: -------------------------------------------------------------------------------- 1 | /* quicksort, edited for struct part */ 2 | 3 | #ifndef QUICKSORT_H 4 | #define QUICKSORT_H 5 | 6 | #include "inventory.h" 7 | 8 | void quicksort(struct part a[], int low, int high); 9 | int split(struct part a[], int low, int high); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /16/projects/04/readline.c: -------------------------------------------------------------------------------- 1 | #include /* isspace */ 2 | #include 3 | #include "readline.h" 4 | 5 | int read_line(char str[], int n) { 6 | int c, i = 0; 7 | 8 | while (isspace(c = getchar())) 9 | ; 10 | while (c != '\n' && c != EOF) { 11 | if (i < n) 12 | str[i++] = c; 13 | c = getchar(); 14 | } 15 | str[i] = '\0'; 16 | return i; 17 | } 18 | -------------------------------------------------------------------------------- /16/projects/04/readline.h: -------------------------------------------------------------------------------- 1 | #ifndef READLINE_H 2 | #define READLINE_H 3 | 4 | /* read_line: Skips leading whitespace characters, then reads the remainder of 5 | * the input line and stores it in str. Truncates the line if its 6 | * length exceeds n. Returns the number of characters stores. 7 | */ 8 | int read_line(char str[], int n); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /16/projects/05/README.md: -------------------------------------------------------------------------------- 1 | ### Project 16.05 2 | 3 | Modify Programming Project 8 from Chapter 5 so that the times are stored in a 4 | single array. The elements of the array will be structures, each containing a 5 | departure time and the corresponding arrival time (Each time will be an integer, 6 | representing the number of minutes since midnight). The program will use a loop 7 | to search the array for the departure time closest to the time entered by the 8 | user. 9 | 10 | ### Solution 11 | 12 | See `5.c`. 13 | -------------------------------------------------------------------------------- /16/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 16.06 2 | 3 | Modify Programming Project 9 from Chapter 5 so that each date entered by the 4 | user is stored in a `date` structure (see Exercise 5). Incorporate the 5 | `compare_dates` function of Exercise 5 into your program. 6 | 7 | ### Solution 8 | 9 | See `6.c`. 10 | -------------------------------------------------------------------------------- /17/exercises/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 17.06 2 | 3 | Modify the `delete_from_list` function so that is uses only one pointer variable 4 | instead of two (`cur` and `prev`). 5 | 6 | ### Solution 7 | 8 | ```c 9 | struct node *delete_from_list(struct node **list, int n) { 10 | struct node *item = *list; 11 | while (item) { 12 | if (item->value == n) { 13 | *list = item->next; 14 | free(item); 15 | break; 16 | } 17 | list = &item->next; 18 | item = item->next; 19 | } 20 | return *list; 21 | } 22 | ``` 23 | -------------------------------------------------------------------------------- /17/exercises/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 17.09 2 | 3 | True or false: If `x` is a structure and `a` is a member of that structure, then 4 | `(&x)->a` is the same as `x.a`. Justify your answer. 5 | 6 | ### Solution 7 | 8 | True. When expanded, the right arrow operator (`->`), expands to the dereference 9 | operator and the dot operator (`*` and `.`); so, `(&x)->a` expands to `*(&x).a`. 10 | The dereference and address-of operators cancel to produce `x.a`. 11 | -------------------------------------------------------------------------------- /17/exercises/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 17.10 2 | 3 | Modify the `print_part` function of Section 16.2 so that is parameter is a 4 | *pointer* to a `part` structure. Use the `->` operator in your answer. 5 | 6 | ### Solution 7 | 8 | ```c 9 | void print_part(struct part *p) 10 | { 11 | printf("Part number: %d\n", p->number); 12 | printf("Part name: %s\n", p->name); 13 | printf("Quantity on hand: %d\n", p->on_hand); 14 | } 15 | ``` 16 | -------------------------------------------------------------------------------- /17/exercises/16/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 17.16 2 | 3 | Write the following function. The call `sum(g, i, j)` should return `g(i) +` ... 4 | `+ g(j)`. 5 | 6 | ```c 7 | int sum(int (*f)(int), int start, int end); 8 | ``` 9 | 10 | ### Solution 11 | 12 | ```c 13 | int sum(int (*f)(int), int start, int end) { 14 | int result = 0; 15 | while (start <= end) { 16 | result += (*f)(start); 17 | start++; 18 | } 19 | return result; 20 | } 21 | ``` 22 | -------------------------------------------------------------------------------- /17/exercises/17/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 17.17 2 | 3 | Let `a` be an array of 100 integers. Write a call of `qsort` that sorts only the 4 | *last* 50 elements in `a` (You don't need to write the comparison function). 5 | 6 | ### Solution 7 | 8 | ```c 9 | qsort(&a[50], 50, sizeof(a[0]), compare); 10 | ``` 11 | 12 | NB: Assuming `compare` is the name of the comparison function. 13 | -------------------------------------------------------------------------------- /17/exercises/18/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 17.18 2 | 3 | Modify the `compare_parts` function so that parts are sorted with their numbers 4 | in *descending* order. 5 | 6 | ### Solution 7 | 8 | ```c 9 | int compare_parts(const void *p, const void *q) 10 | { 11 | return ((struct part *) q)->number - ((struct part *) p)->number; 12 | } 13 | ``` 14 | -------------------------------------------------------------------------------- /17/projects/01/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | inventory: inventory.o readline.o 5 | $(CC) -o inventory inventory.o readline.o $(CFLAGS) 6 | 7 | inventory.o: inventory.c readline.h 8 | $(CC) -c inventory.c $(CFLAGS) 9 | 10 | readline.o: readline.c readline.h 11 | $(CC) -c readline.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /17/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 17.01 2 | 3 | Modify the `inventory.c` program of Section 16.3 so that the `inventory` array 4 | is allocated dynamically and later reallocated when it fills up. Use `malloc` 5 | initially to allocate enough space for an array of 10 `part` structures. When 6 | the array has no more room for new parts, use `realloc` to double its size. 7 | Repeat the double step each time the array becomes full. 8 | 9 | ### Solution 10 | 11 | See `1.c`. 12 | -------------------------------------------------------------------------------- /17/projects/02/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | inventory: inventory.o readline.o 5 | $(CC) -o inventory inventory.o readline.o $(CFLAGS) 6 | 7 | inventory.o: inventory.c readline.h inventory.h 8 | $(CC) -c inventory.c $(CFLAGS) 9 | 10 | readline.o: readline.c readline.h 11 | $(CC) -c readline.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /17/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 17.01 2 | 3 | Modify the `inventory.c` program of Section 16.3 so that the `p` (print) command 4 | calls `qsort` to sort the `inventory` array before it prints the parts. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /17/projects/02/inventory.h: -------------------------------------------------------------------------------- 1 | #ifndef INVENTORY_H 2 | #define INVENTORY_H 3 | 4 | #define NAME_LEN 25 5 | 6 | struct part { 7 | int number; 8 | char name[NAME_LEN+1]; 9 | int on_hand; 10 | } *inventory; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /17/projects/03/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | inventory2: inventory2.o readline.o 5 | $(CC) -o inventory2 inventory2.o readline.o $(CFLAGS) 6 | 7 | inventory.o: inventory.c readline.h 8 | $(CC) -c inventory.c $(CFLAGS) 9 | 10 | readline.o: readline.c readline.h 11 | $(CC) -c readline.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /17/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 17.03 2 | 3 | Modify the `inventory2.c` program of Section 17.5 by adding an `e` (erase) 4 | command that allows the user to remove a part from the database. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /17/projects/04/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | justify: justify.o line.o word.o 5 | $(CC) -o justify justify.o line.o word.o $(CFLAGS) 6 | 7 | justify.o: justify.c line.h word.h 8 | $(CC) -c justify.c $(CFLAGS) 9 | 10 | line.o: line.c line.h 11 | $(CC) -c line.c $(CFLAGS) 12 | 13 | word.o: word.c word.h 14 | $(CC) -c word.c $(CFLAGS) 15 | -------------------------------------------------------------------------------- /17/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 17.04 2 | 3 | Modify the `justify` program of Section 15.3 by rewriting the `line.c` file so 4 | that it stores the current line in a linked list. Each node in the list will 5 | store a single word. The `line` array will be replaced by a variable that points 6 | to the node containing the first word. This variable will store a null pointer 7 | whenever the line is empty. 8 | 9 | ### Solution 10 | 11 | See program files. 12 | -------------------------------------------------------------------------------- /17/projects/04/newquote.txt: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an enormous success. Although 2 | accidents of history surely helped, it evidently satisfied a 3 | need for a system implementation language efficient enough 4 | to displace assembly language, yet sufficiently abstract and 5 | fluent to describe algorithms and interactions in a wide 6 | variety of environments. 7 | -------------------------------------------------------------------------------- /17/projects/04/quote.txt: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an 2 | enormous success. Although accidents of history 3 | surely helped, it evidently satisfied a need 4 | 5 | for a system implementation language efficient 6 | enough to displace assembly language, 7 | yet sufficiently abstract and fluent to describe 8 | algorithms and interactions in a wide variety 9 | of environments. 10 | -------------------------------------------------------------------------------- /17/projects/05/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | sortwords: sortwords.o 5 | $(CC) -o sortwords sortwords.o $(CFLAGS) 6 | 7 | sortwords.o: sortwords.c 8 | $(CC) -c sortwords.c $(CFLAGS) 9 | -------------------------------------------------------------------------------- /17/projects/06/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | sortwords: sortwords.o 5 | $(CC) -o sortwords sortwords.o $(CFLAGS) 6 | 7 | sortwords.o: sortwords.c 8 | $(CC) -c sortwords.c $(CFLAGS) 9 | -------------------------------------------------------------------------------- /17/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 17.06 2 | 3 | Modify Programming Project 5 so that it uses `qsort` to sort the array of 4 | pointers. 5 | 6 | ### Solution 7 | 8 | See program files (same as Project 17.05). 9 | -------------------------------------------------------------------------------- /17/projects/07/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | remind2: remind2.o 5 | $(CC) -o remind2 remind2.o $(CFLAGS) 6 | 7 | remind2.o: remind2.c 8 | $(CC) -c remind2.c $(CFLAGS) 9 | -------------------------------------------------------------------------------- /17/projects/07/README.md: -------------------------------------------------------------------------------- 1 | ### Project 17.07 2 | 3 | (C99) Modify the `remind2.c` program of Section 17.2 so that each element of the 4 | `reminders` array is a pointer to a `vstring` structure (see Section 17.9) 5 | rather than a pointer to an ordinary string. 6 | 7 | ### Solution 8 | 9 | See program files. 10 | -------------------------------------------------------------------------------- /18/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 18.04 2 | 3 | Let `f` be the following function. What will be tha value of `f(10)` if `f` has 4 | never been called before? What will be the value of `f(10)` if `f` has been 5 | called five times previously? 6 | 7 | ```c 8 | int f(int i) 9 | { 10 | static int j = 0; 11 | return i * j++; 12 | } 13 | ``` 14 | 15 | ### Solution 16 | 17 | If `f` has never been called before, `f(10)` will return `0`. If `f` has been 18 | called five times before, `f(10)` will return `50`. 19 | -------------------------------------------------------------------------------- /18/exercises/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 18.07 2 | 3 | Suppose that we declare `x` to be a `const` object. Which one of the following 4 | statements about `x` is *false*? 5 | 6 | (a) If `x` is of type `int`, it can be used as the value of a case label in a 7 | `switch` statement. 8 | (b) The compiler will check that no assignment is made to `x`. 9 | (c) `x` is subject to the same scope rules as variables. 10 | (d) `x` can be of any type. 11 | 12 | ### Solution 13 | 14 | (a) is false because constant variables cannot be used in constant expressions. 15 | All other statements are true. 16 | -------------------------------------------------------------------------------- /18/exercises/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 18.13 2 | 3 | Which of the following declarations are legal? (Assume that `PI` is a macro that 4 | represents 3.14159.) 5 | 6 | (a) `char c = 65;` 7 | (b) `static int i = 5, j = i * i;` 8 | (c) `double d = 2 * PI;` 9 | (d) `double angles[] = {0, PI / 2, PI, 3 * PI / 2};` 10 | 11 | ### Solution 12 | 13 | (a), (c) and (d) are legal declarations. (b) is not legal because a declaration 14 | with static storage duration requires a constant expression. 15 | -------------------------------------------------------------------------------- /18/exercises/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 18.14 2 | 3 | Which kind of variables cannot be initialized? 4 | 5 | (a) Array variables 6 | (b) Enumeration variables 7 | (c) Structure variables 8 | (d) Union variables 9 | (e) None of the above 10 | 11 | ### Solution 12 | 13 | (e). All variables can be initialized. 14 | -------------------------------------------------------------------------------- /18/exercises/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 18.15 2 | 3 | Which property of a variables determines whether or not it has a default initial 4 | value? 5 | 6 | (a) Storage duration 7 | (b) Scope 8 | (c) Linkage 9 | (d) Type 10 | 11 | ### Solution 12 | 13 | (a). A variable with static storage duration will be defaulted to zero upon 14 | declaration. 15 | -------------------------------------------------------------------------------- /19/exercises/01/queue.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_H 2 | #define QUEUE_H 3 | 4 | #include /* C99+ only */ 5 | 6 | typedef int Item; 7 | typedef struct queue_t *Queue; 8 | 9 | void queue_append(Queue, Item i); 10 | void queue_remove_head(Queue); 11 | Item queue_head(Queue); 12 | Item queue_last(Queue); 13 | bool queue_is_empty(Queue); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /19/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 19.02 2 | 3 | Modify the `stack2.c` file to use the `PUBLIC` and `PRIVATE` macros. 4 | 5 | ### Solution 6 | 7 | See `stack2.c`. 8 | -------------------------------------------------------------------------------- /19/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 19.04 2 | 3 | (a) Write an implementation of the `Stack` type, assuming that `Stack` is a 4 | structure containing a fixed-length array. 5 | 6 | (b) Redo the `Stack` type, this time using a linked-list representation instead 7 | of an array. (Show both `stack.h` and `stack.c`) 8 | 9 | ### Solution 10 | 11 | #### (a) 12 | 13 | See `stack.c` and `stack.h`. 14 | 15 | #### (b) 16 | 17 | See `stack2.c` and `stack.h`. 18 | -------------------------------------------------------------------------------- /19/exercises/04/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_H 2 | #define STACK_H 3 | 4 | #include /* C99+ only */ 5 | #define STACK_SIZE 100 6 | 7 | typedef struct *Stack; 8 | typedef int Item; 9 | 10 | Stack create(void); 11 | void destroy(Stack s); 12 | void make_empty(Stack s); 13 | bool is_empty(const Stack s); 14 | bool is_full(const Stack s); 15 | void push(Stack s, Item i); 16 | Item pop(Stack s); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /19/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 19.05 2 | 3 | Modify the `queue.h` header of Exercise 1 so that it defines a `Queue` type, 4 | where `Queue` is a structure containing a fixed-length array (see Exercise 5 | 3(a)). Modify the functions in `queue.h` to take a `Queue *` parameter. 6 | 7 | ### Solution 8 | 9 | See `queue.h`. 10 | -------------------------------------------------------------------------------- /19/exercises/05/queue.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_H 2 | #define QUEUE_H 3 | 4 | #include /* C99+ only */ 5 | 6 | /* 7 | #define QUEUE_SIZE 100 8 | */ 9 | typedef int Item; 10 | /* 11 | struct queue_t { 12 | contents[QUEUE_SIZE]; 13 | } 14 | */ 15 | typedef struct queue_t *Queue; 16 | 17 | void queue_append(Queue, Item i); 18 | void queue_remove_head(Queue); 19 | Item queue_head(Queue); 20 | Item queue_last(Queue); 21 | bool queue_is_empty(Queue); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /19/exercises/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 19.06 2 | 3 | (a) Add a `peek` function to `stackADT.c`. This function will have a parameter 4 | of type `Stack`. When called, it returns the top item on the stack but doesn't 5 | modify the stack. 6 | 7 | (b) Repeat part (a), modifying `stackADT2.c` this time. 8 | (c) Repeat part (a), modifying `stackADT3.c` this time. 9 | 10 | ### Solution 11 | 12 | #### (a) 13 | 14 | See `stackADT.c`. 15 | 16 | #### (b) 17 | 18 | See `stackADT2.c`. 19 | 20 | #### (c) 21 | 22 | See `stackADT3.c`. 23 | -------------------------------------------------------------------------------- /19/exercises/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 19.07 2 | 3 | Modify `stackADT2.c` so that a stack automatically doubles in size when it 4 | becomes full. Have the `push` function dynamically allocate a new array that's 5 | twice as large as the old one and then copy the stack contents from the old 6 | array to the new one. Be sure to have `push` deallocate the old array once the 7 | data has been copied. 8 | 9 | ### Solution 10 | 11 | See `stackADT2.c`. 12 | -------------------------------------------------------------------------------- /19/projects/01/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | stackclient: stackclient.o stack.o 5 | $(CC) -o stackclient stackclient.o stack.o $(CFLAGS) 6 | 7 | stackclient.o: stackclient.c stack.h 8 | $(CC) -c stackclient.c $(CFLAGS) 9 | 10 | stack.o: stack.c stack.h 11 | $(CC) -c stack.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /19/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 19.01 2 | 3 | Modify Programming Project 1 from Chapter 10 so that it uses the stack ADT 4 | described in Section 19.4. You may use any of the implementations of the ADT 5 | described in that section. 6 | 7 | ### Solution 8 | 9 | See program files. 10 | -------------------------------------------------------------------------------- /19/projects/01/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_H 2 | #define STACK_H 3 | 4 | #include 5 | 6 | typedef struct stack_type *Stack; 7 | typedef int Item; 8 | 9 | Stack create(); 10 | void destroy(Stack s); 11 | void make_empty(Stack s); 12 | bool is_empty(const Stack s); 13 | bool is_full(const Stack s); 14 | void push(Stack s, Item i); 15 | Item pop(Stack s); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /19/projects/02/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | stackclient: stackclient.o stack.o 5 | $(CC) -o stackclient stackclient.o stack.o $(CFLAGS) 6 | 7 | stackclient.o: stackclient.c stack.h 8 | $(CC) -c stackclient.c $(CFLAGS) 9 | 10 | stack.o: stack.c stack.h 11 | $(CC) -c stack.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /19/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 19.02 2 | 3 | Modify Programming Project 6 from Chapter 10 so that it uses the stack ADT 4 | described in Section 19.4. You may use any of the implementations of the ADT 5 | described in that section. 6 | 7 | ### Solution 8 | 9 | See program files. 10 | -------------------------------------------------------------------------------- /19/projects/02/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_H 2 | #define STACK_H 3 | 4 | #include 5 | 6 | typedef struct stack_type *Stack; 7 | typedef char Item; 8 | 9 | Stack create(); 10 | void destroy(Stack s); 11 | void make_empty(Stack s); 12 | bool is_empty(const Stack s); 13 | bool is_full(const Stack s); 14 | void push(Stack s, Item i); 15 | Item pop(Stack s); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /19/projects/03/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | stackclient: stackclient.o stackADT3.o 5 | $(CC) -o stackclient stackclient.o stackADT3.o $(CFLAGS) 6 | 7 | stackclient.o: stackclient.c stackADT.h 8 | $(CC) -c stackclient.c $(CFLAGS) 9 | 10 | stackADT3.o: stackADT3.c stackADT.h 11 | $(CC) -c stackADT3.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /19/projects/04/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | stackclient: stackclient.o stackADT3.o 5 | $(CC) -o stackclient stackclient.o stackADT3.o $(CFLAGS) 6 | 7 | stackclient.o: stackclient.c stackADT.h 8 | $(CC) -c stackclient.c $(CFLAGS) 9 | 10 | stackADT3.o: stackADT3.c stackADT.h 11 | $(CC) -c stackADT3.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /19/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 19.04 2 | 3 | Modify the `stackADT.h` and `stackADT3.c` files of Section 19.4 so that a stack 4 | stores values of type `void *`, as described in Section 19.5: the `Item` type 5 | will no longer be used. Modify `stackclient.c` so that it stores pointers to 6 | strings in the `s1` and `s2` stacks. 7 | 8 | ### Solution 9 | 10 | See program files. 11 | -------------------------------------------------------------------------------- /19/projects/05/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | queueclient: queueclient.o queueADT.o 5 | $(CC) -o queueclient queueclient.o queueADT.o $(CFLAGS) 6 | 7 | queueclient.o: queueclient.c queue.h 8 | $(CC) -c queueclient.c $(CFLAGS) 9 | 10 | queueADT.o: queueADT.c queue.h 11 | $(CC) -c queueADT.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /19/projects/05/queue.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_H 2 | #define QUEUE_H 3 | 4 | #include /* C99+ only */ 5 | 6 | typedef int Item; 7 | typedef struct queue_t *Queue; 8 | 9 | Queue queue_create(void); 10 | void queue_destroy(Queue q); 11 | void queue_append(Queue, Item i); 12 | void queue_remove_head(Queue q); 13 | Item queue_head(Queue q); 14 | Item queue_last(Queue q); 15 | bool queue_is_empty(Queue q); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /19/projects/06/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | queueclient: queueclient.o queueADT.o 5 | $(CC) -o queueclient queueclient.o queueADT.o $(CFLAGS) 6 | 7 | queueclient.o: queueclient.c queue.h 8 | $(CC) -c queueclient.c $(CFLAGS) 9 | 10 | queueADT.o: queueADT.c queue.h 11 | $(CC) -c queueADT.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /19/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 19.06 2 | 3 | Modify Programming Project 5 so that the items in a queue are stored in a 4 | dynamically allocated array whose length is passed to the `create` function. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /19/projects/06/queue.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_H 2 | #define QUEUE_H 3 | 4 | #include /* C99+ only */ 5 | 6 | typedef int Item; 7 | typedef struct queue_t *Queue; 8 | 9 | Queue queue_create(int size); 10 | void queue_destroy(Queue q); 11 | void queue_append(Queue, Item i); 12 | void queue_remove_head(Queue q); 13 | Item queue_head(Queue q); 14 | Item queue_last(Queue q); 15 | bool queue_is_empty(Queue q); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /19/projects/07/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | queueclient: queueclient.o queueADT.o 5 | $(CC) -o queueclient queueclient.o queueADT.o $(CFLAGS) 6 | 7 | queueclient.o: queueclient.c queue.h 8 | $(CC) -c queueclient.c $(CFLAGS) 9 | 10 | queueADT.o: queueADT.c queue.h 11 | $(CC) -c queueADT.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /19/projects/07/README.md: -------------------------------------------------------------------------------- 1 | ### Project 19.06 2 | 3 | Modify Programming Project 5 so that the items in a queue are stored in a linked 4 | list (see Exercise 3(b)). 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /19/projects/07/queue.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_H 2 | #define QUEUE_H 3 | 4 | #include /* C99+ only */ 5 | 6 | typedef int Item; 7 | typedef struct queue_t *Queue; 8 | 9 | Queue queue_create(void); 10 | void queue_destroy(Queue q); 11 | void queue_append(Queue, Item i); 12 | void queue_remove_head(Queue q); 13 | Item queue_head(Queue q); 14 | Item queue_last(Queue q); 15 | bool queue_is_empty(Queue q); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /20/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 20.02 2 | 3 | Describe a simple way to "toggle" a bit (change it from 0 to 1 or from 1 to 0). 4 | Illustrate the technique by writing a statement that toggles bit 4 of the 5 | variable `i`. 6 | 7 | ### Solution 8 | 9 | Use the exclusive-or operator with a mask of the bit you want to toggle. For 10 | example, if we want to toggle the 4th bit on the right of the variable `i`, this 11 | is the complete process: 12 | 13 | ```c 14 | i ^= 1 << 4; 15 | ``` 16 | -------------------------------------------------------------------------------- /20/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 20.03 2 | 3 | Explain what effect the following macro has on its arguments. You may assume 4 | that the arguments have the same type. 5 | 6 | ```c 7 | #define M(x,y) ((x)^=(y),(y)^=(x),(x)^=(y)) 8 | ``` 9 | 10 | ### Solution 11 | 12 | The macro swaps the values of `x` and `y`. 13 | -------------------------------------------------------------------------------- /20/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 20.05 2 | 3 | Write macros named `GET_RED`, `GET_GREEN` and `GET_BLUE` that, when given a 4 | color as an argument (see Exercise 4), return its 8-bit red, green and blue 5 | intensities. 6 | 7 | ### Solution 8 | 9 | ```c 10 | #define GET_RED(x) ((x) & 0xff) 11 | #define GET_GREEN(x) ((x) >> 8 & 0xff) 12 | #define GET_BLUE(x) ((x) >> 16 & 0xff) 13 | ``` 14 | -------------------------------------------------------------------------------- /20/exercises/06/swap_bytes.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned short swap_bytes(unsigned short i); 4 | 5 | int main(void) 6 | { 7 | unsigned short i; 8 | printf("Enter a hexadecimal number (up to four digits): "); 9 | scanf("%hx", &i); 10 | printf("Number with bytes swapped: %.4hx\n", swap_bytes(i)); 11 | return 0; 12 | } 13 | 14 | unsigned short swap_bytes(unsigned short i) 15 | { 16 | return (i << 8) | (i >> 8); 17 | } 18 | -------------------------------------------------------------------------------- /20/exercises/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 20.09 2 | 3 | (a) Write the following function: 4 | 5 | ```c 6 | int count_ones(unsigned char ch); 7 | ``` 8 | 9 | `count_ones` should return the number of 1 bits in `ch`. 10 | 11 | (b) Write the function in part (a) without using a loop. 12 | 13 | ### Solution 14 | 15 | ```c 16 | int count_ones(unsigned char ch) 17 | { 18 | if (ch == 0) 19 | return 0; 20 | return count_ones(ch & ch - 1) + 1; 21 | } 22 | ``` 23 | -------------------------------------------------------------------------------- /20/exercises/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 20.13 2 | 3 | If `n` is an `unsigned int` variable, what effect does the following statement 4 | have on the bits in `n`? 5 | 6 | ```c 7 | n &= n - 1; 8 | ``` 9 | 10 | *Hint:* Consider the efect on `n` if this statement is executed more than once. 11 | 12 | ### Solution 13 | 14 | The statement removes the rightmost `1`-bit from `n`. Each time the statement is 15 | executed, `n`'s rightmost bit that is set to 1 is removed. 16 | -------------------------------------------------------------------------------- /20/projects/01/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | union { 4 | float value; 5 | struct float_value { 6 | unsigned int fraction: 23; 7 | unsigned int exponent: 8; 8 | unsigned int sign: 1; 9 | } field; 10 | } f; 11 | 12 | int main(void) 13 | { 14 | f.field.sign = 1; 15 | f.field.exponent = 128; 16 | f.field.fraction = 0; 17 | 18 | printf("%.1f\n", f.value); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /20/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 20.01 2 | 3 | Design a union that makes it possible to view a 32-bit value as either a `float` 4 | or the structure described in Exercise 14. Write a program that stores 1 in the 5 | structure's sign field, 128 in the exponent field and 0 in the fraction field, 6 | then prints the `float` value stored in the union (The answer should be -2.0 if 7 | you've set up the bit-fields correctly). 8 | 9 | ### Solution 10 | 11 | See `1.c`. 12 | -------------------------------------------------------------------------------- /21/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 21.03 2 | 3 | When a macro hides a function, which must come first in the header file: the 4 | macro definition or the function prototype? Justify your answer. 5 | 6 | ### Solution 7 | 8 | For the macro (i.e. the preprocessor) to be aware of the function it is hiding, 9 | the function prototype must come before the macro definition. 10 | -------------------------------------------------------------------------------- /21/projects/01/1.c: -------------------------------------------------------------------------------- 1 | #include /* offsetof */ 2 | #include 3 | 4 | struct s { 5 | char a; 6 | int b[2]; 7 | float c; 8 | } s1; 9 | 10 | int main(void) 11 | { 12 | printf("Size of struct s: %d\n", sizeof(struct s)); 13 | printf("Size and offset of member a: %d -- %d\n", 14 | sizeof(s1.a), offsetof(struct s, a)); 15 | printf("Size and offset of member b: %d -- %d\n", 16 | sizeof(s1.b), offsetof(struct s, b)); 17 | printf("Size and offset of member c: %d -- %d\n", 18 | sizeof(s1.c), offsetof(struct s, c)); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /22/exercises/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 22.01 2 | 3 | Indicate whether each of the following files is more likely to contain text data 4 | or binary data: 5 | 6 | (a) A file of object code produced by a C compiler 7 | (b) A program listing produced by a C compiler 8 | (c) An email message sent from one computer to another 9 | (d) A file containing a graphics image 10 | 11 | ### Solution 12 | 13 | (a) Binary Data 14 | (b) Text Data 15 | (c) Text Data 16 | (d) Binary Data 17 | -------------------------------------------------------------------------------- /22/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 22.04 2 | 3 | Show how each of the following numbers will look if displayed by `printf` with 4 | `%#012.5g` as the conversion specification: 5 | 6 | (a) 83.7361 7 | (b) 29748.6607 8 | (c) 1054932234.0 9 | (d) 0.0000235218 10 | 11 | ### Solution 12 | 13 | (a) `00000083.736` 14 | (b) `00000029749.` 15 | (c) `001.0549e+09` 16 | (d) `002.3522e-05` 17 | -------------------------------------------------------------------------------- /22/exercises/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 22.06 2 | 3 | Write a call of `printf` that prints 4 | 5 | ``` 6 | 1 widget 7 | ``` 8 | 9 | if the `widget` variable (of type `int`) has the value 1, and 10 | 11 | *n* `widgets` 12 | 13 | otherwise, where *n* is the value of `widget`. You are not allowed to use the 14 | `if` statement or any other statement; the answer must be a single call of 15 | `printf`. 16 | 17 | ### Solution 18 | 19 | ```c 20 | printf("%d widget%s", widget, widget != 1 ? "s\n" : "\n"); 21 | ``` 22 | -------------------------------------------------------------------------------- /22/exercises/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 22.09 2 | 3 | Which one of the following calls is *not* a valid way of reading one character 4 | from the standard input stream? 5 | 6 | (a) `getch()` 7 | (b) `getchar()` 8 | (c) `getc(stdin)` 9 | (d) `fgetc(stdin)` 10 | 11 | ### Solution 12 | 13 | (a) `getch()` is not a function in the standard library. 14 | -------------------------------------------------------------------------------- /22/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 22.01 2 | 3 | Extend the `canopen.c` program of Section 22.2 so that the user may put any 4 | number of file names on the command line: 5 | 6 | ``` 7 | canopen foo bar baz 8 | ``` 9 | 10 | The program should print a separate `can be opened` or `can't be opened` message 11 | for each file. Have the program terminate withe status `EXIT_FAILURE` if one or 12 | more of the files can't be opened. 13 | 14 | ### Solution 15 | 16 | See `1.c`. 17 | -------------------------------------------------------------------------------- /22/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 22.02 2 | 3 | Write a program that converts all letters in a file to upper case (Characters 4 | other than letters shouldn't be changed). The program should obtain the file 5 | name from the command line and write its output to `stdout`. 6 | 7 | ### Solution 8 | 9 | See `2.c`. 10 | -------------------------------------------------------------------------------- /22/projects/03/cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zydchina818/C-/c858e108f04d788be752353ed578c4d09328ef15/22/projects/03/cover.jpg -------------------------------------------------------------------------------- /22/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 22.04 2 | 3 | (a) Write a program that counts the number of characters in a text file. 4 | (b) Write a program that counts the number of words in a text file (A "word" is 5 | any sequence of non-white-space characters). 6 | (c) Write a program that counts the number of lines in a text file. 7 | 8 | Have each program obtain the file name from the command line. 9 | 10 | ### Solution 11 | 12 | See `fchar.c`, `fword.c` and `fline.c`. 13 | -------------------------------------------------------------------------------- /22/projects/05/README.md: -------------------------------------------------------------------------------- 1 | ### Project 22.05 2 | 3 | The `xor.c` program of Section 20.1 refuses to encrypt bytes that -- in original 4 | or encrypted form -- are control characters. We can now remove this restriction. 5 | Modify the program so that the names of the input and output files are 6 | command-line arguments. Open both files in binary mode, and remove the test that 7 | checks whether the original and encrypted characters are printing characters. 8 | 9 | ### Solution 10 | 11 | See `xor.c`. 12 | -------------------------------------------------------------------------------- /22/projects/08/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | inventory: inventory.o readline.o 5 | $(CC) -o inventory inventory.o readline.o $(CFLAGS) 6 | 7 | inventory.o: inventory.c readline.h 8 | $(CC) -c inventory.c $(CFLAGS) 9 | 10 | readline.o: readline.c readline.h 11 | $(CC) -c readline.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /22/projects/10/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wpedantic -Wextra -std=c99 3 | 4 | inventory2: inventory2.o readline.o 5 | $(CC) -o inventory2 inventory2.o readline.o $(CFLAGS) 6 | 7 | inventory2.o: inventory2.c readline.h 8 | $(CC) -c inventory2.c $(CFLAGS) 9 | 10 | readline.o: readline.c readline.h 11 | $(CC) -c readline.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /22/projects/11/README.md: -------------------------------------------------------------------------------- 1 | ### Project 22.11 2 | 3 | Write a program that reads a date from the command line and displays it in the 4 | following form: 5 | 6 | ``` 7 | September 13, 2010 8 | ``` 9 | 10 | Allow the user to enter the date as either `9-13-2010` or `9/13/2010`; you may 11 | assume that there are no spaces in the date. Print an error message if the date 12 | doesn't have one of the specified forms. *Hint:* Use `sscanf` to extract the 13 | month, day and year from the command-line argument. 14 | 15 | ### Solution 16 | 17 | See `11.c`. 18 | -------------------------------------------------------------------------------- /22/projects/12/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | items: items.o readline.o 5 | $(CC) -o items items.o readline.o $(CFLAGS) 6 | 7 | items.o: items.c readline.h 8 | $(CC) -c items.c $(CFLAGS) 9 | 10 | readline.o: readline.c readline.h 11 | $(CC) -c readline.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /22/projects/13/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | flighttimes: flighttimes.o readline.o 5 | $(CC) -o flighttimes flighttimes.o readline.o $(CFLAGS) 6 | 7 | flighttimes.o: flighttimes.c readline.h 8 | $(CC) -c flighttimes.c $(CFLAGS) 9 | 10 | readline.o: readline.c readline.h 11 | $(CC) -c readline.c $(CFLAGS) 12 | -------------------------------------------------------------------------------- /22/projects/13/flights.dat: -------------------------------------------------------------------------------- 1 | 8:00 10:16 2 | 9:43 11:52 3 | 11:19 13:31 4 | 12:47 15:00 5 | 14:00 16:08 6 | 15:45 17:55 7 | 19:00 12:20 8 | 21:45 23:58 9 | -------------------------------------------------------------------------------- /22/projects/15/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 3 | 4 | justify: justify.o line.o word.o 5 | $(CC) -o justify justify.o line.o word.o $(CFLAGS) 6 | 7 | justify.o: justify.c line.h word.h 8 | $(CC) -c justify.c $(CFLAGS) 9 | 10 | line.o: line.c line.h 11 | $(CC) -c line.c $(CFLAGS) 12 | 13 | word.o: word.c word.h 14 | $(CC) -c word.c $(CFLAGS) 15 | -------------------------------------------------------------------------------- /22/projects/15/README.md: -------------------------------------------------------------------------------- 1 | ### Project 22.15 2 | 3 | Modify the `justify` program of Section 15.3 so that it reads from one text file 4 | and writes to another. Have the program obtain the names of both files from the 5 | command line. 6 | 7 | ### Solution 8 | 9 | See program files. 10 | -------------------------------------------------------------------------------- /22/projects/15/outquote.txt: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an enormous success. Although 2 | accidents of history surely helped, it evidently satisfied a 3 | need for a system implementation language efficient enough 4 | to displace assembly language, yet sufficiently abstract and 5 | fluent to describe algorithms and interactions in a wide 6 | variety of environments. -------------------------------------------------------------------------------- /22/projects/15/quote.txt: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an 2 | enormous success. Although accidents of history 3 | surely helped, it evidently satisfied a need 4 | 5 | for a system implementation language efficient 6 | enough to displace assembly language, 7 | yet sufficiently abstract and fluent to describe 8 | algorithms and interactions in a wide variety 9 | of environments. 10 | -------------------------------------------------------------------------------- /22/projects/16/README.md: -------------------------------------------------------------------------------- 1 | ### Project 22.16 2 | 3 | Modify the `fcopy.c` program of Section 22.4 so that is uses `fread` and 4 | `fwrite` to copy the file in blocks of 512 bytes (The last block may contain 5 | fewer than 512 bytes, of course). 6 | 7 | ### Solution 8 | 9 | See `fcopy.c`. 10 | -------------------------------------------------------------------------------- /22/projects/17/input.txt: -------------------------------------------------------------------------------- 1 | 404.817.6900 2 | (215) 686-1776 3 | 312-746-6000 4 | 877 275 5273 5 | 6173434200 6 | -------------------------------------------------------------------------------- /22/projects/19/README.md: -------------------------------------------------------------------------------- 1 | ### Project 22.19 2 | 3 | (a) Write a program that converts a Windows text file to a UNIX text file (See 4 | Section 22.1 for a discussion of the differences between Windows and UNIX text 5 | files). 6 | 7 | (b) Write a program that converts a UNIX text file to a Windows text file. 8 | 9 | In each case, have the program obtain the names of both files from the command 10 | line. *Hint:* Open the input file in `"rb"` mode and the output file in`"wb"` 11 | mode. 12 | 13 | ### Solution 14 | 15 | See `win2unix.c` and `unix2win.c`. 16 | -------------------------------------------------------------------------------- /23/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 23.03 2 | 3 | (C99) Check the documentation for your compiler to see if it performs 4 | contraction on arithmetic expressions and, if so, under what circumstances. 5 | 6 | ### Solution 7 | 8 | With GCC, when the `FP_CONTRACT` does not disallow floating point expression 9 | contraction, the flags `-ffc-contract=fast`, `-funsafe-math-optimizations` or 10 | `-ffast-math` must be used. 11 | -------------------------------------------------------------------------------- /23/exercises/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 23.04 2 | 3 | Using `isalpha` and `isalnum`, write a function that checks whether a string has 4 | the syntax of a C identifier (it consists of letters, digits and underscores, 5 | with a letter or underscore at the beginning). 6 | 7 | ### Solution 8 | 9 | ```c 10 | bool is_identifier(char *s) 11 | { 12 | if (!isalpha(*s) && (*s != '_')) 13 | return false; 14 | s++; 15 | while (*s) 16 | { 17 | if (!isalnum(*s) && (*s != '_')) 18 | return false; 19 | s++; 20 | } 21 | return true; 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /23/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 23.05 2 | 3 | Using `isxdigit`, write a function that checks whether a string represents a 4 | valid hexadecimal number (it consists solely of hexadecimal digits). If so, the 5 | function returns the value of the number as a `long int`. Otherwise, the 6 | function returns -1. 7 | 8 | ### Solution 9 | 10 | ```c 11 | long int hexstringtolong(char *s) 12 | { 13 | while (*s) 14 | { 15 | if (!isxdigit(*s)) 16 | return -1; 17 | s++; 18 | } 19 | return strtol(s, NULL, 16); 20 | } 21 | ``` 22 | -------------------------------------------------------------------------------- /23/exercises/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 23.08 2 | 3 | Use `strchr` to write the following function: 4 | 5 | ```c 6 | int numchar(const char *s, char ch); 7 | ``` 8 | 9 | `numchar` returns the number of times each character `ch` occurs in the string 10 | `s`. 11 | 12 | ### Solution 13 | 14 | ```c 15 | int numchar(const char *s, char ch) 16 | { 17 | int count = 0; 18 | s = strchr(s, ch); 19 | while (s != NULL) 20 | { 21 | count++; 22 | s = strchr(s + 1, ch); 23 | } 24 | return count; 25 | } 26 | ``` 27 | -------------------------------------------------------------------------------- /23/exercises/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 23.09 2 | 3 | Replace the test condition in the following `if` statement by a single call of 4 | `strchr`: 5 | 6 | 7 | `if (ch == 'a' || ch == 'b' || ch == 'c')` ... 8 | 9 | ### Solution 10 | 11 | ```c 12 | if (strchr("abc", ch) != NULL) 13 | { 14 | /* ... */ 15 | } 16 | ``` 17 | -------------------------------------------------------------------------------- /23/exercises/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 23.11 2 | 3 | Write a call of `memset` that replaces the last `n` characters in a 4 | null-ternimated string `s` with `!` characters. 5 | 6 | ### Solution 7 | 8 | ```c 9 | memset(&s[strlen(s)-n], '!', n); 10 | ``` 11 | -------------------------------------------------------------------------------- /23/projects/01/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 -lm 3 | 4 | 1: 1.o 5 | $(CC) -o 1 1.o $(CFLAGS) 6 | 7 | 1.o: 1.c 8 | $(CC) -c 1.c $(CFLAGS) 9 | -------------------------------------------------------------------------------- /23/projects/01/quadratic-formula.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zydchina818/C-/c858e108f04d788be752353ed578c4d09328ef15/23/projects/01/quadratic-formula.png -------------------------------------------------------------------------------- /23/projects/02/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include /* isspace */ 3 | #include /* C99+ */ 4 | 5 | int main(void) 6 | { 7 | bool ready_to_copy = false; 8 | char c; 9 | 10 | while ((c = getchar()) != EOF) 11 | { 12 | if (ready_to_copy) 13 | putchar(c); 14 | else if (!isspace(c)) 15 | { 16 | ready_to_copy = true; 17 | putchar(c); 18 | } 19 | if (c == '\n') 20 | ready_to_copy = false; 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /23/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 23.02 2 | 3 | Write a program that copies a text file from standard input to standard output, 4 | removing all white-space characters from the beginning of each line. A line 5 | consisting entirely of white-space characters will not be copied. 6 | 7 | ### Solution 8 | 9 | See `2.c`. 10 | -------------------------------------------------------------------------------- /23/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 23.03 2 | 3 | Write a program that copies a text file from standard input to standard output, 4 | capitalizing the first letter in each word. 5 | 6 | ### Solution 7 | 8 | See `3.c`. 9 | -------------------------------------------------------------------------------- /23/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 23.04 2 | 3 | Write a program that prompts the user to enter a series of words separated by 4 | single spaces, then prints the words in reverse order. Read the input as a 5 | string, and then use `strtok` to break it into words. 6 | 7 | ### Solution 8 | 9 | See `4.c`. 10 | -------------------------------------------------------------------------------- /23/projects/05/5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include /* exp */ 3 | 4 | int main(void) 5 | { 6 | double a, p, r; 7 | int t; 8 | 9 | printf("Enter amount deposited: "); 10 | scanf("%lf", &p); 11 | printf("Enter interest rate: "); 12 | scanf("%lf", &r); 13 | printf("Enter number of years: "); 14 | scanf("%d", &t); 15 | 16 | r /= 100.0; 17 | a = p * exp(r * t); 18 | 19 | printf("Total compounded: $ %.2lf\n", a); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /23/projects/05/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 -lm 3 | 4 | 5: 5.o 5 | $(CC) -o 5 5.o $(CFLAGS) 6 | 7 | 5.o: 5.c 8 | $(CC) -c 5.c $(CFLAGS) 9 | -------------------------------------------------------------------------------- /23/projects/06/6.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include /* strchr */ 3 | 4 | int main(void) 5 | { 6 | char c; 7 | 8 | while ((c = getchar()) != EOF) 9 | { 10 | if (strchr("\a\b\f\r\t\v", c) != NULL) 11 | putchar('?'); 12 | else 13 | putchar(c); 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /23/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 23.06 2 | 3 | Write a program that copies a text file from standard input to standard output, 4 | replacing each control character (other than `\n`) by a question mark. 5 | 6 | ### Solution 7 | 8 | See `6.c`. 9 | -------------------------------------------------------------------------------- /23/projects/07/7.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include /* strchr */ 3 | #include /* isspace */ 4 | 5 | int main(void) 6 | { 7 | char c, d = '\0'; 8 | int count; 9 | 10 | while ((c = getchar()) != EOF) 11 | { 12 | if (strchr(".?!", d) != NULL && isspace(c)) 13 | count++; 14 | d = c; 15 | } 16 | 17 | printf("Number of sentences: %d\n", count); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /23/projects/07/README.md: -------------------------------------------------------------------------------- 1 | ### Project 23.07 2 | 3 | Write a program that counts the number of sentences in a text file (obtained 4 | from standard input). Assume that each sentence ends with a `.`, `?` or `!` 5 | follwed by a white-space character (including `\n`). 6 | 7 | ### Solution 8 | 9 | See `7.c`. 10 | -------------------------------------------------------------------------------- /24/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 24.02 2 | 3 | Write a call of `assert` that causes a program to terminate if a variable named 4 | `top` has the value `NULL`. 5 | 6 | ### Solution 7 | 8 | ```c 9 | assert(top != NULL); 10 | ``` 11 | -------------------------------------------------------------------------------- /24/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 24.03 2 | 3 | Modify the `stackADT2.c` file of Section 19.4 so that it uses an `assert` to 4 | test for errors instead of using `if` statements (Note that the `terminate` 5 | function is no longer necessary and can be removed). 6 | 7 | ### Solution 8 | 9 | See `stackADT2.c`. 10 | -------------------------------------------------------------------------------- /25/exercises/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 25.03 2 | 3 | One of the useful properties of UTF-8 is that no sequence of bytes within a 4 | multibyte character can possibly represent another valid multibyte character. 5 | Does the Shift-JIS encoding for *kanji* (discusses in Exercise 2) have this 6 | property? 7 | 8 | ### Solution 9 | 10 | No. Leaving out the first byte in a multibyte character in Shift-JIS encoding 11 | could print an entirely different character, for example `\x81\x40` is a valid 12 | multibyte character, while `\x40` itself maps to the symbol `@`. 13 | -------------------------------------------------------------------------------- /25/exercises/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 25.05 2 | 3 | Repeat Exercise 4, this time using the UTF-8 multibyte encoding. For example, 4 | the phrase *déjà vu* could be represented by the string `"d\xc3\xa9j\xc3\xa0 5 | vu"`. 6 | 7 | ### Solution 8 | 9 | (a) `C\xc3\xb4te d'Azur` 10 | (b) `cr\xc3\xa8me br\xc3\xbbl\xc3\xa9e` 11 | (c) `cr\xc3\xa8me fra\xc3\xaeche` 12 | (d) `Fahrvergn\xc3\xbcgen` 13 | (e) `t\xc3\xaate-\xc3\xa0-t\xc3\xaate` 14 | -------------------------------------------------------------------------------- /25/exercises/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 25.07 2 | 3 | (C99) Modify the program fragment in Exercise 6 by replacing as many tokens as 4 | possible by digraphs and macros defined in ``. 5 | 6 | ### Solution 7 | 8 | ```c 9 | while ((orig_char = getchar()) not_eq EOF) <% 10 | new_char = orig_char xor KEY; 11 | if (isprint(orig_char) and isprint(new_char)) 12 | putchar(new_char); 13 | else 14 | putchar(orig_char); 15 | %> 16 | ``` 17 | -------------------------------------------------------------------------------- /25/projects/01/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *clocale = setlocale(LC_ALL, NULL); 8 | char *locale = setlocale(LC_ALL, ""); 9 | if (strcmp(clocale, locale) == 0) 10 | printf("Current locale is same as C locale\n"); 11 | else 12 | printf("Current locale differs from C locale\n"); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /25/projects/01/README.md: -------------------------------------------------------------------------------- 1 | ### Project 25.01 2 | 3 | Write a program that tests whether your compiler's `""` (native) locale is the 4 | same as its `"C"` locale. 5 | 6 | ### Solution 7 | 8 | See `1.c`. 9 | -------------------------------------------------------------------------------- /26/exercises/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 26.07 2 | 3 | Explain the meaning of the following statement, assuming that `value` is a 4 | variable of type `long int` and `p` is a variable of type `char *`: 5 | 6 | ```c 7 | value = strtol(p, &p, 10); 8 | ``` 9 | 10 | ### Solution 11 | 12 | Assign `value` the `long int` value of the string p, in base 10, storing the 13 | following non-consumed character to `p`. 14 | -------------------------------------------------------------------------------- /26/exercises/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 26.08 2 | 3 | Write a statement that randomly assigns one of the numbers 7, 11, 15 or 19 to 4 | the variable `n`. 5 | 6 | ### Solution 7 | 8 | ```c 9 | r = rand() % 4; 10 | n = r == 0 ? 7 : r == 1 ? 11 : r == 2 ? 15 : 19; 11 | ``` 12 | -------------------------------------------------------------------------------- /26/exercises/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 26.09 2 | 3 | Write a function that returns a random `double` value *d* in the range 0.0 <= 4 | *d* < 1.0. 5 | 6 | ### Solution 7 | 8 | ```c 9 | double rand_double(void) 10 | { 11 | return (double) rand() / (RAND_MAX + 1) 12 | } 13 | ``` 14 | -------------------------------------------------------------------------------- /26/exercises/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 26.10 2 | 3 | Convert the following calls of `atoi`, `atol` and `atoll` into calls of 4 | `strtol`, strtol` and `strtoll`, respectively. 5 | 6 | (a) `atoi(str)` 7 | (b) `atol(str)` 8 | (c) `atoll(str)` 9 | 10 | ### Solution 11 | 12 | (a) `strtol(str, &str, 10)` 13 | (b) `strtol(str, &str, 10)` 14 | (c) `strtoll(str, &str, 10)` 15 | -------------------------------------------------------------------------------- /26/exercises/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 26.12 2 | 3 | Write a function that, when passed a year, returns a `time_t` value representing 4 | 12:00 a.m. on the first day of that year. 5 | 6 | ### Solution 7 | 8 | ```c 9 | time_t get_midnight(int year) 10 | { 11 | time_t t; 12 | if (year < 1900) 13 | return 0; /* not valid input */ 14 | t.tm_year = year - 1900; 15 | t.tm_mon = 0; 16 | t.tm_mday = 1; 17 | t.tm_hour = 0; 18 | t.tm_min = 0; 19 | t.tm_sec = 0; 20 | 21 | return t; 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /26/projects/01/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include /* rand, srand */ 3 | 4 | int main(void) 5 | { 6 | int i; 7 | for (i = 0; i < 1000; i++) 8 | printf("%d", rand() & 1); 9 | printf("\n"); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /26/projects/02/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void f1(void); 5 | void f2(void); 6 | 7 | int main(void) 8 | { 9 | atexit(f2); 10 | atexit(f1); 11 | return 0; 12 | } 13 | 14 | void f1(void) 15 | { 16 | printf("That's all, "); 17 | } 18 | 19 | void f2(void) 20 | { 21 | printf("Folks!\n"); 22 | } 23 | -------------------------------------------------------------------------------- /26/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 26.02 2 | 3 | Write a program that tests the `atexit` function. The program should have two 4 | functions (in addition to `main`), one of which prints `That's all, ` and the 5 | other `folks!`. Use the `atexit` function to register both to be called at 6 | program termination. Make sure they're called in the proper order so that we see 7 | the message `That's call, folks!` on the screen. 8 | 9 | ### Solution 10 | 11 | See `2.c`. 12 | -------------------------------------------------------------------------------- /26/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 26.03 2 | 3 | Write a program that uses the `clock` function to measure how long it takes 4 | `qsort` to sort an array of 1000 integers that are originally in reverse order. 5 | Run the program for arrays of 10000 and 100000 integers as well. 6 | 7 | ### Solution 8 | 9 | See `3.c`. 10 | -------------------------------------------------------------------------------- /26/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 26.04 2 | 3 | Write a program that prompts the user for a date (month, day and year) and an 4 | integer `n`, then prints the date that's `n` days later. 5 | 6 | ### Solution 7 | 8 | See `4.c`. 9 | -------------------------------------------------------------------------------- /26/projects/05/README.md: -------------------------------------------------------------------------------- 1 | ### Project 26.05 2 | 3 | Write a program that prompts the user to enter two dates, then prints the 4 | difference between them, measure in days. *Hint*: Use the `mktime` and 5 | `difftime` functions. 6 | 7 | ### Solution 8 | 9 | See `5.c`. 10 | -------------------------------------------------------------------------------- /26/projects/06/6a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | time_t cur = time(NULL); 7 | struct tm *p; 8 | char s[37]; 9 | 10 | p = localtime(&cur); 11 | strftime(s, sizeof(s), "%A, %B %d, %Y %I:%M", p); 12 | printf("%s%c\n", s, p->tm_hour <= 11 ? 'a' : 'p'); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /26/projects/06/6b.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | time_t cur = time(NULL); 7 | char s[22]; 8 | 9 | strftime(s, sizeof(s), "%a, %d %b %y %H:%M", localtime(&cur)); 10 | printf("%s\n", s); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /26/projects/06/6c.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | time_t cur = time(NULL); 7 | struct tm *p; 8 | char date[9]; 9 | char time[12]; 10 | 11 | p = localtime(&cur); 12 | strftime(date, sizeof(date), "%m/%d/%y", p); 13 | strftime(time, sizeof(time), "%I:%M:%S %p", p); 14 | 15 | printf("%s %s\n", date, time[0] == '0' ? &time[1] : time); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /26/projects/06/README.md: -------------------------------------------------------------------------------- 1 | ### Project 26.06 2 | 3 | Write programs that display the current date and time in each of the following 4 | formats. Use `strftime` to do all or most of the formatting. 5 | 6 | (a) `Sunday, June 3, 2007 05:48p` 7 | (b) `Sun, 3 Jun 07 17:48` 8 | (c) `06/03/07 5:48:34 PM` 9 | 10 | ### Solution 11 | 12 | See `6a.c`, `6b.c` and `6c.c`. 13 | -------------------------------------------------------------------------------- /27/exercises/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 27.01 2 | 3 | (C99) Locate the declarations of the `int`*N*`_t` and `uint`*N*`_t` types in 4 | the `` header installed on your system. Which values of *N* are 5 | supported? 6 | 7 | ### Solution 8 | 9 | On my machine, the `int`*n*`_t` and `uint`*N*`_t` types are declared on separate 10 | headers included in the `` header. Regardless, the available types 11 | include 8-, 16-, 32- and 64-bit exact integer types, signed and unsigned. 12 | -------------------------------------------------------------------------------- /27/exercises/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 27.02 2 | 3 | (C99) Write the parameterized macros `INT32_C(n)`, `UINT32_C(n)`, `INT64_C(n)` 4 | and `UINT64_C(n)`, assuming that the `int` type and `long int` types are 32 bits 5 | wide and the `long long int` type is 64 bits wide. *Hint:* Use the `##` 6 | preprocessor operator to attach a suffix to `n` containing a combination of `L` 7 | and/or `U` characters (See Section 7.1 for a discussion of how to use the `L` 8 | and `U` suffixes with integer constants). 9 | 10 | ### Solution 11 | -------------------------------------------------------------------------------- /27/projects/01/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 -lm 3 | 4 | quadratic: quadratic.o 5 | $(CC) -o quadratic quadratic.o $(CFLAGS) 6 | 7 | quadratic.o: quadratic.c 8 | $(CC) -c quadratic.c $(CFLAGS) 9 | -------------------------------------------------------------------------------- /27/projects/02/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | double real, imag, r, theta; 7 | 8 | printf("Enter real value of complex number: "); 9 | scanf("%lf", &real); 10 | printf("Enter imaginary value of complex number: "); 11 | scanf("%lf", &imag); 12 | 13 | r = sqrt(real * real + imag * imag); 14 | theta = atan(real / imag); 15 | 16 | printf("r = %g\ntheta = %g rads\n", r, theta); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /27/projects/02/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 -lm 3 | 4 | 2: 2.o 5 | $(CC) -o 2 2.o $(CFLAGS) 6 | 7 | 2.o: 2.c 8 | $(CC) -c 2.c $(CFLAGS) 9 | -------------------------------------------------------------------------------- /27/projects/02/README.md: -------------------------------------------------------------------------------- 1 | ### Project 27.02 2 | 3 | (C99) Write a program that converts a complex number in Cartesian coordinates to 4 | polar form. The user will enter *a* and *b* (the real and imaginary parts of the 5 | number); the program will display the values of *r* and θ. 6 | 7 | ### Solution 8 | 9 | See `2.c`. 10 | -------------------------------------------------------------------------------- /27/projects/03/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | double real, imag, r, theta; 7 | 8 | printf("Enter r: "); 9 | scanf("%lf", &r); 10 | printf("Enter theta (in radians): "); 11 | scanf("%lf", &theta); 12 | 13 | real = r * cos(theta); 14 | imag = r * sin(theta); 15 | 16 | printf("%g %c %gi\n", real, imag < 0 ? '-' : '+', imag); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /27/projects/03/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 -lm 3 | 4 | 3: 3.o 5 | $(CC) -o 3 3.o $(CFLAGS) 6 | 7 | 3.o: 3.c 8 | $(CC) -c 3.c $(CFLAGS) 9 | -------------------------------------------------------------------------------- /27/projects/03/README.md: -------------------------------------------------------------------------------- 1 | ### Project 27.03 2 | 3 | (C99) Write a program that converts a complex number in polar coordinates to 4 | Cartesian form. After the user enters the values of *r* and θ, 5 | the program will display the number in the form *a* + *bi*, where 6 | 7 | *a* = *r* cos θ 8 | *b* = *r* sin θ 9 | 10 | ### Solution 11 | 12 | See `3.c`. 13 | -------------------------------------------------------------------------------- /27/projects/04/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Wpedantic -std=c99 -lm 3 | 4 | 4: 4.o 5 | $(CC) -o 4 4.o $(CFLAGS) 6 | 7 | 4.o: 4.c 8 | $(CC) -c 4.c $(CFLAGS) 9 | -------------------------------------------------------------------------------- /27/projects/04/README.md: -------------------------------------------------------------------------------- 1 | ### Project 27.04 2 | 3 | (C99) Write a program that displays the *n*th roots of unity when given a 4 | positive integer *n*. The *n*th roots of unity are given by the formula 5 | *e*2π*ik*/*n*, where *k* is an integer between 0 and *n* - 1. 6 | 7 | ### Solution 8 | 9 | See `4.c`. 10 | -------------------------------------------------------------------------------- /book-notice.txt: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | -------------------------------------------------------------------------------- /cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zydchina818/C-/c858e108f04d788be752353ed578c4d09328ef15/cover.jpg --------------------------------------------------------------------------------