The Pre-Processor and standard libraries - Exercises

Chapter chap8 section 12

  1. Using the maths library functions sin() and cos() write a program that prints out lines of text whose full length represents the range of values -1 to +1 with a suitably positioned asterisk representing the value of sin() and a hash symbol representing the value of cos(). Produce output for values of input parameter representing angles in the range 0 degrees to 360 degrees in steps of 10 degrees.

  2. Define a macro max(a,b) to determine the larger of two integers. Define a further macro max3(a,b,c) in terms of max(a,b) to determine the largest of three numbers. Incorporate your macros in a demonstration program that prompts the user for three numbers and reports the largest.

  3. Write a program to read text from the standard input until an end-of-file condition is encountered. When all the text has been read in print out an analysis of the numbers of lines by length.

  4. The function rand() generates random integers. Write a program to verify this by generating a fairly large number of random integers and analysing them into 20 frequency ranges. Display the number of random numbers generated in each range.

  5. Write a program to randomly fill an array of 26 characters with the letters of the alphabet. Print out the randomised alphabet you have generated.

  6. Extend the program of the previous exercise to read in text from the standard and replace all letters (preserving case) with the equivalent letter from the shuffled alphabet. Print out the modified text. This could be regarded as a very simple encryption system.

    To ensure that encryption is consistent you can use the library function srand() which uses a single integer parameter to initialise the random number generator. The random initialisation can be regarded as an encryption key. Devise a decryption program to recover the text that you encrypted earlier.