Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Exercises

Exercises

F.2Write 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.
F.3Write a program that prints the command-line arguments of the program.
F.4Write 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.]
F.5Read the manuals for your system to determine what signals are supported by the signal-handling library (<csignal>). 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.
F.6Write a program that dynamically allocates an array of integers using a function from <cstdlib>, 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.
F.7Write a program that takes two filenames 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.
F.8Write a program that uses goto statements to simulate a nested looping structure that prints a square of asterisks. The program should use only the following three output statements:
cout << '*';
cout << ' ';
cout << endl;

F.9Provide the definition for union Data containing char charcter1, short short1, long long1, float float1 and double double1.
F.10Create union Integer with members char c, short s, int i and long l. 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?
F.11Create 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?
F.12Given 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 };


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


 Â