You are encouraged to work with a partner.
Work in a subdirectory called initslab9, where inits are your initials. Once you have completed the C programs and made script files to demonstrate your testing of each, remove any extraneous files so that your directory contains just the .c files and the script files. Then make a tarball of that directory and send it to me as an attachment to the email whose subject line should be CS33 Lab9. Send the email to cfk@cs.swarthmore.edu.
Each program must follow these following guidelines:
float raiseRealToPower(float base, int exponent);
This function will raise base to the power exponent. The base can be any floating point value (negative, positive, zero) and the exponent can be any integer value (negative, positive, zero). You are to write this function yourself using basic C operations and control statements. You may not use math.h functions like exp or pow.
After you have written this function, test that the function is working by displaying a table of the powers of 0.5 between -15 and +5. Your table should be similar, but necessarily exactly the same as this:
-15 32768.00000 -14 16384.00000 -13 8192.00000 -12 4096.00000 -11 2048.00000 -10 1024.00000 -9 512.00000 -8 256.00000 -7 128.00000 -6 64.00000 -5 32.00000 -4 16.00000 -3 8.00000 -2 4.00000 -1 2.00000 0 1.00000 1 0.50000 2 0.25000 3 0.12500 4 0.06250 5 0.03125
You will implement several functions to allow for encrypting
and decrypting text using a method that is just slightly better
than a Caesar Cipher. The idea is that a word or phrase (of
all letters) is chosen to construct a key. First remove all
duplicates to get a partial key, then append the letters of
the alphabet (in order) that are not in the partial key to
get the full key. For example, if Trailblazers is the initial
phrase, the partial key will be: trailbzes. The full key is:
trailbzescdfghjkmnopquvwxy. To encrypt, imagine the full
alphabet above the full key.
Your task is to write 3 functions and a main program that tests the functions. Possible prototypes for your functions might be:
The function prepare_key takes a string as argument which is the initial phrase from which to construct a full key. It removes duplicates and extends the string to a full key. It returns 0 (FALSE) if it is passed an empty string. Otherwise it returns 1 (TRUE).
The function encrypt takes two arguments. The second argument, key, is a full key as delivered by prepare_key. The first is a string (<= 80 characters) to be encrypted by this method. Nonalphabetic characters of data are not changed. The case of alphabetic characters is to be preserved.
The function decrypt reconstructs an original message from an encrypted string passed to it in data using the key passed to it in key.
prepare_key is much harder to write than encrypt and decrypt. So start with the easier functions using one of my full keys shown below. Once you can successfully encrypt and decrypt, then tackle prepare_key.
You would not ordinarily show the full key or show the plain text when encrypting, but I do so here for illustrative purposes and your driver program should also. In the encrypting and decrypting parts, the lines I entered from the keyboard alternate with the lines output by my test program. Here are a couple of sample runs of my program: