1 / 12

Language Independence

Language Independence. § 7.1 Structuring Data and Building Algorithms Building Algorithms: Key Concepts. Definition of an Algorithm. Algorithms describe the steps one needs to take in order to perform a certain computation. Thus… programs in different languages,

willem
Download Presentation

Language Independence

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. Language Independence §7.1 Structuring Data and Building Algorithms Building Algorithms: Key Concepts

  2. Definition of an Algorithm Algorithms describe the steps one needs to take in order to perform a certain computation. Thus… programs in different languages, although looking different, may execute the same algorithm, … if they follow the same steps.

  3. Eg. One Algorithm: Different Expressions search array J O Y C E Writing an Algorithm to Search for a Character (i.e. Y) in Array

  4. Expressed in English search • In order to search for a particular character in an array, we need to know the array, the size of the array, and the character we are searching for • Then, we start by setting a variable, i, to the beginning of the array. We check to see if the character in the array at location i matches the character we are looking for. If it does not, we increment i and loop. If it is, then we return i • If we arrive at the end of the array because i has been incremented until it is the size of the array, we return a value to indicate we did not find the character in the array

  5. Expressed in Chinese search • 為了在陣列中尋找特定的字元,我們必須知道陣列和陣列的大小和字元 • 然後我們設定依個參數i, 開始這個陣列. 我們檢查如果這個字元是否符合我們尋找的,若不符合則i+1進行回圈,若符合則return i • 若i到達此陣列的大小則結束,我們return一個值去指出我們沒找到的陣列的字元

  6. Expressed in Pseudo-Code (a) search function search (a: array, size: integer, key: character): integer loop i from 1 to size if ai = key then return(i) end loop return (-1) {i.e. not found} end function

  7. Expressed in Pseudo-Code (b) search Algorithm: SearchCharacter Purpose: To search for a character in an array. Pre: an existing array of characters, size of the array, character for which to search Post: none Return: position of character in array (or -1 if not) Description: int SearchCharacter (array ,size, key){ index = 0; while(index < size){ if (a[index]==key)return(index); index++; } return(-1); }

  8. Expressed in BASICA search algorithm 1000 REM Before this point, 1010 REM A should be set to the input array 1010 REM SIZE should be set to the size of the array 1020 REM KEY should be set to the key character 1030 REM The return value is stored in RETVAL 1030 FOR I = 1 TO SIZE 1040 IF A(I) = KEY THEN GOTO 1070 1050 NEXT 1060 I = -1 1070 RETVAL = I

  9. Expressed in ANSI-C search algorithm int find (char a[], int size, int key){ int i; for(i=0; i<size; i++) if(a[i]==key)return (i); return(-1); }

  10. Machine Code & Assembly (ASS) ; find a character in memory, return pos ; assume array starts at M1D, location in R1 ; assume the memory contains the character 06 11EF LOAD R0  EF ; Get item from keyboard 07 3310 LADD R3  &R1 ; Get next array element 08 A100 INC R1++ ; Advance pointer 09 D307 JUMP PC=M07 ; If no match, try again 1A B100 DEC R1-- ; Lower Index 1B 2FF1 SAVE FF  R1 ; Print Memory Location 1C 0000 HALT ASS Assembly Language Specifications

  11. The Bottom Line an algorithm may be difficult to implement because language constraints ! • An algorithm is still the same algorithm even when expressed in a different language • Different algorithms can perform the same task • Some algorithms may be incompatible with some languages (eg. a recursive algorithm in an non-recursive language)

  12. Next Up: Quality

More Related