Table of Contents for
Intermediate C Programming

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Intermediate C Programming by Yung-Hsiang Lu Published by CRC Press, 2015
  1. Front Cover
  2. Contents (1/2)
  3. Contents (2/2)
  4. List of Figures (1/2)
  5. List of Figures (2/2)
  6. List of Tables
  7. Foreword
  8. Preface
  9. Author, Reviewers, and Artist
  10. Rules in Software Development
  11. Source Code
  12. I. Computer Storage: Memory and File
  13. 1. Program Execution (1/2)
  14. 1. Program Execution (2/2)
  15. 2. Stack Memory (1/5)
  16. 2. Stack Memory (2/5)
  17. 2. Stack Memory (3/5)
  18. 2. Stack Memory (4/5)
  19. 2. Stack Memory (5/5)
  20. 3. Prevent, Detect, and Remove Bugs (1/2)
  21. 3. Prevent, Detect, and Remove Bugs (2/2)
  22. 4. Pointers (1/6)
  23. 4. Pointers (2/6)
  24. 4. Pointers (3/6)
  25. 4. Pointers (4/6)
  26. 4. Pointers (5/6)
  27. 4. Pointers (6/6)
  28. 5. Writing and Testing Programs (1/4)
  29. 5. Writing and Testing Programs (2/4)
  30. 5. Writing and Testing Programs (3/4)
  31. 5. Writing and Testing Programs (4/4)
  32. 6. Strings (1/3)
  33. 6. Strings (2/3)
  34. 6. Strings (3/3)
  35. 7. Programming Problems and Debugging (1/4)
  36. 7. Programming Problems and Debugging (2/4)
  37. 7. Programming Problems and Debugging (3/4)
  38. 7. Programming Problems and Debugging (4/4)
  39. 8. Heap Memory (1/3)
  40. 8. Heap Memory (2/3)
  41. 8. Heap Memory (3/3)
  42. 9. Programming Problems Using Heap Memory (1/4)
  43. 9. Programming Problems Using Heap Memory (2/4)
  44. 9. Programming Problems Using Heap Memory (3/4)
  45. 9. Programming Problems Using Heap Memory (4/4)
  46. 10. Reading and Writing Files (1/3)
  47. 10. Reading and Writing Files (2/3)
  48. 10. Reading and Writing Files (3/3)
  49. 11. Programming Problems Using File (1/2)
  50. 11. Programming Problems Using File (2/2)
  51. II. Recursion
  52. 12. Recursion (1/4)
  53. 12. Recursion (2/4)
  54. 12. Recursion (3/4)
  55. 12. Recursion (4/4)
  56. 13. Recursive C Functions (1/4)
  57. 13. Recursive C Functions (2/4)
  58. 13. Recursive C Functions (3/4)
  59. 13. Recursive C Functions (4/4)
  60. 14. Integer Partition (1/5)
  61. 14. Integer Partition (2/5)
  62. 14. Integer Partition (3/5)
  63. 14. Integer Partition (4/5)
  64. 14. Integer Partition (5/5)
  65. 15. Programming Problems Using Recursion (1/5)
  66. 15. Programming Problems Using Recursion (2/5)
  67. 15. Programming Problems Using Recursion (3/5)
  68. 15. Programming Problems Using Recursion (4/5)
  69. 15. Programming Problems Using Recursion (5/5)
  70. III. Structure
  71. 16. Programmer-Defined Data Types (1/6)
  72. 16. Programmer-Defined Data Types (2/6)
  73. 16. Programmer-Defined Data Types (3/6)
  74. 16. Programmer-Defined Data Types (4/6)
  75. 16. Programmer-Defined Data Types (5/6)
  76. 16. Programmer-Defined Data Types (6/6)
  77. 17. Programming Problems Using Structure (1/4)
  78. 17. Programming Problems Using Structure (2/4)
  79. 17. Programming Problems Using Structure (3/4)
  80. 17. Programming Problems Using Structure (4/4)
  81. 18. Linked Lists (1/3)
  82. 18. Linked Lists (2/3)
  83. 18. Linked Lists (3/3)
  84. 19. Programming Problems Using Linked List (1/2)
  85. 19. Programming Problems Using Linked List (2/2)
  86. 20. Binary Search Trees (1/4)
  87. 20. Binary Search Trees (2/4)
  88. 20. Binary Search Trees (3/4)
  89. 20. Binary Search Trees (4/4)
  90. 21. Parallel Programming Using Threads (1/5)
  91. 21. Parallel Programming Using Threads (2/5)
  92. 21. Parallel Programming Using Threads (3/5)
  93. 21. Parallel Programming Using Threads (4/5)
  94. 21. Parallel Programming Using Threads (5/5)
  95. IV. Applications
  96. 22. Finding the Exit of a Maze (1/5)
  97. 22. Finding the Exit of a Maze (2/5)
  98. 22. Finding the Exit of a Maze (3/5)
  99. 22. Finding the Exit of a Maze (4/5)
  100. 22. Finding the Exit of a Maze (5/5)
  101. 23. Image Processing (1/3)
  102. 23. Image Processing (2/3)
  103. 23. Image Processing (3/3)
  104. 24. Huffman Compression (1/10)
  105. 24. Huffman Compression (2/10)
  106. 24. Huffman Compression (3/10)
  107. 24. Huffman Compression (4/10)
  108. 24. Huffman Compression (5/10)
  109. 24. Huffman Compression (6/10)
  110. 24. Huffman Compression (7/10)
  111. 24. Huffman Compression (8/10)
  112. 24. Huffman Compression (9/10)
  113. 24. Huffman Compression (10/10)
  114. A. Linux
  115. B. Version Control
  116. C. Integrated Development Environments (IDE) (1/3)
  117. C. Integrated Development Environments (IDE) (2/3)
  118. C. Integrated Development Environments (IDE) (3/3)
