Robroy Gregg: robroy@armory.com
Revision: 1.10
Last updated: May 12, 2003
| Revision | Change description | Date |
| 1.1 | Initial post | March 8, 2001 |
| 1.2 | Added current info through Chap. 10 | March 9, 2001 |
| 1.3 | Changed source links to point to HTML files created with "c2html" | March 10, 2001 |
| 1.4 | Added info through Chap. 11 | April 17, 2001 |
| 1.5 | Added Chap. 12 | May 2, 2001 |
| 1.6 | Added Chap. 13 | May 4, 2001 |
| 1.7 | Added Chap. 14 | May 9, 2001 |
| 1.8 | Added Chap. 15 | May 15, 2001 |
| 1.9 | Added Chap. 16 | May 22, 2001 |
| 1.10 |
Fixed a bug in exercise 5.2, and added exercises
5.1 and 6.1 |
May 12, 2003 |
Reviewing Chapter 6 exercises.
Sponsor: Steve Hodges
Text: Practical C Programming by Steve Oualline; 3rd Ed.
Supplemental text: A Book on C by Al Kelley and Ira Pohl; 4th Ed.
The goal of this course is to enhance my understanding of the C programming language by completing the main text and the related exercises.<no exercises exist for chapters 1-3...>
Although I finished this course a couple of years ago, I'll continue to update this page as I repair whatever bugs I happen to notice.
|
|
Exercise | Source |
|
|
Write a program to print your name, social security number, and date of birth. | |
|
|
Write a program to print a block E using asterisks (*), where the E has a height of seven characters and a width of five characters. | 4.2.c |
|
|
Write a program to compute the area and perimeter of a rectangle with a width of three inches and a height of five inches. What changes must be made to the program so that it works for a rectangle with a width of 6.8 inches and a length of 2.3 inches? | 4.3.c |
|
|
Write a program to print "HELLO" in big block letters; each letter should have a height of seven characters and width of five characters. | 4.4.c |
|
|
Write a program that deliberately makes the following mistakes: Prints a float-point number using the %d conversion; Prints an integer using the %f conversion; Prints a character using the %d conversion. |
|
|
Exercise | Source |
|
|
Write a program that converts Centigrade to Fahrenheit. F = (9/5)C + 32 | 5.1.c |
|
|
Write a program to calculate the volume of a sphere. (4/3)pi(r^3) | 5.2.c |
|
|
Write a program that prints the perimeter of a rectangle given its height and width. perimeter = 2 * (width + height) | 5.3.c |
|
|
Write a program that converts kilometers per hour to miles per hour. miles = (kilometer * 0.6213712) | 5.4.c |
|
|
Write a program that takes hours and minutes as input, and then outputs the total number of minutes. (1 hour 30 minutes = 90 minutes). | 5.5.c |
|
|
Write a program that takes an integer as the number of minutes, and outputs the total hours and minutes (90 minutes = 1 hour 30 minutes). | 5.6.c |
|
|
Exercise | Source |
|
|
Write a program to find the square of the distance between two points. (For a more advanced problem, find the actual distance. This problem involves using the standard function sqrt.) | 6.1.c |
|
|
A professor generates letter grades using this scale: 0-60 F, 61-70 D, 71-80 C, 81-90 B, 91-100 A. Given a numeric grade, print the letter. | 6.2.c |
|
|
Modify the previous program to print a + or a - after the letter grade, based on the last digit of the score. Modifiers: Last digit 1-3 -, 4-7 ' ', 8-0 +. For example, 81=B-, 94=A, and 68=D+. | 6.3.c |
|
|
Given an amount of money (less than $1.00), compute the number of quarters, dimes, nickels, and pennies needed. | 6.4.c |
|
|
A leap year is any year divisible by 4, unless the years is divisible by 100, but not 400. Write a program to tell if a year is a leap year. | 6.5.c |
|
|
Write a program that, given the number of hours an employee worked and the hourly wage, computes the employee's weekly pay. Count any hours over 40 as overtime at time and a half. | 6.6.c |
coming soon...
|
|
Exercise | Source |
|
|
Print a checker board (8-by-8 grid). Each square should be 5-by-3 characters wide. | 8.1.c |
|
|
The total resistance of n resistors in parallel is: (1/R) = 1/R1 + 1/R2 + 1/R3 + ... + 1/Rn. Write a program to compute the total resistance for any number of parallel resistors. | 8.2.c |
|
|
Write a program to average n numbers. | |
|
|
Write a program to print out the multiplication table. | 8.4.c |
|
|
Write a program that reads a character and prints out whether or not it is a vowel or a consonant. | 8.5.c |
|
|
Write a program that converts numbers to words. For example, 895 results in "eight nine five." | 8.6.c |
|
|
The number 85 is pronounced "eight-five," not "eight five." Modify the previous program to handle the numbers 0 through 100 so that all numbers come out as we really say them. For example, 13 would be "thirteen" and 100 would be "one hundred." |
|
|
Exercise | Source |
|
|
Write a procedure that counts the number of words in a string. (Your documentation should describe exactly how you define a word.) Write a program to test your new procedure. | 9.1.c |
|
|
Write a function begins(string1,string2) that returns true if string1 begins string2. Write a program to test the function. | 9.2.c |
|
|
Write a function count(number, array, length) that counts the number of times number appears in array. The array has length elements. The function should be recursive. Write a test program to go with the function. | 9.3.c |
|
|
Write a function that takes a character array and returns a primitive hash code by adding up the value of each character in the array. | |
|
|
Write a function that returns the maximum value of an array of numbers. | 9.5.c |
|
|
Write a function that scans a character array for the character "-" and replaces it with "_". | 9.6.c |
|
|
Exercise | Source |
|
|
Write a macro that returns TRUE if its parameter is divisible by 10 and FALSE otherwise. | 10.1.c |
|
|
Write a macro is_digit that returns TRUE if its argument is a decimal digit. | 10.2.c |
|
|
Write a second macro is_hex that returns true if its argument is a hex digit (0-9, A-F, a-f). The second macro should reference the first. | 10.3.c |
|
|
Write a preprocessor macro that swaps two integers. | 10.4.c |
|
|
Exercise | Source |
|
|
Write a set of parameterized macros, clear_bit and test_bit, to go with the set_bit operation defined in Example 11-3. Write a main program to test these macros. | 11.1.c |
|
|
Write a program to draw a 10-by-10 bitmapped square. You can borrow the code from Example 11-3 to print the results. | 11.2.c |
|
|
Change Example 11-3 so that it draws a white line across a black background. | 11.3.c |
|
|
Write a program that counts the number of bits set in an integer. For example, the number 5 (decimal), which is 0000000000000101 (binary), has two bits set. | 11.4.c |
|
|
Write a program that takes a 32-bit integer (long int) and splits it into eight 4-bit values. (Be careful of the sign bit.) | 11.5.c |
| 6 | Write a program that will take the bits in a number and shift them to the left end. For example, 01010110 (binary) would become 11110000 (binary). | 11.6.c |
|
|
Exercise | Source |
|
|
Design a structure to hold the data for a mailing list. Write a function to print out the data. | 12.1.c |
|
|
Design a structure to store time and date. Write a function to find the difference between two times in minutes. | 12.2.c |
|
|
Design an airline reservation data structure that contains the following
data:
|
12.3.c |
|
|
Write a program that lists all the planes that leave from two airports specified by the user. | 12.4.c |
|
|
Exercise | Source |
|
|
Write a program that uses pointers to set each element of an array to zero. | 13.1.c |
|
|
Write a function that takes a single string as its argument and returns a pointer to the first nonwhite character in the string. | 13.2.c |
|
|
Exercise | Source |
|
|
Write a program that reads a file and then counts the number of lines in it. | 14.1.c |
|
|
Write a program to copy a file, expanding all tabs to multiple spaces. | 14.2.c |
|
|
Writea program that reads a file containing a list of numbers, and then writes two files, one with all numbers divisible by three and another containing all the other numbers. | 14.3.c |
|
|
Write a program that reads an ASCII file
containing a list of numbers and writes a binary file containing the same
list. Write a program that goes the other way so that you can check
your work.
(I wrote one program that reads either ASCII or binary files depending on command-line arguments.) |
14.4.c |
|
|
Write a program that copies a file and removes all characters with the high bit set ((ch & 0x80) != 0). | 14.5.c |
| 6 | Design a file format to store a person's name, address, and other information. Write a program to read this file and produce a set of mailing labels. | 14.6.c
14.6.data |
|
|
Exercise | Source |
|
|
Take one of your previous programs and run it using the interactive debugger to examine several intermediate values. | 15.1.txt |
|
|
Write a matrix multiply function. Create a test program that not only tests the function, but times it as well. Optimize it using pointers and determine the time savings. | |
|
|
Write a program to sum the elements in an array. Optimize it. | 15.3.c |
|
|
Write a program that counts the number of bits in a character array. Optimize it through the user of register integer variables. Time it on several different arrays of different sizes. How much time do you save? | 15.4.c |
|
|
Write your own version of the library function memcpy. Optimize it. Most implementations of memcpy are written in assembly language and take advantage of all the quirks and tricks of the processor. How does your memcpy compare with others? | 15.5.c |
|
|
Exercise | Source |
|
|
Write a program that uses strings to represent floating-point numbers in the format used in this chapter. A typical string might look like "+1.333E+2". The program should have functions to read, write, add, subtract, multiply, and divide floating-point numbers. | 16.1.c |
|
|
Create a set of functions to handle fixed-point numbers. A fixed-point number has a constant (fixed) number of digits to the right of the decimal point. |