Absolute C++ (4th Edition) part 21

Absolute C++ (4th Edition) part 21. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | 202 Arrays Display Sorting an Array part 2 of 3 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 50 The array elements a 0 through a numberUsed - 1 have values. Postcondition The values of a 0 through a numberUsed - 1 have been rearranged so that a 0 a 1 . a numberUsed - 1 . void swapValues int v1 int v2 Interchanges the values of v1 and v2. int indexOfSmallest const int a int startindex int numberUsed Precondition 0 startindex numberUsed. Reference array elements have values. Returns the index i such that a i is the smallest of the values a startIndex a startIndex 1 . a numberUsed - 1 . int main cout This program sorts numbers from lowest to highest. n int sampleArray 10 numberUsed fillArray sampleArray 10 numberUsed sort sampleArray numberUsed cout In sorted order the numbers are n for int index 0 index numberUsed index cout sampleArray index cout endl return 0 void fillArray int a int size int numberUsed The rest of the definition of fillArray is given in Display . void sort int a int numberUsed int indexOfNextSmallest for int index 0 index numberUsed - 1 index Place the correct value in a index indexOfNextSmallest indexOfSmallest a index numberUsed swapValues a index a indexOfNextSmallest a 0 a 1 . a index are the smallest of the original array elements. The rest of the elements are in the remaining positions. void swapValues int v1 int v2 int temp temp v1 v1 v2 Programming with Arrays 203 Display Sorting an Array part 3 of 3 51 v2 temp 52 53 54 int indexOfSmallest const int a int startindex int numberUsed 55 56 int min a startIndex 57 indexOfMin startindex 58 59 60 61 62 63 64 65 66 Sample Dialogue This program sorts numbers from lowest to highest. Enter up to 10 nonnegative whole numbers. Mark the end of the list with a negative number. 80 30 50 70 60 90 20 30 40 -1 In sorted order the numbers are 20 30 30 40 50 60 70 80 90 for int index startindex 1 index numberUsed index if a .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.