PROGRAMMING for EOR (346022) 7-Jul-2011

Similar documents
COS 126 General Computer Science Spring Written Exam 1

Module 16: Data Flow Analysis in Presence of Procedure Calls Lecture 32: Iteration. The Lecture Contains: Iteration Space.

Data Structures Lecture 3 Order Notation and Recursion

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher

CS13002 Programming and Data Structures, Spring 2005

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4)

COMPUTATIONAL PROPERIES OF DSP ALGORITHMS

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

In this article we will see how the OmniThreadLibrary can be used to split a simple image processing kernel across multiple threads.

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

C Curves That Fill Space

Lecture Notes on Dynamic Programming

CIS 110 Introduction to Computer Programming Summer 2017 Final. Recitation # (e.g., 201):

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

Adding Integers. Unit 1 Lesson 6

CS 161 Fall 2015 Final Exam

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Bentley Rules for Optimizing Work

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

ESC101 : Fundamental of Computing

COS 126 Exam Review. Exams overview Example programming exam Example written exam questions (part 1)

CS61B Lecture #35. [The Lecture #32 notes covered lectures #33 and #34.] Today: Enumerated types, backtracking searches, game trees.

ECE 563 Spring 2012 First Exam

WYSE Academic Challenge 2002 Computer Science Test (Sectional) SOLUTION

Tribhuvan University Institute of Science and Technology 2065

CS111: PROGRAMMING LANGUAGE II

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Solutions to Problem Set 1

Arrays III and Enumerated Types

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Exam Sample Questions CS3212: Algorithms and Data Structures

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

APCS Semester #1 Final Exam Practice Problems

CS 3410 Ch 5 (DS*), 23 (IJP^)

Data Structures and Algorithms Winter Semester

ECEN 615 Methods of Electric Power Systems Analysis Lecture 11: Sparse Systems

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Introduction to Algorithms

Algorithm Analysis. Jordi Cortadella and Jordi Petit Department of Computer Science

The Citizen s Guide to Dynamic Programming

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Lecture 13 Geometry and Computational Geometry

XVIII Open Cup named after E.V. Pankratiev Stage 1: Grand Prix of Romania, Sunday, September 17, 2017

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 4 Certificate in IT SOFTWARE DEVELOPMENT

Sorting. Weiss chapter , 8.6

Algorithm Analysis. Jordi Cortadella and Jordi Petit Department of Computer Science

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

Algorithms and Complexity. Exercise session 3+4

BITG 1113: Array (Part 1) LECTURE 8

ˆ The exam is closed book, closed calculator, and closed notes except your one-page crib sheet.

ENGR 1181 MATLAB 09: For Loops 2

ESC101N: Fundamentals of Computing End-sem st semester

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

Dynamic Programming Algorithms

Computer Science & Engineering 150A Problem Solving Using Computers

Data Structures CSci 1200 Test 1 Questions

The exam is closed book, closed calculator, and closed notes except your one-page crib sheet.

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Lecture Notes on Binary Search Trees

Total Score /1 /20 /41 /15 /23 Grader

CSE 101 Homework 5. Winter 2015

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

ESc101 : Fundamental of Computing

CMSC 131A, Midterm 1 (Practice) SOLUTION. Fall 2017

Lecture #39. Initial grading run Wednesday. GUI files up soon. Today: Dynamic programming and memoization.

