TAILIEUCHUNG - Lecture Introduction to computer and programming - Lecture No 19

The contents of this chapter include all of the following: Pointer arithmetic – a program, sorting techniques – a program, passing an entire array to a function , array and pointer, 2 dimensional array. | CSC103: Introduction to Computer and Programming Lecture No 19 Previous lecture A program to calculate percentage marks Array initialization Bound checking Passing array elements to function By value By address Pointer arithmetic Printing value of array elements using pointer Today’s lecture outline Pointer arithmetic – a program Sorting techniques – a program Passing an Entire Array to a Function Array and pointer 2 Dimensional Array Pointer arithmetic - program 3 i 560 *x j 790 c k 320 *y *z 560 790 320 564 321 794 564 794 321 Go to program Sorting program Sorting means arranging element of array in ascending or descending order After arranging array elements in ascending order After arranging array elements in descending order 24 34 12 44 56 17 0 1 2 3 4 5 500 504 508 512 516 520 12 17 24 34 44 56 0 1 2 3 4 5 500 504 508 512 516 520 56 44 34 24 17 12 0 1 2 3 4 5 500 504 508 512 516 520 Sorting techniques Selection sort Bubble sort Insertion sort Selection sort C program – Selection sort 44 33 55 22 11 0 1 2 3 4 i = 0 j = 1 if ( 44 > 33) 33 44 55 22 11 0 1 2 3 4 j = 2 if ( 33 > 55) true false j = 3 if ( 33 > 22) true 22 44 55 33 11 0 1 2 3 4 j = 4 if ( 22 > 11) true 11 44 55 33 22 0 1 2 3 4 Cont. i = 1 j = 2 if ( 44 > 55) j = 3 if ( 44 > 33) false true j = 4 if ( 33 > 22) true 11 44 55 33 22 0 1 2 3 4 11 33 55 44 22 0 1 2 3 4 11 22 55 44 33 0 1 2 3 4 Cont i = 2 j = 3 if ( 55> 44) j = 4 if ( 44 > 33) true true 11 22 55 44 33 0 1 2 3 4 11 22 44 55 33 0 1 2 3 4 11 22 33 55 44 0 1 2 3 4 Cont i = 3 j = 4 if ( 55 > 44) true 11 22 33 55 44 0 1 2 3 4 11 22 33 44 55 0 1 2 3 4 Go to program Bubble sort Insertion sort *j Passing array to a function 24 34 12 44 56 17 0 1 2 3 4 5 500 504 508 512 516 520 500 i 0 element = 24 Program output 504 508 1 element = 34 element = 12 2 512 3 element = 44 516 4 element = 56 520 element = 17 5 524 6 Go to program Example functions void display(int *p, int size); void sort_ascending (int *p, int size); void sort_descending (int *p,

TỪ KHÓA LIÊN QUAN
Đã 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.