The Australian National University Faculty of Engineering & Information Technology Department of Computer Science COMP1120-2003-01 The Programming Competency Test 1 Introduction The purpose of COMP1120 is to give a fast-track presentation of the material in the first-year courses, COMP1100 and COMP1110, for those with demonstrable programming skills. This document makes clear what we mean when we say that competence in programming is required. 2 The Required Concepts Each of the following words and phrases is associated with concepts that a student with the requisite skills should have. The depth of knowledge expected is that of being able to describe what each term means, to say what the connections between them are and to be able to read and understand programs based on these ideas. 2.1 Syntactic Terminology reserved words expression, binary operator, unary operator, operator precedence, variable, identifier, integer constant, boolean constant, array element, function call statement, assignment, conditional, loop, procedure call, compound statement declaration, scope, procedure and function definition, parameters, formal versus actual parameters, local variable, routine body program 2.2 Notions Related to Execution assignment, state of execution, array element, array access arithmetic operations: addition (+), subtraction ( ), multiplication ( ), integer division (div) logical operations: and, or and not sequence, conditional, iteration, routine invocation, initialization 1
COMP1120-2003-01 2 3 The Assessment Process When prospective COMP1120 students are tested it will be on the basis of being able to state what happens when some given programs are executed. Although it would be nice if an understanding of the supplied programs only required the very general concepts mentioned above, these programs have to be written down. Therefore, a specific algorithmic notation needs to be agreed on. The programming language, salty, described mostly by example below, is one that is limited to expressing just the ideas that we deem to be necessary (though not sufficient) for a competent coder. 4 SALTY (Simple Algorithmic Language for Testing You) Because the constructs of this language are minor variations of the corresponding ones in most common conventional languages (such as C, Eiffel, Java, Perl, Python), the syntax of salty should be inferred from short explanations of representative examples. 4.1 Expressions In the expression x+y z the identifiers x,y and z name distinct variables. The value of the whole expression is computed by adding the value of the variable x to the product of the values of the variables y and z. Computationally, x+y z is equivalent to x+(y z) rather than (x+y) z because the operator has higher precedence than +. As in normal mathematics, the expression x y+1 is equivalent to (x y)+1. For operators such as + and with the same precedence, evaluation proceeds from left to right. Multiplication and division also have the same precedence as each other. The boolean expression x+y z+1 (equivalent to (x+y) (z+1) ) evaluates to one of the two boolean values true or false in the standard way. The boolean expressions not b and c or d and ((not b) and c) or d are computationally equivalent. This indicates the relative precedence of the logical operators. The relative precedence of all the salty operators is captured in the following table:- 6 not 5, div 4 +, 3 =,, <, >,, 2 and 1 or In salty expressions the above operators combine values in standard ways. The values operated on, as illustrated above, are the values of simple variables, of constants, and of sub-expressions; they may also be the values of array elements and the results of applying functions to arguments. Evaluating the expression f(x+1)+g(a, b) is done by adding together the results of applying f and g to their respective argument values. In the case of f it is the result of computing x+1; in g s case, the two arguments are the values of a and b respectively.
COMP1120-2003-01 3 The expression A[j]+gcd(x, B[k]) adds two values the first is the value of an array element and the other is the result of applying the function called gcd to its two integer arguments. A and B are both arrays of integers. The elements of any array are either all integers or all booleans, deping on how the array is declared. A predefined function, called high, gives the value of the upper bound for the subscript of an array. It takes an array as its argument and returns a non-negative integer. The lower bound of all array subscripts is 0. 4.2 Statements Assignment statements are easily recognizable since the the left arrow symbol ( ) separates the variable being assigned to from the expression whose value it is that is assigned. In the assignment statement x x+1 the value of the variable x is incremented. The semicolon symbol is used in salty as a separator; in particular it is used to separate statements that are to be executed in sequence. We refer to a sequence of statements such as tmp x; x y; y tmp as a compound statement. Conditional statements come in just two flavours - those with one branch and those with two. A conditional statement with a single branch has the following form: if e then c where e is a boolean expression and c is a compound statement. A conditional statement with both branches has the following form: if e then c 1 else c 2 The only looping construct in salty is the whilestatement. It has the following form: while e do c The boolean expression, e, must be computed and found to be true before each execution of the body of the while statement. The execution of the statement finishes when the condition is computed and found to be false. 4.3 Procedures and Functions Functions are much the same as in most programming languages. They are routines that take a number of values and compute a value which is used in the evaluation of expressions. A function declaration is recognizable as such by the presence of the keyword function in its heading. As illustrated in the following example, a return statement is the means by which the result of a function call is indicated; of course, such results are restricted in salty to integers and boolean values.
COMP1120-2003-01 4 function gcd(i,j: integer): integer m i; n j; while m n > 0 do if m>n then m m n else n n m return m+n In salty we use the term procedure for a routine that is not a function; no value is returned by a procedure and each call of a procedure is a statement. The above example illustrates that routines may have local variables. No local variable in a routine may have the same name as any parameter of that routine. An identifier used in a routine must match a parameter of that routine, a local variable or, if neither of those apply, a global variable. Parameters for procedures and functions that are integers or booleans are passed by value; that is, the expressions in a function call that correspond to such parameters, are evaluated and a value is passed to the function. Parameters for procedures and functions that are arrays, on the other hand, are passed by reference; that is, the array mentioned in the call is the array that is used in the routine body. In a routine heading, the types of simple parameters (integers and booleans) are given in the form that is illustrated above. The type of a parameter which is an array is specified by postfixing the identifier with one of the notations array of integeror array of boolean. 4.4 Programs A salty program consists of declarations followed by a compound statement. Its form is illustrated by the following instance of a (very uninteresting) program: program sillyone var A: array [10] of integer; function even(i: integer): boolean return ((i div 2) 2) = i m 5; if even(m+1) then n 3. As illustrated above, variable declarations in both routines and the main program, commence with the reserved word var.
COMP1120-2003-01 5 The initial value of every variable is undefined. When an array is declared an upper bound for the subscript is specified. The lower bound is always 0. In the array A in the example above, the elements are numbered 0 to 10, inclusive. No input or output is specified for salty but you will be asked to say what the values of global variables are at the of execution of a program. In the above example, the values of m and n are 5 and 3, respectively. 5 A Short SALTY Quiz The following two questions indicate the sort of problems that will be posed on the entry quiz for COMP1120. 5.1 Question 1 Consider the following salty program: program question1 var A: array [10] of integer; m 0; while m<10 do A[m] m m; m m+1 m 1; n 0; while m<10 do n n+a[m] A[m 1]; m m+1. What is the final value of the variable n? 5.2 Discussion of Question 1 A programmer would be expected to recognize immediately that there are two loops, each of which is used to scan the array A. It should also be obvious that the first loop initializes ten array elements with the squares of the numbers from 0 to 9, while the second adds up the successive differences between each adjacent pair of array elements. You don t have to do much arithmetic to simplify the sum 0+(1-0)+(4-1)+(9-4)+(16-9)+(25-16)+(36-25)+(49-36)+(64-49)+(81-64) to get 81.
COMP1120-2003-01 6 5.3 Question 2 Consider the following salty program: program question2 var i,j: integer; function r(x,y: integer): integer return x ((x div y) y) var A: array [5] of integer; function f(x: array of integer): integer m 0; n 0; while m high(x) do if r(x[m], 3)>0 then n n+1 m m+1; return n i 0; while i 5 do A[i] r(i i, 7); i i+1; i 0; j 0; while i 5 do j j+f(a); A[i] A[i]+j; i i+1. 1. What is the value of the expression r(7,2)? 2. What is the final value of j? 5.4 Discussion of Question 2 The function r should be recognizable as giving the remainder on division, when the integer given as the first parameter is divided by the integer given as the second parameter. Hence the value of r(7,2) is 1. The function f counts the number of elements of A that are not divisible by 3. After the first loop in the program body has executed the elements of A will contain the numbers 0, 1, 4, 2, 2 and 4. Applying f to the array in this state gives 5. Successive values of j are 0, 5, 11, 16, 21, 26 and 31.
COMP1120-2003-01 7 6 What SALTY is NOT The Disclaimer salty is not a language for writing software or even for communicating algorithms. Although salty bears a resemblance to Pascal and Modula-2, it is not exactly a subset of either. salty is not a language that is rigorously defined anywhere. It is supposed to be selfexplanatory to people with programming experience. Both the language and this document are hot off the press so mistakes are quite possible. If anyone sees an example of a salty program that has multiple sensible interpretations, this is a matter of great interest to Malcolm Newey.