CS103 COMPUTER PROGRAMMING LECTURE 2 Hafiza Maria Maqsood

  • Slides: 11
Download presentation
CS-103 COMPUTER PROGRAMMING LECTURE 2 Hafiza Maria Maqsood FAST NU – CHINIOT FSD campus

CS-103 COMPUTER PROGRAMMING LECTURE 2 Hafiza Maria Maqsood FAST NU – CHINIOT FSD campus Hafiza Maria Maqsood, FAST-NU, Chiniot FSD Campus 1

Linear Search and Binary Search array for a key value Linear search Compare each

Linear Search and Binary Search array for a key value Linear search Compare each element of array with key value Start at one end, go to other Useful for small and unsorted arrays Inefficient If search key not present, examines every element

Linear Search and Binary Search Binary search Only used with sorted arrays Compare middle

Linear Search and Binary Search Binary search Only used with sorted arrays Compare middle element with key If equal, match found If key < middle Repeat search on first half of array If key > middle Repeat search on last half Very fast 30 element array takes at most 5 steps

Binary seach algorithm Int binarysearch( int *arr, int size, int val) { Int low

Binary seach algorithm Int binarysearch( int *arr, int size, int val) { Int low = 0, high = size -1, mid; While (high >=low) { mid = (low+high)/2; if(val = = arr[mid]) return 1; Else if(arr[mid] < val) low = mid +1; Else high = mid – 1; } return 0; } Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus 4

Linear search algorithm bool l_search ( int *list, int size, int key, int*& rec

Linear search algorithm bool l_search ( int *list, int size, int key, int*& rec ) { // Basic sequential search bool found = false; int i; for ( i = 0; i < size; i++ ) { if ( key == list[i] ) break; } if ( i < size ) { found = true; rec = &list[i]; } return found; 5 Hafiza } Maria Maqsood, FAST-NU, Chiniot - FSD Campus

Sorting Arrays Sorting data Important computing application Virtually every organization must sort some data

Sorting Arrays Sorting data Important computing application Virtually every organization must sort some data Massive amounts must be sorted Bubble sort (sinking sort) Several passes through the array Successive pairs of elements are compared If increasing order (or identical), no change If decreasing order, elements exchanged Repeat these steps for every element

Storage Classes Variables have attributes Have seen name, type, size, value Storage class How

Storage Classes Variables have attributes Have seen name, type, size, value Storage class How long variable exists in memory Scope Where variable can be referenced in program

Storage Classes Automatic storage class Variable created when program enters its block Variable destroyed

Storage Classes Automatic storage class Variable created when program enters its block Variable destroyed when program leaves block Only local variables of functions can be automatic Automatic by default keyword auto explicitly declares automatic register keyword Hint to place variable in high-speed register Good for often-used items (loop counters) Often unnecessary, compiler optimizes Specify either register or auto, not both register int counter = 1;

Storage Classes Static storage class Variables exist for entire program For functions, name exists

Storage Classes Static storage class Variables exist for entire program For functions, name exists for entire program May not be accessible, scope rules still apply (more later) static keyword Local variables in function Keeps value between function calls Only known in own function extern keyword Default for global variables/functions Globals: defined outside of a function block Known in any function that comes after it

Scope Rules Scope Portion of program where identifier can be used File scope Defined

Scope Rules Scope Portion of program where identifier can be used File scope Defined outside a function, known in all functions Global variables, function definitions and prototypes Function scope Can only be referenced inside defining function Only labels, e. g. , identifiers with a colon (case: )

Scope Rules Block scope Begins at declaration, ends at right brace } Can only

Scope Rules Block scope Begins at declaration, ends at right brace } Can only be referenced in this range Local variables, function parameters static variables still have block scope Storage class separate from scope Function-prototype scope Parameter list of prototype Names in prototype optional Compiler ignores In a single prototype, name can be used once