Integer Partition 211
FIGURE 14.1: Graphical illustration of f1 calls f2.
void f1 ()1
{2
f2 () ;3
f3 () ;4
}5
FIGURE 14.2: Graphical illustration of f1 calls f2 and f3.
This is the third example and the calling relation is shown in Fig. 14.3.
void f1 ()1
{2
f2 () ;3
f3 () ;4
}5
void f2 ()6
{7
f3 () ;8
}9
FIGURE 14.3: Graphical illustration of f1 calls f2 and f3; f2 also calls f3.
Here we add a loop to the function f1 and Fig. 14.4 shows the relation.
void f1 ()1
{2
int count ;3
for ( count = 1; count < 4; count ++)4
{5
f2 () ;6
}7
f3 () ;8
212 Intermediate C Programming
}9
void f2 ()10
{11
f3 () ;12
}13
FIGURE 14.4: Graphical illustration of f1 calls f2 in a loop and f3 outside a loop; f2
calls f3.
Next, let’s consider the skeleton of the partition function:
void partition ( i n t * arr , i nt ind , i nt left )1
{2
int val ;3
for ( val = 1; val <= left ; val ++)4
{5
arr [ ind ] = val ;6
partition ( arr , ind + 1 , left - val ) ;7
}8
}9
Fig. 14.5 illustrates the calling relation.
FIGURE 14.5: Graphical illustration of partition when the initial value of left is 3.
When left is 3, then val can be 1, 2, or 3.
1. When val is 1, left-val is 2. Thus, partition(arr, 1, 2) is called.
2. When val is 2, left-val is 1. Thus, partition(arr, 1, 1) is called.
3. When val is 3, left-val is 0. Thus, partition(arr, 1, 0) is called.
When left is 2, val can be 1 or 2. The calling relationship is illustrated in Fig. 14.6 and
Fig. 14.7.
The call tree is a different way to help understand the calling relation. It is a higher
level representation than the call stack because each call is represented by arguments and
we do not need to examine all of the addresses and values used in each call.
Integer Partition 213
FIGURE 14.6: Graphical illustration of partition when the value of left is 2.
FIGURE 14.7: Graphical illustration of partition when the value of left is 1.
14.3 Generating Partitions with Restrictions
The program at the beginning of this chapter prints all possible partitions. This section
explains how to change the program such that it generates partitions with restrictions, for
example, partitioning with odd numbers or using sequences of increasing numbers. One
simple solution is to check whether the restrictions have been satisfied before printing.
Thus, in the base case, before printing anything, the function checks whether this partition
is valid under the restriction. For example, if we are partitioning with odd numbers only,
printPartition can be modified as follows:
void print Partit ion ( in t * arr , int length )1
{2
int ind ;3
// check whether any number is even4
// if an even number is used , do not print a n ything5
for ( ind = 0; ind < length ; ind ++)6
214 Intermediate C Programming
{7
i f (( arr [ ind ] % 2) == 0)8
{9
return ;10
}11
}12
for ( ind = 0; ind < length - 1; ind ++)13
{14
printf (" %d + " , arr [ ind ]) ;15
}16
printf (" %d\ n" , arr [ length - 1]) ;17
}18
To check whether the numbers form an increasing sequence:
void print Partit ion ( in t * arr , int length )1
{2
int ind ;3
for ( ind = 0; ind < length - 1; ind ++)4
{5
i f ( arr [ ind ] >= arr [ ind + 1]) // not i ncreasing6
{7
return ;8
}9
}10
for ( ind = 0; ind < length - 1; ind ++)11
{12
printf (" %d + " , arr [ ind ]) ;13
}14
printf (" %d\ n" , arr [ length - 1]) ;15
}16
However, checking before printing is inefficient because many invalid partitions have al-
ready been generated. Instead, a more efficient solution does not generate invalid partitions.
This section explains how to generate valid partitions satisfying one of the following restric-
tions: (i) using odd numbers only, (ii) using increasing numbers, and (iii) using alternating
odd and even numbers.
14.3.1 Using Odd Numbers Only
The function partition generates only partitions that meet the criteria. It is thus much
faster than an approach where all partitions are generated and then “filtered” before being
printed. If only odd numbers are used, val can be an odd number only.
void partition ( i n t * arr , i nt ind , i nt left )1
{2
int val ;3
i f ( left == 0)4
{5
prin t Partit ion ( arr , ind );6
return ;7
}8
for ( val = 1; val <= left ; val += 2) // odd n u mbers only9
Integer Partition 215
{10
arr [ ind ] = val ;11
partition ( arr , ind + 1 , left - val ) ;12
}13
}14
This will generate fewer partitions and all of them are valid.
14.3.2 Using Sequences of Increasing Numbers
To generate partitions using increasing numbers, the smallest value of val must be
greater than the most recently used value stored in arr. However, if ind is zero, then no
previously used value is stored in arr, and val can start from one.
void partition ( i n t * arr , i nt ind , i nt left )1
{2
int val ;3
i f ( left == 0)4
{5
prin t Partit ion ( arr , ind );6
return ;7
}8
int min = 1;9
i f ( ind != 0)10
{11
min = arr [ ind - 1] + 1;12
}13
for ( val = min ; val <= left ; val ++)14
{15
arr [ ind ] = val ;16
partition ( arr , ind + 1 , left - val ) ;17
}18
}19
14.3.3 Using Alternating Odd and Even Numbers
To generate alternating odd and even numbers, the function must check whether ind
is zero. If it is zero, val can be either odd or even, because val is being written into the
first position of the partition. If ind is greater than zero, then the function needs to check
arr[ind - 1]. If arr[ind - 1] is odd, then val must be an even number. If arr[ind - 1]
is even, then val must be an odd number. This is checked in line 18.
void partition ( i n t * arr , i nt ind , i nt left )1
{2
int val ;3
i f ( left == 0)4
{5
prin t Partit ion ( arr , ind );6
return ;7
}8
for ( val = 1; val <= left ; val ++)9
{10