1 / 42

CS1010E Programming Methodology Tutorial 6 Arrays and Search

CS1010E Programming Methodology Tutorial 6 Arrays and Search. C14,A15,D11,C08,C11,A02. Arrays. Homogenous Collection of Data Declaration: TYPE name [ NumberofElement > 0] int scores[10]; char name[5]; double prices[7]; etc Initialization: Initialize before use !!!!

yetta-chan
Télécharger la présentation

CS1010E Programming Methodology Tutorial 6 Arrays and Search

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. CS1010E Programming MethodologyTutorial 6Arrays and Search C14,A15,D11,C08,C11,A02

  2. Arrays • Homogenous Collection of Data • Declaration: • TYPE name [NumberofElement > 0] • int scores[10]; char name[5]; double prices[7]; etc • Initialization: • Initialize before use !!!! • Initializing each element separately • for(i=0;i<10;i++) arr[i] = something; • Initializing array at the time of declaration • int a[] = {1, 2, 3,4}; • What if int a[10] = {1,2,3} ?;

  3. Arrays • Access by index: • a[10], a[0] etc. • Access by address: • *(a+ 10) Why? • Array Specials : • Array Name: • stores the address of the first element • Array Length: • Always keep the length of array!! This is vital! • Array is pass-by-reference • Why & how ? • No variable size: • inti = 10; inta[i]; IS WRONG!! What is the output? What is the output?

  4. Question 1 • printArray(list1, 5); • 11 22 33 44 55 list1 list2

  5. Question 1 • passElement(list1[0]); • printArray(list1, 5); • 11 22 33 44 55 list1 list2

  6. Question 1 • changeElements(list2); • printArray(list2, 5); • ?? list1 list2 list[2] list[4]

  7. Question 1 • changeElements(list2); • printArray(list2, 5); • 99 99 77 99 88 list1 list2 list[2] list[4]

  8. Question 1 • copyArray(list2, list1, 5) ); • printArray(list2, 5); • ??? list1 list2

  9. Question 1 • copyArray(list2, list1, 5) ); • printArray(list2, 5); list1 list2 List1[0] List2[0]

  10. Question 1 • copyArray(list2, list1, 5) ); • printArray(list2, 5); list1 list2 List1[1] List2[1]

  11. Question 1 • copyArray(list2, list1, 5) ); • printArray(list2, 5); list1 list2 List1[2] List2[2]

  12. Question 1 • copyArray(list2, list1, 5) ); • printArray(list2, 5); list1 list2 List1[3] List2[3]

  13. Question 1 • copyArray(list2, list1, 5) ); • printArray(list2, 5); • 11 22 33 44 55 list1 list2 List1[4] List2[4]

  14. Question 1 • printPattern(list1,5); • ??? list1 list2

  15. Question 1 • printPattern(list1,5); • printArray(list+4, 1) • 55 list1 list2 list+ 5 – i = list + 4 i = 1

  16. Question 1 • printPattern(list1,5); • printArray(list+3, 1) • 44 55 list1 list2 list+ 5 – i = list + 3 i = 2

  17. Question 1 • printPattern(list1,5); • printArray(list+2, 1) • 33 44 55 list1 list2 list+ 5 – i = list + 2 i = 3

  18. Question 1 • printPattern(list1,5); • printArray(list+1, 1) • 22 33 44 55 list1 list2 list+ 5 – i = list + 1 i = 4

  19. Question 1 • printPattern(list1,5); • printArray(list+0, 1) • 11 22 33 44 55 list1 list2 list+ 5 – i = list + 0 i = 5

  20. Question 3 (a) • FindIndex(int list[], intnumElem, int key) • Return the position where the key should be inserted • Algorithm I : • Scan the list, [0..numElem -1] • Find the first element whose value is greater than key • if(list[k] key) continue • if(list[k] > key) break; • Return the position of that element key Stops, return 4

  21. Question 3 (a) • Translate Algorithm I into Code: Direct Translate Cleaner version

  22. Question 3 (b) • shiftRight(int list[], int start, int n) • Right shift 1 position in the range of [ start, start + n -1] • Right most element will become the first one • Algorithm II: • Store the last element • tmp = list[start+n-1] • Reverse Scan list start+n-1  start +1 • list[k] = list[k-1] • Set start element to • list[start] = temp;

  23. Question 3(b) • Algorithm II: • Store the last element • tmp = list[start+n-1] • Reverse Scan list start+n-1  start +1 • list[k] = list[k-1] • Set start element to • list[start] = temp; Start = 1, n = 4 Start + n -1 = 4 [1, 4] tmp = 44

  24. Question 3(b) • Algorithm II: • Store the last element • tmp = list[start+n-1] • Reverse Scan list start+n-1  start +1 • list[k] = list[k-1] • Set start element to • list[start] = temp; Start = 1, n = 4 [1, 4] tmp = 44

  25. Question 3(b) • Algorithm II: • Store the last element • tmp = list[start+n-1] • Reverse Scan list start+n-1  start +1 • list[k] = list[k-1] • Set start element to • list[start] = temp; Start = 1, n = 4 [1, 4] tmp = 44

  26. Question 3(b) • Algorithm II: • Store the last element • tmp = list[start+n-1] • Reverse Scan list start+n-1  start +1 • list[k] = list[k-1] • Set start element to • list[start] = temp; Start = 1, n = 4 [1, 4] tmp = 44 Stops scan

  27. Question 3(b) • Algorithm II: • Store the last element • tmp = list[start+n-1] • Reverse Scan list start+n-1  start +1 • list[k] = list[k-1] • Set start element to • list[start] = temp; Start = 1, n = 4 [1, 4] tmp = 44

  28. Question 3 (b) • Translate Algorithm II into Code Code for Algorithm II

  29. Question 3 (c) • InsertionSort(int list[], int n) • Sort elements in list in increasing order • Algorithm III: • Scan list from [1, n-1] • Find insertion position • Index = findIndex(k + 1, list[k]); • Insert list[k] to that position • rightshift(index, k-index+1) When scanning list[k],list[0…k-1] is sorted

  30. Question 3 (c) • Algorithm III: • For element list[k] • Find insertion position • Index = findIndex(k + 1, list[k]); • Insert list[k] to that position • rightshift(index, k-index+1) When scanning list[k],list[0…k-1] is sorted list list[0…3] is sorted list[4]

  31. Question 3 (c) • Algorithm III: • For element list[k] • Find insertion position • Index = findIndex(k + 1, list[k]); • Insert list[k] to that position • rightshift(index, k-index+1) list Insertion position list[0…3] is sorted list[4] Index = 2

  32. Question 3 (c) • Algorithm III: • For element list[k] • Find insertion position • Index = findIndex(k + 1, list[k]); • Insert list[k] to that position • rightshift(index, k-index+1) list Insertion position Rightshift[2, 3] Index = 2 list[4]

  33. Question 3 (c) • Algorithm III: • For element list[k] • Find insertion position • Index = findIndex(k + 1, list[k]); • Insert list[k] to that position • rightshift(index, k-index+1) list list[0, 4] is sorted move to list[5]

  34. Question 3 (c) • Translate Algorithm III into code: Code for Algorithm III

  35. Thank you See you next week!!

More Related