Cpphtp5 AppE IM
Cpphtp5 AppE IM
C Legacy Code
Topics
Well use a signal I have tried
and found far-reaching and
easy to yell. Waa-hoo!
Zane Grey
It is quite a three-pipe
problem.
Sir Arthur Conan Doyle
OBJECTIVES
In this chapter you will learn:
William Shakespeare
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Self-Review Exercises
E.1
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
s) A(n)
is an entity containing a collection of variables that occupy the same
memory, but at different times.
ANS: union.
t) The
keyword is used to introduce a union definition.
ANS: union.
Exercises
E.2
Write a program that calculates the product of a series of integers that are passed to function
using a variable-length argument list. Test your function with several calls, each with a different number of arguments.
product
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
call
c )
b, c, d )
a, b, c, d, e )
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
4
41
42
43
44
45
46
47
48
49
50
51
a = 1, b = 2, c = 3, d = 4, e = 5
The
The
The
The
E.3
sum
sum
sum
sum
of
of
of
of
a and
a, b,
a, b,
a, b,
b is: 3
and c is: 6
c, and d is: 10
c, d, and e is: 15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
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.]
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
6
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
if ( a[ i ] < a[ j ] )
swap( &a[ j ], &a[ i ] );
// display sorted array
cout << "\n\nThe sorted array is:\n";
for ( int j = 0; j < count; j++ )
cout << setw( 3 ) << a[ j ];
cout << '\n';
} // end else
return 0;
} // end main
// function that swaps two integers
void swap( int * const xPtr, int * const yPtr )
{
int temp; // temporary variable used for swapping
// swap the two integers
temp = *xPtr;
*xPtr = *yPtr;
*yPtr = temp;
} // end function swap
C:\>exE_04.exe -a
Enter up to 100 integers (EOF to end input): 5 8 34 -2 65 7 4
^Z
The sorted array is:
-2 4 5 7 8 34 65
C:\>exE_04.exe -d
Enter up to 100 integers (EOF to end input): 77 2 -8 9 44 8 76 41 99
^Z
The sorted array is:
99 77 76 44 41 9 8
2 -8
E.5
Read the manuals for your system to determine what signals are supported by the signalhandling 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.
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
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.
<cstdlib>,
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
8
47
48
49
50
51
52
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.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Contents of text.txt:
Programming in C++ is fun.
Contents of copy.txt:
.nuf si ++C ni gnimmargorP
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:
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
10
*****
*
*
*
*
*
*
*****
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
39
40
11
return 0;
} // end main
E.9
E.10
Create union Integer with members char charcter1, short short1, int integer1 and long
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?
long1.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
12
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
Enter a
Current
char c
short s
int i
long l
character: w
values in union Integer are:
= w
= -13193
= -858993545
= -858993545
Enter a
Current
char c
short s
int i
long l
short: 5
values in union Integer are:
= ?
= 5
= -859045883
= -859045883
13
long: 1000000
values in union Integer are:
= @
= 16960
= 1000000
= 1000000
E.11
Create union FloatingPoint with members float float1, double double1 and long double
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?
longDouble.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
14
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
E.12
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
which of the following are correct statements for initializing the union?
a)
A p = b;
// b is of type A
b)
A q = x;
// x is a double
c)
A r = 3.14159;
d)
A s = { 79.63 };
e)
A t = { "Hi There!" };
f)
A u = { 3.14159, "Pi" };
g)
ANS: Correct
ANS: Incorrect
ANS: Incorrect
ANS: Correct
ANS: Incorrect
ANS: Incorrect
ANS: Incorrect
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
15