Programming Exercises

I l @ ve RuBoard

Programming Exercises

  1. Start developing a header file of preprocessor definitions that you want to use.

  2. The harmonic mean of two numbers is obtained by taking the inverses of the two numbers , averaging them, and taking the inverse of the result. Use a #define directive to define a macro "function" that performs this operation. Write a simple program that tests the macro.

  3. Polar coordinates describe a vector in terms of magnitude and the counterclockwise angle from the x-axis to the vector. Rectangular coordinates describe the same vector in terms of x and y components (see Figure 16.3). Write a program that reads the magnitude and angle (in degrees) of a vector and then displays the x and y components . The relevant equations are these:

    Figure 16.3. Unary and binary operators.
    graphics/16fig03.jpg
     x = r cos A    y = r sin A 

    To do the conversion, use a function that takes a structure containing the polar coordinates and returns a structure containing the rectangular coordinates (or use pointers to such structures, if you prefer).

  4. Modify the program in Listing 16.12 so that it processes TV show names instead of numbers and saves the strings in a file. If the file already exists when the program is run, have it first load the contents into memory and then display them. Use a fixed- size record for storing each string, and use fread() and fwrite() .

  5. This exercise is the same as Exercise 4 except that you should use variable- sized records to store the strings. Each string should be preceded in storage by a byte holding the length of the string. On input, the program will first read the number byte and then use that information to determine the number of bytes to read for the string.

  6. The ANSI library features a clock() function with this description:

     #include <time.h> clock_t clock (void); 

    Here, clock_t is a type defined in time.h . The function returns the processor time, which is given in some implementation-dependent units. (If the processor time is unavailable or cannot be represented, the function returns a value of -1 .) However, CLOCKS_PER_SEC , also defined in time.h , is the number of processor time units per second. Therefore, dividing the difference between two return values of clock() by CLOCKS_PER_SEC gives you the number of seconds elapsed between the two calls. Typecasting the values to double before division enables you to get fractions of a second. Write a function that takes a double argument representing a desired time delay and then runs a loop until that amount of time has passed. Write a simple program that tests the function.

  7. Write a function that takes as arguments the name of an array of type int elements, the size of an array, and a value representing the number of picks. The function then selects the indicated number of items at random from the array and prints them. No array element is to be picked more than once. (This simulates picking lottery numbers or jury members .) Also, if your implementation has time() (discussed in Chapter 13, "Storage Classes and Program Development" ) or a similar function available, use its output with srand () to initialize the rand() random number generator. Write a simple program that tests the function.

  8. Modify Listing 16.13 so that it uses an array of struct names elements (as defined after the listing) instead of an array of double . Use fewer elements, and initialize the array explicitly to a suitable selection of names.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net