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