2 marks. class q1c{ class point{ int p,q; point(int p, int q){ this.p=p; this.q=q; } void printpoint(){ System.out.println(this.p+" "+this.

DIT960 Datastrukturer

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

Dynamic Programming II

Midterm Sample Answer ECE 454F 2008: Computer Systems Programming Date: Tuesday, Oct 28, p.m. - 5 p.m.

Procedural programming with C

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CS 1110 Final, December 8th, Question Points Score Total: 100

Algorithms Exam TIN093/DIT600

Complexity, General. Standard approach: count the number of primitive operations executed.

1.3 Conditionals and Loops

Lecture Notes on Tries

The Simplex Algorithm for LP, and an Open Problem

Midterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays

CSE 373 Final Exam 3/14/06 Sample Solution

'C' Programming Language

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

1 Dynamic Programming

Project 1. due date Sunday July 8, 2018, 12:00 noon

Hypercubes. (Chapter Nine)

Department of Computer Science COMP The Programming Competency Test

Object-Oriented Design Lecture 16 CS 3500 Fall 2010 (Pucella) Friday, Nov 12, 2010

Transcription:

PROGRAMMING for EOR (346022) 7-Jul-2011 Lecturer: Bert Bettonvil, Manfred Jeusfeld This is NO open book exam. The use of electronic devices such as calculators, smartphones or computers is not allowed. The exam consists of 5 questions with 2 parts each, and all are of equal weight. The exam will be marked before August 1. By then, a standard elaboration is presented on Blackboard, and you may make an appointment by e-mail to view your exam. The programs asked must: be clear, short and be written in Delphi/Pascal, except for question 2a and 2b: these must be answered in C++. must have a clear algorithm and clear identifiers. Further remarks: This exam is not about reproducing details or user-interface. The latter has been tested exhaustively while making the assignments. The emphasis of the exam is on algorithms, use of data structures and parameters. Most input and output is done by use of parameters. However, sometimes knowledge of the user-interface is needed. In the listings we mostly use OUTPUT(...), which can be read as Edit1.Text:=..., depending on the graphical environment. The emphasis in this exam is on how the value(s) of the variable(s) are computed, and not on how these are reported. In case you know what must happen in a program, but you do not know how it must be done, then indicate this in a clear way. Example: ### Here A must be inverted ###. 1

Question 1 (Delphi) [1.a] A recursive function can always be transformed into an iterative function. Do so for the function F where N is a positive integer number. function F (N: Integer): Integer; if N =0 then F := 0 else F := 2 * F(N div 2) + 1; [1.b] Give the output for the variable P,R,T of the following program module (no elaboration): procedure Proc1b; var A,R: Integer; procedure computef(var P,Q: Integer; R,S: Integer); P := R*S; R := R+S; S := P+Q; Q := S-R; A:=3; R:=5; computef(r,a,a,r); OUTPUT(A,R) 2

Question 2 (C++) Let A[N][N] be a two-dimensional array of integer numbers (i.e. indexes run from 0... N-1. Consider the following game. A player 1 chooses a number i between 0 and N-1. Then, player 2 chooses a number j between 0 and N-1. As a result of the game, player 1 pays A[i][j] to player 2. Note that A[i][j] can be negative meaning that the payment goes into the opposite direction. [2.a] Player 2 assumes that player 1 has no knowledge of the matrix A. Hence he assumes that player 1 chooses an arbitrary number i. Player 2 has two alternative strategies in mind. Strategy 1 is to always select the same number as player 1, i.e. j=i. Strategy 2 would is to select an arbitrary number j. Write a function Better with parameter A that return 1 if strategy 1 is better and 2 is strategy 2 is better. Note that you need to compute the expected payment for both strategies and also note that the solution depends on the values in matrix A. [2.b] Player 2 gets information about the matrix A. He wants to precompute an array B that returns j=b[i] for which A[i][j] is maximal, i.e. returns the best result for him. Write a procedure makeb that creates the array B out of the matrix A. 3

Question 3 (Delphi) Consider a the following type definitions const N=...; {* number of points *} type Point = record xpos: Real; ypos: Real Curve = Array[1..N] of Point; The type curve represents points of a curve. We assume that the x- positions are strictly increasing, i.e. for any curve c and for any two indexes 1 i, j N the following holds: i < j then c[i].xpos < c[j].xpos. [3.a] (Integral) Implement a function function integral(c: Curve) : Real; that computes the integral of the curve c. The integral shall be approximated by the average between the area of the upper y- values (gray reactangles) and the lower y-values (hatched rectangles) of two consecutive points. Note that the distance between two x values is not a constant. 4

[3.b] (First zero point) Implement a function function firstzeropoint(c: Curve): Real; that computes the first zero point of the curve c, where each two consecutive points are connected by a straight line. Return 0.0 if there is no such zero point. In the above drawing, the first zero point is indicated by the arrow. You should return the x-value, where the line crosses the zero-level. 5

Question 4 (Delphi) Consider the following type definitions for a linked list. type ElemP = ^Elem Elem = record value: Integer; next: ElemP end [4.a]: Write a function function Count(x: Integer; Head: ElemP): Integer; that counts the number of occurrences of value x in the linked list with first element Head. [4.b]: Write a procedure procedure Reverse(Head: ElemP; var NewHead: ElemP); that reverses the elements in the list original list Head, i.e. the new list starts with the last element of the original list and ends with the first element of the original list. 6

Question 5 (Delphi) A logistics company plans truck tours to customers. const N=...; {* number of trucks *} M=...; {* number of customers *} maxhops =...; {* max number of stops in a tour *} type Coordinates = record xpos: Real; ypos: Real Truck = record trucknr: Integer; currentpos: Coordinates; booked: Boolean Customer = record custnr: Integer; homebase: Coordinates Trucks = array[1..n] of Truck; Customers = array[1..m] of Customer; Tour = array[1..maxhops] of Integer; { the entries in a tour are customer numbers } The coordinates are in an Euklidian space (x- and y-values) where the distance between two coordinates are computed by the Euklidian distance. [5.a] Implement a function function nearesttruck(cust: Customer; tr:trucks): Integer; that returns the number of the truck in tr that is closest to the home base of customer cust. Use Euklidian distance. Note: it is useful to define a function edistance that calculates the Euklidian distance between two coordinates. This function can be used for both 5a and 5b. 7

[5.b] Implement the procedure procedure optimizetour(start: Coordinates; custs: Customers; var t: Tour); that optimizes the stops in the tour t as follows: the first stop in tour t is the customer that is closed to the given start point. The second is the customer in the set of remaining customers in the tour that is closest to the first stop (computed in the preceding step), and so on. The procedure is called with an non-optimized tour t. After calling the procedure, the tour t shall be optimized. 8

Master solutions Q1a function F_it(N: Integer): Integer; var I,Res: Integer; I:=N; Res:=0; while (I>0) do Res:=2*Res + 1; I:= I div 2; F_it := Res Important here is that you may not increase the result for each i (1<=i<==N). Q1b A=10 R=15 9

Q2 const int N=2; typedef int TMatr[N][N]; typedef int TMatr1[N]; Q2a int Better(TMatr& A) { } double count1=0; double count2=0; for (int i=0; i < N; i++) { count1 += A[i][i]; for (int j=0; j < N; j++) { count2 += A[i][j]; } } count2 /= N; return (count1>count2)? 1 : 2; count1 is the expected value for strategy 1 (always choose the same number); count2 is the expected value for the strategy 2 (randomized strategy). Q2b void MakeB(TMatr& A, TMatr1& B) { int win; for (int i=0; i<n; i++){ win=a[i][0]; B[i]=0; for (int j=0; j<n; j++){ if (A[i][j]>win) {B[i]=j; win=a[i][j];} } } } Note that B is a matrix containing indexes to A (B[i]=j means: choose j if opponent chooses i). The variable win is for determining the best position j in A[i][j]. 10

Q3 const N= 5; {* points *} type Point = record xpos: Real; ypos: Real Curve = Array[1..N] of Point; var mycurve: Curve; Q3a function integral(c: Curve) : Real; var i: Integer; sum1,sum2: Real; function min(r1,r2: Real): Real; if (r1 < r2) then min := r1 else min := r2 function max(r1,r2: Real): Real; if (r1 < r2) then max := r2 else max := r1 sum1 := 0.0; sum2 := 0.0; for i := 2 to N do sum1 := sum1 + max(c[i-1].ypos,c[i].ypos) * (c[i].xpos - c[i-1].xpos); sum2 := sum2 + min(c[i-1].ypos,c[i].ypos) * (c[i].xpos - c[i-1].xpos) integral := (sum1 + sum2)/2 Simpler solutions were possible! 11

Q3b function firstzeropoint(c:curve): Real; var found: Boolean; i: Integer; zp: Real; found := false; i := 1; zp := 0.0; while not found and (i < N) do i := i + 1; if (c[i-1].ypos <= 0.0) and (c[i].ypos >= 0.0) or (c[i-1].ypos >= 0.0) and (c[i].ypos <= 0.0) then found := true; if (c[i].ypos = c[i-1].ypos) and (c[i].ypos = 0.0) then zp := c[i].xpos else zp := c[i-1].xpos - ( c[i-1].ypos*(c[i].xpos-c[i-1].xpos) ) / ( c[i].ypos - c[i-1].ypos ) end firstzeropoint := zp; Note that the curve C only contains the carrying points and that the zero point is typically between two consecutive points, or could be just hit by some point of C. Actuallu firstzeropoint is only well-defined when found=true. But the initial value was acceptable in such cases. 12

Q4 Q4a function Count(x: Integer; Head: ElemP): Integer; var Help: ElemP; Count:=0; Help:=Head; while (Help<>nil) do if (Help^.value=x) then inc(count); Help:=Help^.Next; Loop with Help<>nil, not Help^.next<M>nil! Q4b procedure Reverse(Head: ElemP; var NewHead: ElemP); var Help1,Help2: ElemP; if (Head=nil) then NewHead:=nil else new(newhead); NewHead^.value:=Head^.value; NewHead^.Next:=nil; Help1:=Head; while (Help1^.Next<>nil) do Help1:=Help1^.Next; new(help2); Help2^.value:=Help1^.value; Help2^.Next:=NewHead; NewHead:=Help2; You needed to take care of Head=nil and that NewHead is actually set correctly. So Help1 scans the old list, and Help2 is for setting the NewHead iteratively. No dispose! 13

14

Q5 const N= 2; {* number of trucks *} M= 3; {* number of customers *} maxhops = 2; {* maximum number of stops in a tour *} type Coordinates = record xpos: Real; ypos: Real Truck = record trucknr: Integer; model: String; currentpos: Coordinates; booked: Boolean Customer = record custnr: Integer; homebase: Coordinates Trucks = array[1..n] of Truck; Customers = array[1..m] of Customer; Tour = array[1..maxhops] of Integer; var mytrucks: Trucks; mycustomers: Customers; 15

{* 5.a *} function edistance(p1,p2: Coordinates) : Real; edistance := sqrt( sqr(p1.xpos - p2.xpos) + sqr(p1.ypos - p2.ypos) ); This is the Euklidian distance. function nearesttruck(cust: Customer; tr:trucks): Integer; var i,nt: Integer; bestdist: Real; nt := 0; for i := 1 to N do if ( (nt=0) and not (tr[i].booked=true) ) then nt := i; bestdist := edistance(cust.homebase,tr[nt].currentpos) end else if edistance(cust.homebase,tr[i].currentpos) < bestdist then nt := i; bestdist := edistance(cust.homebase,tr[i].currentpos) end nearesttruck := tr[nt].trucknr 16

{* 5.b *} procedure optimizetour(start: Coordinates; custs: Customers; var tour: Tour); var currentpos: Coordinates; i,j,help: Integer; currentpos := start; for i := 1 to maxhops do for j := i+1 to MaxHops do if edistance(currentpos,custs[tour[i]].homebase) > edistance(currentpos,custs[tour[j]].homebase) then help := tour[i]; tour[i] := tour[j]; tour[j] := help; currentpos := custs[tour[i]].homebase end end This task is to be solved by a double loop (sorting tour). 17