The following guidelines are expected for all homework submissions:

Problems for Assignment #5

  1. Write a C program, dec2bin.c to convert a base-10 number to its 32-bit binary value equivalent. You may take the base-10 number in from the command line, or you may prompt the user for the number and read in her response [your option]. Your output must be a string of 32 binary digits which correspond to the base-10 value. For example, running the program with dec2bin 65535 [or just dec2bin if asking the user] should produce the output string 00000000000000001111111111111111. Use unsigned integers and error check the entry [as you always should].
  2. Modify your C program from problem #1 to to make the program dec2hex.c which will output the 32-bit hexidecimal value equivalent in 8-digit format with a leading 0x. For this modification, you must also handle an optional command line argument which indicates the number of bits that the output hex value will represent, either 32 or 64. If there are no arguments present on the command line, you must ask the user for the number to convert and default to the number of bits in the representation. For example, running the program with dec2hex 65535 32 [or just dec2hex 32 if asking the user for the value] should produce the output string 0x0000FFFF. Running the program with input dec2hex 65535 64 [or just dec2hex 64] will result in the output of 0x000000000000FFFF. Use unsigned integers. Remember "32" will produce 8-digits and "64" will produce 16 digits. Both outputs must be prefixed with "0x".
  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 [not an option for this one]. 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. You may need to adapt the format to allow for increasingly large results. For example, timesTables 5 should produce an output as follows:
       1    2    3    4    5
       2    4    6    8   10
       3    6    9   12   15
       4    8   12   16   20
       5   10   15   20   25
                     
    You will need to research the output format specifiers for printf().
  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 must take a file name as a command line argument. As you read the file's 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 to the terminal. Be sure you handle any error conditions like files that don't exist or errors while reading the file. You must also be able to handle files that are in different directories from where your program resides.