1 / 22

CSC103: Introduction to Computer and Programming

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.

nura
Download Presentation

CSC103: Introduction to Computer and Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC103: Introduction to Computer and Programming Lecture No 19

  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. 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. Pointer arithmetic - program 564 560 i k j c 1.5 *x 3 320 560 790 794 321 564 794 790 *y 321 *z 320 Go to program

  5. Sorting program 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 12 24 56 34 44 17 12 34 24 34 24 44 44 17 56 12 17 56 500 500 500 504 504 504 508 508 508 512 512 512 516 516 516 520 520 520 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

  6. Sorting techniques Selection sort Bubble sort Insertion sort

  7. Selection sort

  8. C program – Selection sort i = 0 j = 1 true if ( 44 > 33) j = 2 if ( 33 > 55) false j = 3 if ( 33 > 22) true 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 22 33 44 11 44 33 44 44 55 55 55 55 33 22 22 33 11 22 11 11 j = 4 if ( 22 > 11) true

  9. Cont. i = 1 j = 2 false if ( 44 > 55) j = 3 if ( 44 > 33) true j = 4 if ( 33 > 22) true 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 11 11 11 44 33 22 55 55 55 33 44 44 22 22 33

  10. Cont.. i = 2 j = 3 true if ( 55> 44) j = 4 if ( 44 > 33) true 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 11 11 11 22 22 22 44 33 55 44 55 55 33 33 44

  11. Cont… i = 3 j = 4 true if ( 55 > 44) 0 0 1 1 2 2 3 3 4 4 11 11 22 22 33 33 55 44 44 55 Go to program

  12. Bubble sort

  13. Insertion sort

  14. Passing array to a function *j 504 500 524 512 520 516 508 0 1 2 3 4 5 5 1 6 0 3 2 i 4 Program output 24 34 12 44 56 17 element = 24 element = 34 500 504 508 512 516 520 element = 12 Go to program element = 44 element = 56 element = 17

  15. Example functions void display(int *p, int size); void sort_ascending (int *p, int size); void sort_descending(int *p, int size);

  16. Array and pointer that is num 500 0 1 2 3 4 5 that is, *(500) = 24 24 34 12 44 56 17 500 504 508 512 516 520 int num[ ] = { 24, 34, 12, 44, 56, 17 } ; Mentioning the name of the array we get its base address by saying *numwe would be able to refer to the zeroth element of the array, *num and *( num + 0 ) both refer to 24

  17. Cont. Go to program • *( num + 1 ) refer the first element of the array • This means that all the following notations are same • num[i] • *( num + i ) • *( i + num )

  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. Example program row column 0 1 0 10 85 1 65 12 stud[3][1] stud[2][1] stud[0][1] stud[1][1] stud[2][0] stud[3][0] stud[1][0] stud[0][0] 2 13 89 3 14 92 Go to program Roll Number Marks

  20. Initializing a 2-Dimensional Array

  21. Example program = + Write a program Write a program that adds two 4 x 4 matrices.

More Related