E.14. Wrap-Up

Answers to Self Review Exercises

E.1

a) redirect input (<). b) redirect output (>). c) append output (>>). d) pipe (|). e) ellipsis (...). f) va_start. g) va_arg. h) va_end. i) argc. j) argv. k) make, Makefile. l) exit. m) atexit. n) suffix. o) signal. p) raise. q) calloc. r) realloc. s) union. t) union.

Exercises

E.2

Write a program that calculates the product of a series of integers that are passed to function product using a variable-length argument list. Test your function with several calls, each with a different number of arguments.

E.3

Write a program that prints the command-line arguments of the program.

E.4

Write a program that sorts an integer array into ascending order or descending order. The program should use command-line arguments to pass either argument -a for ascending order or -d for descending order. [Note: This is the standard format for passing options to a program in UNIX.]

E.5

Read the manuals for your system to determine what signals are supported by the signal-handling library (). Write a program with signal handlers for the signals SIGABRT and SIGINT. The program should test the trapping of these signals by calling function abort to generate a signal of type SIGABRT and by pressing Ctrl+C to generate a signal of type SIGINT.

 
E.6

Write a program that dynamically allocates an array of integers using a function from , not the new operator. The size of the array should be input from the keyboard. The elements of the array should be assigned values input from the keyboard. Print the values of the array. Next, reallocate the memory for the array to half of the current number of elements. Print the values remaining in the array to confirm that they match the first half of the values in the original array.

E.7

Write a program that takes two file names as command-line arguments, reads the characters from the first file one at a time and writes the characters in reverse order to the second file.

E.8

Write a program that uses goto statements to simulate a nested looping structure that prints a square of asterisks as shown in Fig. E.10. The program should use only the following three output statements:

 cout << '*';
 cout << ' ';
 cout << endl;
E.9

Provide the definition for union Data containing char charcter1, short short1, long long1, float float1 and double double1.

E.10

Create union Integer with members char charcter1, short short1, int integer1 and long long1. Write a program that inputs values of type char, short, int and long and stores the values in union variables of type union Integer. Each union variable should be printed as a char, a short, an int and a long. Do the values always print correctly?

E.11

Create union FloatingPoint with members float float1, double double1 and long double longDouble. Write a program that inputs values of type float, double and long double and stores the values in union variables of type union FloatingPoint. Each union variable should be printed as a float, a double and a long double. Do the values always print correctly?

E.12

Given the union

 union A
 {
 double y;
 char *zPtr;
 };
 

which of the following are correct statements for initializing the union?

  1. A p = b; // b is of type A
  2. A q = x; // x is a double
  3. A r = 3.14159;
  4. A s = { 79.63};
  5. A t = { "Hi There!" };
  6. A u = { 3.14159, "Pi" };
  7. A v = { y = 7.843 ,zPtr = &x };

Figure E.10. Example for Exercise E.8.

 *****
 * *
 * *
 * *
 *****
 




C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627
Simiral book on Amazon

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