Download presentation
Presentation is loading. Please wait.
Published byBertha Bradley Modified over 9 years ago
1
1 CSC103: Introduction to Computer and Programming Lecture No 19
2
2 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
3
3 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
4
4 Pointer arithmetic - program 3 i 560 *x 1.5 j 790 c k 320 *y *z 560 790 320 564 321 794 564 794 321 Go to program
5
5 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 243412445617 012345 500 504508512516520 121724344456 012345 500 504508512516520 564434241712 012345 500 504508512516520
6
6 Sorting techniques Selection sort Bubble sort Insertion sort
7
7 Selection sort
8
8 C program – Selection sort 4433552211 01234 i = 0 j = 1 if ( 44 > 33) 3344552211 01234 j = 2 if ( 33 > 55) true false j = 3 if ( 33 > 22)true 2244553311 01234 j = 4 if ( 22 > 11)true 1144553322 01234
9
9 Cont. i = 1 j = 2 if ( 44 > 55) j = 3 if ( 44 > 33) false true j = 4 if ( 33 > 22)true 1144553322 01234 1133554422 01234 1122554433 01234
10
10 Cont.. i = 2 j = 3 if ( 55> 44) j = 4 if ( 44 > 33) true 1122554433 01234 1122445533 01234 1122335544 01234
11
11 Cont… i = 3 j = 4 if ( 55 > 44) true 1122335544 01234 1122334455 01234 Go to program
12
12 Bubble sort
13
13 Insertion sort
14
14 *j Passing array to a function 243412445617 012345 500 504508512516520 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
15
15 Example functions void display(int *p, int size); void sort_ascending (int *p, int size); void sort_descending (int *p, int size);
16
16 Array and pointer int num[ ] = { 24, 34, 12, 44, 56, 17 } ; Mentioning the name of the array we get its base address by saying *num we would be able to refer to the zeroth element of the array, *num and *( num + 0 ) both refer to 24 243412445617 012345 500 504508512516520 that is num 500 that is, *(500) = 24
17
17 Cont. *( num + 1 ) refer the first element of the array This means that all the following notations are same num[i] *( num + i ) *( i + num ) Go to program
18
18 Two Dimensional Arrays It is also possible for arrays to have two or more dimensions For example you want to input roll no and mark of 4 students. Since we know 1D array, so for this problem we can have two 1D array – One for roll number – Second for marks One 2D array can be used to store these values
19
19 Example program 10 0 1 2 3 rowcolumn Roll Number Marks stud[0][0] stud[0][1] stud[1][0] stud[1][1] stud[2][0] stud[2][1] stud[3][0] stud[3][1] 10 85 12 65 13 89 14 92 Go to program
20
20 Initializing a 2-Dimensional Array
21
21 Example program Write a program that adds two 4 x 4 matrices. 3561 5329 10-34 723-9 42-70 5731 630 89-23 + 771 10 5 73-44 15111-6 = Write a program
22
22
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.