Department of Computer Science COMP The Programming Competency Test

Similar documents
Operators. Java operators are classified into three categories:

1 Lexical Considerations

Operators in C. Staff Incharge: S.Sasirekha

SECTION II: LANGUAGE BASICS

Lexical Considerations

Lexical Considerations

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

easel LANGUAGE REFERENCE MANUAL

Information Science 1

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Chapter 2 Working with Data Types and Operators

Unit 3. Operators. School of Science and Technology INTRODUCTION

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

The PCAT Programming Language Reference Manual

L-System Fractal Generator: Language Reference Manual

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Computers Programming Course 6. Iulian Năstac

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

(Not Quite) Minijava

CSCE 120: Learning To Code

CS111: PROGRAMMING LANGUAGE II


Hava Language Technical Reference

6.001 Notes: Section 4.1

CS 320: Concepts of Programming Languages

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Spoke. Language Reference Manual* CS4118 PROGRAMMING LANGUAGES AND TRANSLATORS. William Yang Wang, Chia-che Tsai, Zhou Yu, Xin Chen 2010/11/03

Language Reference Manual simplicity

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Sprite an animation manipulation language Language Reference Manual

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

2.2 Syntax Definition

Visual C# Instructor s Manual Table of Contents

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

(Refer Slide Time: 4:00)

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation


age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

Operators & Expressions

Lecture 6: Arithmetic and Threshold Circuits

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

Review of the C Programming Language for Principles of Operating Systems

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Review of the C Programming Language

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

Lesson 3: Basic Programming Concepts

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Chapter 12 Variables and Operators

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Algorithmic "imperative" language

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Types and Static Type Checking (Introducing Micro-Haskell)

OUTLINE. Number system. Creating MATLAB variables Overwriting variable Error messages Making corrections Entering multiple statements per line

Introduction to Programming Using Java (98-388)

A Short Summary of Javali

EXPRESSIONS AND ASSIGNMENT CITS1001

Chapter Goals. Contents LOOPS

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

C/C++ Programming for Engineers: Working with Integer Variables

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

CGS 3066: Spring 2015 JavaScript Reference

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

DaMPL. Language Reference Manual. Henrique Grando

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Chapter 3: Operators, Expressions and Type Conversion

An algorithm may be expressed in a number of ways:

Full file at

Lecture 2: SML Basics

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

LECTURE 17. Expressions and Assignment

Constructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo

YOLOP Language Reference Manual

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

This book is licensed under a Creative Commons Attribution 3.0 License

CS4215 Programming Language Implementation. Martin Henz

Lecture 3 Operators MIT AITI

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Information Science 1

Bits, Words, and Integers

Mirage. Language Reference Manual. Image drawn using Mirage 1.1. Columbia University COMS W4115 Programming Languages and Translators Fall 2006

Typescript on LLVM Language Reference Manual

JAVA OPERATORS GENERAL

Programming in C++ 5. Integral data types

Mathematically Rigorous Software Design Review of mathematical prerequisites

Lecture 2: Big-Step Semantics

Chapter 12 Variables and Operators

SCoLang - Language Reference Manual

COSC 122 Computer Fluency. Iteration and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Transcription:

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.