tailieunhanh - A Complete Guide to Programming in C++ part 50
A Complete Guide to Programming in C++ part 50. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | SOLUTIONS 469 int a3 len3 lenl len2 a3 splice al lenl a2 len2 pos if a3 NULL cerr n Invalid position n endl else cout The new spliced array endl for i 0 i len3 i cout setw 12 a3 i cout endl delete al delete a2 delete a3 return 0 ------------------------------------------------------------ Function splice inserts the array v2 into vl starting at position pos. int splice int vl int lenl int v2 int len2 int pos if pos 0 pos lenl return NULL int i 0 il 0 i2 0 int v new int len1 len2 for i v i 0 i1 0 v1 i1 i1 pos i i1 1st part for i2 v i 0 i2 v2 i2 len2 i i2 2nd part for v i i1 len1 v1 i1 i i1 3rd part return v 470 CHAPTER 2 I DYNAMIC MEMORY ALLOCATION Exercise 2 ------------------------------------------------------ Implements the merge algorithm. ------------------------------------------------------ include iostream include iomanip include cstdlib include ctime using namespace std Prototypes void selectionSort int arr int len int merge int v1 int lenl int v2 int len2 int main cout n The Merge Algorithm n endl int i lenl 10 len2 20 int a1 new int len1 a2 new int len2 Initialize the random number generator with the current time srand unsigned time NULL for i 0 i lenl i Initialized arrays a1 i rand for i 0 i len2 i a2 i rand selectionSort al lenl To sort array al. selectionSort a2 len2 To sort array a2. Output the arrays cout The sorted arrays endl cout 1st array endl for i 0 i lenl i cout setw l2 al i cout endl cout 2nd array endl for i 0 i len2 i cout setw l2 a2 i cout endl int a3 len3 a3 merge al lenl a2 len2 len3 lenl len2 SOLUTIONS 471 cout The new merged array endl for i 0 i len3 i cout setw 12 a3 i cout endl delete al delete a2 delete a3 return 0 ---------------------------------------------------------- Function selectionSort . inline void swap int a int b To swap. int temp a a b b temp void selectionSort int arr int len register int p minp Pointer to array elements int last arr len-1 Pointer to the last element for arr last arr minp arr Search minimum
đang nạp các trang xem trước