1 / 29

Procedures and coding

Procedures and coding. Variable assignment, iterative procedures and control structures. Al-Khwarizmi. Decimal to binary conversion. The table of values, at left, was generated by a executing a function in a computer language (J or Matlab).

Télécharger la présentation

Procedures and coding

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. Procedures and coding Variable assignment,iterative procedures and control structures Al-Khwarizmi

  2. Decimal to binary conversion • The table of values, at left, was generated by a executing a function in a computer language (J or Matlab). • This function will have a name and a syntax for the function call and given the inputs 81.65 and the number 10 for the number of binary places, it will deliver the binary representation 1010001.1010011001by means of some procedure (or algorithm).

  3. The algorithm – the things that need to be done • Find the largest power of 2 required. • Calculate the corresponding power of 2 • Record 1 in the binary expansion, stored as character data. • Subtract the power from the number. • Check if the next power down is less than the remaining number – record a 1 in the binary expansion if it is, 0 if not. • Subtract the power or 0 from the number. • Return to step 5 repeat 5, 6 and 7 until the correct number of entries in the binary expansion have been obtained. • Insert the binary point.

  4. “Variables” are names for values that change y • In the table, the columns are marked with the names of variables. • n is one of a list of exponents • p is one of a list of powers of 2 • b is one of a list of Booleans (0 or 1) • n – p x b is one of a list of remainders, generated iteratively i.e. by a repetitive process from the values of the other variables. Let us call this variable y. • Since the values of n, p and b change with every iteration (repetition), we must write the steps of an algorithm in terms of variables, i.e. algebraically.

  5. The iterative procedure in detail y • We start each iteration by decreasing the value of n by 1; then: • calculate 2n ; • test2n < y – variable b is given this value, the result (0 or 1) of a binary decision; • compute a new value for y from the current values of n, p and b ; (then return to step 1).

  6. Defining the function • Let us call the user defined function that returns the binary representation of a decimal to any given number of binary places • d2b (every function must have a name) • The computer language we use, might allow a choice of syntax, for example in J one could have : p d2b y (or y d2b p ) but in Matlab you can only have the pre-fix syntax d2b(y,p) or d2b(p,y)– where p represents the number of binary places and y the decimal to be converted to binary. • In J one could also have d2b z where z represents a pair of numbers (either p,y or y,p).

  7. Documentation is important • Clearly every user defined function needs documentation that describes what the function does, what it’s syntax is and what the input variables (or function arguments) are. • The instructions for a user defined function are generally held in text files laid out according to given rules which the computer language can interpret and convert to machine code.

  8. Writing and storing instructions for a computer requires a computer language • BUT: • we will write commands in a combination of standard algebraic notation and some special conventions, which we will call (our) pseudo-code. You must stick to these conventionsif this pseudo-code is to make any sense at all.

  9. Declaration • The first thing that must appear in the text file that stores the definition of a function is a declaration that this file is just that, a function definition. • In Matlab each function has a separate file and begins with the line, something like: • function B = d2b(p,y) • In J many functions can be defined in the same file, and one way of starting a function definition is: • d2b =: 3 : 0where 3 is a code for a function and the 0 indicates that the lines following are the instructions.

  10. Pseudo-code convention • For our pseudo-code we will start a function definition with what we call the syntax line. It will look like this: • [0] B = p d2b yof course, you can choose some other syntax likeB = d2b(y,p) or even a post-fix syntax since this is an entirely imaginary computer language.

  11. Comment lines indicated by NB. • The syntax line is followed by comment lines documenting the function. For example: • [0.1] NB. d2b finds the binary representation of a decimal to any given number of binary places with the function call p d2b y • [0.2] NB. Input p is the number of binary places required • [0.3] NB. Input y is the decimal converted to binary • [0.4] NB. Output B is the binary representation of y to p binary places • In J the comment symbol is NB. • In Matlab the comment symbol is % • A comment symbol is like a pre-fix function that returns nothing.

  12. The output • In J the output is whatever is computed on the last line of the algorithm, but in Matlab it is whatever is stored in variableB, given in the Matlab syntax line: function B = d2b(p,y) • when all instructions have beencompleted. • We adopt the Matlab convention for our pseudo-code. Given the syntax line: • [0] B = p d2b ywhatever is in B when the algorithm has finished executing is the output.

  13. Where does the output go? • With interpreters the output is sent to the screen by default, but the output can also be stored in a variable (see later about assignment). • If it is sent to the screen it is usually discarded (although Matlab stores it in variable ans until the next line is executed). If it is stored in a variable it may or may not be displayed as well. • In compiled languages you have to give an instruction to print the output.

  14. Implementations of a plus function • In Matlab the following lines are saved into a file called plus.m • function r = plus(a,b) % …. [adequate documentation] • r = a + b • In J the following lines need to be executed plus =: 3 : 0 x + y NB. [… documentation needed] • ) • In our pseudo-code: • [0] z = c plus d • [0.1] NB. […documentation] • [1] z  c + d

  15. Storing data temporarily in memory • Computer systems allow us to store lists of characters or numbers under names that we can assign. We call these variable names but they are like the common pronouns: he, she, it, they etc. They “point” to data locations like pronouns point to things. • Storing a single character is not much use. Usually a list or string of characters i.e. text is required. • Similarly a single number is often not enough but whole lists of (related) numbers are required e.g. assignment and exam marks. (Sometimes numeric lists are called vectors.)

  16. Storing data under variable names • The operation of storing a character (or a list of characters) or a number (or list of numbers) in some temporary location that is referred to by a name is represented, in our pseudo-code (and in APL) by the assignment arrow  • Name  number/character (or list)for example: • x  4temperatures  43.5 34.7 23.1 28.4 30.5address  '64 Sumner Street' • [Single quotes, '…', indicate that between them is a list of characters]

  17. Storing data under variable names • We can do things to x and temperatures and address independent of the actual numbers or characters that they represent. They are called (assigned) variables.Computer Algebra Systems allow the algebraic manipulation of (algebraic) variables that do not have values assigned to them.

  18. Assignment and equality • In Matlab = is used for the assignment arrow, so to test whether two things are the same or not one must use the symbol = = • In J =: is used for the assignment arrow since J only uses symbols on the ASCII keyboard for the names of primitive functions. Equality is tested by = • In pseudo-code we will use  for assigning values to variables and = for testing equality.

  19. Find the largest power of 2 required. Calculate the corresponding power of 2 Record 1 in a list of bits Subtract the power from the number. Calculate the next power down Check if less than the remaining number Record a 1 in the binary expansion if it is, 0 if not. Subtract the power or 0 from the number. Return to step 5 repeat 5, 6 and 7 until the correct number of entries in the binary expansion have been obtained. Convert Boolean list to character data and insert the binary point. [0] B = p d2b y [0.1] NB.d2b finds the binary representation of a decimal to any given number of binary places with the function call p d2b y [0.2] NB.Input p is the number of binary places required [0.3] NB. Input y is the decimal converted to binary [0.4] NB.Output B is the binary form of y to p binary places [ 1] N  { largest power } [ 2] w  2 ^ N [ 3] b  1 NB. w < y is 1 [ 4] B  1 NB. B will be the list of b’s [ 5] y  y – b * w [ 6] N  N – 1 [ 7] w  2 ^ N [ 8] b  w < y [ 9] B  B, b NB. Tack b onto B [10] y  y – b * w { repeat [5],[6],[7],[8] and [9] } Function listing

  20. Computing the largest power of 2 less than any given number • We are looking for the number N such that, for some given number M 2N < M or 2N = M but 2N+1 > M • If we could find the number x for which 2x = M then N would either be x or the biggest integer (whole number) less than x, because the values of 2x increase as x increases. • Now 2x = M if x log 2 = log M or x = (log M) / (log 2)

  21. The floor function • A function called floor, or something similar, is found in all computer languages, spreadsheets etc., to return the integer part of a number, i.e. the largest integer less than any particular input number. • It’s syntax will usually be a pre-fix one: floor(u) for example, floor(2.3) should return 2; floor(8.9) should return 8 and floor(-5.7) should return -6. • So floor(log(M)/log(2))will return the value of the largest power of 2 less than M

  22. Calling functions within a function • [0] B = p d2b y[0.1] NB. d2b finds the binary representation of a decimal to any given number of binary places with the function call p d2b y • [0.2] NB. Input p is the number of binary places required • [0.3] NB. Input y is the decimal converted to binary • [0.4] NB. Output B is the binary form of y to p binary places • [1] N  floor( log(y) / log(2)) • [2] w  2 ^ N • [3] B  b  1 NB. Not every language will permit this (J does) • [4] y  y – b * w • [5] N  N – 1 • [6] w  2 ^ N • [7] b  w < y • [8] B  B , b NB. Tack (concatenate) b onto B • [9] y  y – b * w • [10]{ Repeat [5],[6],[7],[8] and [9] UNTIL .?. or a given number of times } • [11] { Convert list to character and insert binary point.}

  23. Control structures for iteration • We use the numbering in our pseudo-code to indicate that control passes from one line to the next according to the numbering. The control line is the line being executed by the computer – like the execution line in an interpreter. • We use sub-numbering to indicate blocks of code executed at a particular point before control passes to the next line. For iterative processes we look at two control structures, the for loop and the while loop.

  24. for loops • for loops are used when we know how many times a particular set of operations must be repeated. For example, in our d2b code we know that we have to repeat steps [5] to [9] exactly N+p times. • Our pseudo-code convention is: • [5] for k = 1 to k = N+p • [5.1] N  N – 1 • [5.2] w  2 ^ N • [5.3] b  w < y • [5.4] B  B , b • [5.5] y  y – b * w • [6] { Convert list to character and insert binary point.}

  25. while loops • while loops are used when we don’t know how many times a particular set of operations must be repeated but we can compute a condition which, while satisfied, indicates another pass through the loop should be made. For example, in our d2b code we know that we have to repeat steps [5] to [9] while N > -(p+1) . Our pseudo-code convention is: • [5] while N > – (p+1) NB. This is a binary decision • [5.1] N  N – 1 • [5.2] w  2 ^ N • [5.3] b  w < y • [5.4] B  B , b • [5.5] y  y – b * w • [6] { Convert list to character and insert binary point.}

  26. Delegating jobs (functions) • The final step of the algorithm can be delegated to another function which converts a list to a character string with a point inserted at a particular place. • For example • 5 List2char 1 0 1 0 1 1 0 0 1 0 1 • would return the character string 101011.00101 • In order to define this function we need to know what data manipulating functions our computer language provides. This varies greatly with the data types available in particular languages.

  27. Storing data in files • Data stored temporarily in the memory of a computer as it runs some software program is lost when the program is closed unless it is saved in a permanent storage (on disc, tape, etc.) in computer files. • All software will provide a save function to permanently store character (text) and numeric data and possibly user defined programs (macros). There may be special formats (additional information) stored with the data and specified file extensions (e.g.notes.txt , letter.doc , compute.m , work.ijs , pix.jpg , song.wav) for particular types of data.

  28. We assume that the text of our pseudo-code can be saved and recalled and run in our pseudo world • [0] B = p d2b y[0.1] NB. d2b finds the binary representation of a decimal to any given number of binary places with the function call p d2b y • [0.2] NB. Input p is the number of binary places required • [0.3] NB. Input y is the decimal converted to binary • [0.4] NB. Output B is the binary form of y to p binary places • [1] N  floor( log(y) / log(2)) • [2] w  2 ^ N • [3] B  b  1 • [4] y  y – b * w • [5] while N > – (p+1) NB. This is a binary decision • [5.1] N  N – 1 • [5.2] w  2 ^ N • [5.3] b  w < y • [5.4] B  B , b • [5.5] y  y – b * w • [6] B  p List2char B Of course, it would be better if we could actually see it work in the real world.

  29. What’s the big idea? • Variables store values – assignment is absolutely crucial to computing. (Storage capacity is what the abacus didn’t have.) • Storing instructions – function listings – different in different environments (hence pseudo-code) but always algebraic in respect to variables and with functions calling other functions algebraically, i.e. by name.Every function has a name, a particular syntax for a function call and an unambiguous output obtained from its algorithm for given inputs. • Control structures – for and while loops for iterative procedures. • Saving data and function listings to disc for permanent storage.

More Related