Problems for Assignment #5

Learning Outcomes: 1) writing programs using the C language; 2) handling string, integer, and binary representations; 3) understanding the change between 32-bit and 64-bit representations; 4) converting from decimal to binary/hexadecimal and back; and 5) learning to use the command line arguments
  1. Write a C program, dec2bin.c to convert a base-10 number to its 32-bit binary equivalent value. You must take the base-10 number in from the command line. Your output should be a string of binary digits which correspond to the base-10 value. For example, running the program with dec2bin 65535 should produce the output string 00000000000000001111111111111111. Use unsigned integers.
  2. Write a C program similar to problem #1 to to make the program dec2hex.c which will output the 32-bit or 64-bit [8-digit or 16-digit] hexidecimal equivalent of its input. For this modification of the first program, you must also handle an optional command line argument which indicates the number of digits the output hex value will contain, either 8 or 16. This will be the second argument on the line and if it is omitted the program will default to 8 to get the 32-bit equivalent. For example, running the program with dec2hex 65535 8 should produce the output string 0x0000FFFF, and running with dec2hex 65535 16 should result in the output string 0x000000000000FFFF. Running with dec2hex 65535 should also produce the output string 0x0000FFFF. [HINT: check out the printf() output format specifications — you'll find some help there to make things easier.]
  3. Write a C program timesTables.cto output the times tables from 2 to N, where N is a user-defined number taken from the command line. Output the values in a nice table, using a format specifier that will allow for enough space for the results to be neatly aligned in columns. Try to pad the values so that they have at least one space before and one space after the value in each cell of the table. Make sure you have enough space for the longest number in all of the cells.
  4. Write a C program holdit.c that times you as you hold your breath. The program must put out a short message that has instructions on what to do, which should read something like,
    "This program will time how long you can hold your breath. Take a deep breath, then press the 'Enter' key. When you absolutely have to exhale, press the enter key again. The duration will be displayed in minutes and seconds."
    You will need to research the way the time functions work in C.
  5. Write a C program wordcount.c that counts the number of words in a file of text. Your program should take a file name as a command line argument. As you read the file contents, keep a count of the number of words which are separated by whitespace. [Research what is meant by whitespace in the C environment.] When the file has been completely read, close the file and write out the number of words. Be sure you handle error conditions like files that don't exist or errors encountered while reading the file. You should also be able to handle files that are in different directories from where your program resides.