PROGRAMMING for EOR (346022) 7-Jul-2011

Size: px
Start display at page:

Download "PROGRAMMING for EOR (346022) 7-Jul-2011"

Transcription

1 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 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

2 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

3 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

4 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

5 [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

6 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

7 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

8 [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

9 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

10 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

11 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

12 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

13 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 14

15 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

16 {* 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

17 {* 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

COS 126 General Computer Science Spring Written Exam 1

COS 126 General Computer Science Spring Written Exam 1 COS 126 General Computer Science Spring 2017 Written Exam 1 This exam has 9 questions (including question 0) worth a total of 70 points. You have 50 minutes. Write all answers inside the designated spaces.

More information

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

Module 16: Data Flow Analysis in Presence of Procedure Calls Lecture 32: Iteration. The Lecture Contains: Iteration Space. The Lecture Contains: Iteration Space Iteration Vector Normalized Iteration Vector Dependence Distance Direction Vector Loop Carried Dependence Relations Dependence Level Iteration Vector - Triangular

More information

Data Structures Lecture 3 Order Notation and Recursion

Data Structures Lecture 3 Order Notation and Recursion Data Structures Lecture 3 Order Notation and Recursion 1 Overview The median grade.cpp program from Lecture 2 and background on constructing and using vectors. Algorithm analysis; order notation Recursion

More information

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

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher Date of Performance:... Actual Date of Completion:... Expected Date of Completion:... ----------------------------------------------------------------------------------------------------------------

More information

CS13002 Programming and Data Structures, Spring 2005

CS13002 Programming and Data Structures, Spring 2005 CS13002 Programming and Data Structures, Spring 2005 Mid-semester examination : Solutions Roll no: FB1331 Section: @ Name: Foolan Barik Answer all questions. Write your answers in the question paper itself.

More information

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

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays The University Of Michigan Lecture 07 Andrew M. Morgan Sorting Arrays Element Order Of Arrays Arrays are called "random-access" data structures This is because any element can be accessed at any time Other

More information

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

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion Review from Lectures 5 & 6 Arrays and pointers, Pointer arithmetic and dereferencing, Types of memory ( automatic, static,

More information

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS July 8 13, 2008 Shumen, Bulgaria TASKS AND SOLUTIONS Day 1 Task 1. TOWERS OF COINS Statement Asen and Boyan are playing the following game. They choose two

More information

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

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18 Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical

More information

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

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4) CS210 Data Structures (171) Final Exam Name: ID Instructions: This exam contains four questions with multiple parts. Time allowed: 180 minutes Closed Book, Closed Notes. There are 10 pages in this exam

More information

COMPUTATIONAL PROPERIES OF DSP ALGORITHMS

COMPUTATIONAL PROPERIES OF DSP ALGORITHMS COMPUTATIONAL PROPERIES OF DSP ALGORITHMS 1 DSP Algorithms A DSP algorithm is a computational rule, f, that maps an ordered input sequence, x(nt), to an ordered output sequence, y(nt), according to xnt

More information

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

VARIABLE, OPERATOR AND EXPRESSION [SET 1] VARIABLE, OPERATOR AND EXPRESSION Question 1 Write a program to print HELLO WORLD on screen. Write a program to display the following output using a single cout statement. Subject Marks Mathematics 90

More information

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

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion Announcements: Test 1 Information Test 1 will be held Monday, Sept 25th, 2017 from 6-7:50pm Students will be randomly assigned

More information

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

In this article we will see how the OmniThreadLibrary can be used to split a simple image processing kernel across multiple threads. In a previous article named " Easy multi-thread programming Delphi ", the AsyncCalls library was used to process multiple images at the same time. However, the processing of every single image was still

More information

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

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Reading for this lecture: Carrano, Chapter 2 Copy constructor, destructor, operator=

More information

C Curves That Fill Space

C Curves That Fill Space C Curves That Fill Space A space-filling curve completely fills up part of space by passing through every point in that part. It does that by changing direction repeatedly. We will only discuss curves

More information

Lecture Notes on Dynamic Programming

Lecture Notes on Dynamic Programming Lecture Notes on Dynamic Programming 15-122: Principles of Imperative Computation Frank Pfenning Lecture 23 November 16, 2010 1 Introduction In this lecture we introduce dynamic programming, which is a

More information

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

CIS 110 Introduction to Computer Programming Summer 2017 Final. Recitation # (e.g., 201): CIS 110 Introduction to Computer Programming Summer 2017 Final Name: Recitation # (e.g., 201): Pennkey (e.g., paulmcb): My signature below certifies that I have complied with the University of Pennsylvania

More information

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

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam 1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Exam Name: Email Address: TA: Section: You have three hours to complete this exam. For coding questions, you do not need to include

More information

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

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam Seat Number Name CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam This is a closed book exam. Answer all of the questions on the question paper in the space provided. If

More information

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

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING Friday 20 th December 2013 14:30 to 16:30 INSTRUCTIONS TO CANDIDATES 1.

More information

Adding Integers. Unit 1 Lesson 6

Adding Integers. Unit 1 Lesson 6 Unit 1 Lesson 6 Students will be able to: Add integers using rules and number line Key Vocabulary: An integer Number line Rules for Adding Integers There are two rules that you must follow when adding

More information

CS 161 Fall 2015 Final Exam

CS 161 Fall 2015 Final Exam CS 161 Fall 2015 Final Exam Name: Student ID: 1: 2: 3: 4: 5: 6: 7: 8: Total: 1. (15 points) Let H = [24, 21, 18, 15, 12, 9, 6, 3] be an array of eight numbers, interpreted as a binary heap with the maximum

More information

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

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution Recursion [ Why is it important?] ~7 easy marks in Exam Paper Seemingly Different Coding Approach In Fact: Strengthen Top-down Thinking Get Mature in - Setting parameters - Function calls - return + work

More information

Bentley Rules for Optimizing Work

Bentley Rules for Optimizing Work 6.172 Performance Engineering of Software Systems SPEED LIMIT PER ORDER OF 6.172 LECTURE 2 Bentley Rules for Optimizing Work Charles E. Leiserson September 11, 2012 2012 Charles E. Leiserson and I-Ting

More information

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

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50 1 NET/JRF-COMPUTER SCIENCE & APPLICATIONS UNIT TEST : DATA STRUCTURE Time: 01 : 00 Hour Date : 02-06-2017 M.M. : 50 INSTRUCTION: Attempt all the 25 questions. Each question carry TWO marks. 1. Consider

More information

ESC101 : Fundamental of Computing

ESC101 : Fundamental of Computing ESC101 : Fundamental of Computing End Semester Exam 19 November 2008 Name : Roll No. : Section : Note : Read the instructions carefully 1. You will lose 3 marks if you forget to write your name, roll number,

More information

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

COS 126 Exam Review. Exams overview Example programming exam Example written exam questions (part 1) COS 126 Exam Review Exams overview Example programming exam Example written exam questions (part 1) Exams overview (revisited) We have exams in the fall Two written exams. Two programming exams. Prep sessions

More information

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

CS61B Lecture #35. [The Lecture #32 notes covered lectures #33 and #34.] Today: Enumerated types, backtracking searches, game trees. CS61B Lecture #35 [The Lecture #32 notes covered lectures #33 and #34.] Today: Enumerated types, backtracking searches, game trees. Coming Up: Graph Structures: DSIJ, Chapter 12 Last modified: Mon Nov

More information

ECE 563 Spring 2012 First Exam

ECE 563 Spring 2012 First Exam ECE 563 Spring 2012 First Exam version 1 This is a take-home test. You must work, if found cheating you will be failed in the course and you will be turned in to the Dean of Students. To make it easy not

More information

WYSE Academic Challenge 2002 Computer Science Test (Sectional) SOLUTION

WYSE Academic Challenge 2002 Computer Science Test (Sectional) SOLUTION Computer Science - 1 WYSE Academic Challenge 2002 Computer Science Test (Sectional) SOLUTION 1. Access to moving head disks requires three periods of delay before information is brought into memory. The

More information

Tribhuvan University Institute of Science and Technology 2065

Tribhuvan University Institute of Science and Technology 2065 1CSc.102-2065 2065 Candidates are required to give their answers in their own words as for as practicable. 1. Draw the flow chart for finding largest of three numbers and write an algorithm and explain

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

More information

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem.

More information

Solutions to Problem Set 1

Solutions to Problem Set 1 CSCI-GA.3520-001 Honors Analysis of Algorithms Solutions to Problem Set 1 Problem 1 An O(n) algorithm that finds the kth integer in an array a = (a 1,..., a n ) of n distinct integers. Basic Idea Using

More information

Arrays III and Enumerated Types

Arrays III and Enumerated Types Lecture 15 Arrays III and Enumerated Types Multidimensional Arrays & enums CptS 121 Summer 2016 Armen Abnousi Multidimensional Arrays So far we have worked with arrays with one dimension. Single dimensional

More information

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

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

Exam Sample Questions CS3212: Algorithms and Data Structures

Exam Sample Questions CS3212: Algorithms and Data Structures Exam Sample Questions CS31: Algorithms and Data Structures NOTE: the actual exam will contain around 0-5 questions. 1. Consider a LinkedList that has a get(int i) method to return the i-th element in the

More information

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

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1 DIVIDE & CONQUER Definition: Divide & conquer is a general algorithm design strategy with a general plan as follows: 1. DIVIDE: A problem s instance is divided into several smaller instances of the same

More information

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

More information

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

CS 3410 Ch 5 (DS*), 23 (IJP^) CS 3410 Ch 5 (DS*), 23 (IJP^) *CS 1301 & 1302 text, Introduction to Java Programming, Liang, 7 th ed. ^CS 3410 text, Data Structures and Problem Solving Using Java, Weiss, 4 th edition Sections Pages Review

More information

Data Structures and Algorithms Winter Semester

Data Structures and Algorithms Winter Semester Page 0 German University in Cairo October 24, 2018 Media Engineering and Technology Faculty Prof. Dr. Slim Abdennadher Dr. Wael Abouelsadaat Data Structures and Algorithms Winter Semester 2018-2019 Midterm

More information

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

ECEN 615 Methods of Electric Power Systems Analysis Lecture 11: Sparse Systems ECEN 615 Methods of Electric Power Systems Analysis Lecture 11: Sparse Systems Prof. Tom Overbye Dept. of Electrical and Computer Engineering Texas A&M University overbye@tamu.edu Announcements Homework

More information

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

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs). Lecture 13 Graph Algorithms I 13.1 Overview This is the first of several lectures on graph algorithms. We will see how simple algorithms like depth-first-search can be used in clever ways (for a problem

More information

Introduction to Algorithms

Introduction to Algorithms Introduction to Algorithms Dynamic Programming Well known algorithm design techniques: Brute-Force (iterative) ti algorithms Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic

More information

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

Algorithm Analysis. Jordi Cortadella and Jordi Petit Department of Computer Science Algorithm Analysis Jordi Cortadella and Jordi Petit Department of Computer Science What do we expect from an algorithm? Correct Easy to understand Easy to implement Efficient: Every algorithm requires

More information

The Citizen s Guide to Dynamic Programming

The Citizen s Guide to Dynamic Programming The Citizen s Guide to Dynamic Programming Jeff Chen Stolen extensively from past lectures October 6, 2007 Unfortunately, programmer, no one can be told what DP is. You have to try it for yourself. Adapted

More information

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Lecture 13 Geometry and Computational Geometry

Lecture 13 Geometry and Computational Geometry Lecture 13 Geometry and Computational Geometry Euiseong Seo (euiseong@skku.edu) 1 Geometry a Branch of mathematics concerned with questions of shape, size, relative position of figures, and the properties

More information

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

XVIII Open Cup named after E.V. Pankratiev Stage 1: Grand Prix of Romania, Sunday, September 17, 2017 Problem A. Balance file: 1 second 512 mebibytes We say that a matrix A of size N N is balanced if A[i][j] + A[i + 1][j + 1] = A[i + 1][j] + A[i][j + 1] for all 1 i, j N 1. You are given a matrix A of size

More information

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

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition Programming as a contract Assertions, pre/postconditions and invariants Assertions: Section 4.2 in Savitch (p. 239) Loop invariants: Section 4.5 in Rosen Specifying what each method does q Specify it in

More information

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

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples Elements of Dynamic Programming COSC 3A - Design and Analysis of Algorithms 8 Elements of DP Memoization Longest Common Subsequence Greedy Algorithms Many of these slides are taken from Monica Nicolescu,

More information

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

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 4 Certificate in IT SOFTWARE DEVELOPMENT BCS THE CHARTERED INSTITUTE FOR IT BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 4 Certificate in IT SOFTWARE DEVELOPMENT Wednesday 25th March 2015 - Afternoon Time: TWO hours Section A and Section B each

More information

Sorting. Weiss chapter , 8.6

Sorting. Weiss chapter , 8.6 Sorting Weiss chapter 8.1 8.3, 8.6 Sorting 5 3 9 2 8 7 3 2 1 4 1 2 2 3 3 4 5 7 8 9 Very many different sorting algorithms (bubblesort, insertion sort, selection sort, quicksort, heapsort, mergesort, shell

More information

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

Algorithm Analysis. Jordi Cortadella and Jordi Petit Department of Computer Science Algorithm Analysis Jordi Cortadella and Jordi Petit Department of Computer Science What do we expect from an algorithm? Correct Easy to understand Easy to implement Efficient: Every algorithm requires

More information

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

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit. Name CS 110 Practice Final Exam originally from Winter, 2003 Instructions: closed books, closed notes, open minds, 3 hour time limit. There are 4 sections for a total of 49 points. Part I: Basic Concepts,

More information

Algorithms and Complexity. Exercise session 3+4

Algorithms and Complexity. Exercise session 3+4 Algorithms and Complexity. Exercise session 3+4 Dynamic Programming Longest Common Substring The string ALGORITHM and the string PLÅGORIS have the common substring GORI. The longest common substring of

More information

BITG 1113: Array (Part 1) LECTURE 8

BITG 1113: Array (Part 1) LECTURE 8 BITG 1113: Array (Part 1) LECTURE 8 1 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional (1 D)

More information

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

ˆ The exam is closed book, closed calculator, and closed notes except your one-page crib sheet. CS Summer Introduction to Artificial Intelligence Midterm ˆ You have approximately minutes. ˆ The exam is closed book, closed calculator, and closed notes except your one-page crib sheet. ˆ Mark your answers

More information

ENGR 1181 MATLAB 09: For Loops 2

ENGR 1181 MATLAB 09: For Loops 2 ENGR 1181 MATLAB 09: For Loops Learning Objectives 1. Use more complex ways of setting the loop index. Construct nested loops in the following situations: a. For use with two dimensional arrays b. For

More information

ESC101N: Fundamentals of Computing End-sem st semester

ESC101N: Fundamentals of Computing End-sem st semester ESC101N: Fundamentals of Computing End-sem 2010-11 1st semester Instructor: Arnab Bhattacharya 8:00-11:00am, 15th November, 2010 Instructions 1. Please write your name, roll number and section below. 2.

More information

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

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions U.C. Berkeley CS70 : Algorithms Midterm 2 Solutions Lecturers: Sanjam Garg and Prasad aghavra March 20, 207 Midterm 2 Solutions. (0 points) True/False Clearly put your answers in the answer box in front

More information

Dynamic Programming Algorithms

Dynamic Programming Algorithms Based on the notes for the U of Toronto course CSC 364 Dynamic Programming Algorithms The setting is as follows. We wish to find a solution to a given problem which optimizes some quantity Q of interest;

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using

More information

Data Structures CSci 1200 Test 1 Questions

Data Structures CSci 1200 Test 1 Questions Overview Data Structures CSci 1200 Test 1 Questions Test 1 will be held Monday, February 14, 2011, 12:00-1:30pm, Darrin 308. No make-ups will be given except for emergency situations, and even then a written

More information

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

The exam is closed book, closed calculator, and closed notes except your one-page crib sheet. CS Summer Introduction to Artificial Intelligence Midterm You have approximately minutes. The exam is closed book, closed calculator, and closed notes except your one-page crib sheet. Mark your answers

More information

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

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001 CPSC 211, Sections 201 203: Data Structures and Implementations, Honors Final Exam May 4, 2001 Name: Section: Instructions: 1. This is a closed book exam. Do not use any notes or books. Do not confer with

More information

Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning Lecture 17 1 Introduction In the previous two lectures we have seen how to exploit the structure of binary

More information

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

Total Score /1 /20 /41 /15 /23 Grader NAME: NETID: CS2110 Spring 2015 Prelim 2 April 21, 2013 at 5:30 0 1 2 3 4 Total Score /1 /20 /41 /15 /23 Grader There are 5 questions numbered 0..4 on 8 pages. Check now that you have all the pages. Write

More information

CSE 101 Homework 5. Winter 2015

CSE 101 Homework 5. Winter 2015 CSE 0 Homework 5 Winter 205 This homework is due Friday March 6th at the start of class. Remember to justify your work even if the problem does not explicitly say so. Writing your solutions in L A TEXis

More information

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1 Algorithm Analysis Spring Semester 2007 Programming and Data Structure 1 What is an algorithm? A clearly specifiable set of instructions to solve a problem Given a problem decide that the algorithm is

More information

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

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm ECE G205 Fundamentals of Computer Engineering Fall 2003 Exercises in Preparation to the Midterm The following problems can be solved by either providing the pseudo-codes of the required algorithms or the

More information

ESc101 : Fundamental of Computing

ESc101 : Fundamental of Computing ESc101 : Fundamental of Computing I Semester 2008-09 Lecture 37 Analyzing the efficiency of algorithms. Algorithms compared Sequential Search and Binary search GCD fast and GCD slow Merge Sort and Selection

More information

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

CMSC 131A, Midterm 1 (Practice) SOLUTION. Fall 2017 CMSC 131A, Midterm 1 (Practice) SOLUTION Fall 2017 Question Points 1 10 2 10 3 10 4 20 5 10 6 20 7 20 Total: 100 This test is open-book, open-notes, but you may not use any computing device other than

More information

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

Lecture #39. Initial grading run Wednesday. GUI files up soon. Today: Dynamic programming and memoization. Lecture #39 Initial grading run Wednesday. GUI files up soon. Today: Dynamic programming and memoization. Last modified: Fri Dec 10 12:19:27 2004 CS61B: Lecture #39 1 A puzzle (D. Garcia): Dynamic Programming

More information

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.

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. Question1. What will be the output of the following programs? Give reasons. [4, 7, 4] No credit will be given if you do not give reasons (even if your output is correct). Also, if the reasoning is wrong

More information

DIT960 Datastrukturer

DIT960 Datastrukturer DIT90 Datastrukturer suggested solutions for exam 07-0-0. The following code takes as input two arrays with elements of the same type. The arrays are called a and b. The code returns a dynamic array which

More information

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

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8 CSE341T/CSE549T 09/17/2014 Lecture 8 Scan and its Uses 1 Scan Today, we start by learning a very useful primitive. First, lets start by thinking about what other primitives we have learned so far? The

More information

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

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr CPSC 259: Data Structures and Algorithms for Electrical Engineers APSC 160 Review Hassan Khosravi Borrowing many questions from Ed Knorr CPSC 259 Pointers Page 1 Learning Goal Briefly review some key programming

More information

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

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -0 Department of Electronics and Communication INTERNAL ASSESSMENT TEST 1 Date : 30/8/2017 Marks: 0 Subject & Code

More information

Dynamic Programming II

Dynamic Programming II Lecture 11 Dynamic Programming II 11.1 Overview In this lecture we continue our discussion of dynamic programming, focusing on using it for a variety of path-finding problems in graphs. Topics in this

More information

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

Midterm Sample Answer ECE 454F 2008: Computer Systems Programming Date: Tuesday, Oct 28, p.m. - 5 p.m. Midterm Sample Answer ECE 454F 2008: Computer Systems Programming Date: Tuesday, Oct 28, 2008 3 p.m. - 5 p.m. Instructor: Cristiana Amza Department of Electrical and Computer Engineering University of

More information

Procedural programming with C

Procedural programming with C Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical

More information

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

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm CIS 110 Introduction To Computer Programming February 29, 2012 Midterm Name: Recitation # (e.g. 201): Pennkey (e.g. bjbrown): My signature below certifies that I have complied with the University of Pennsylvania

More information

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

CS 1110 Final, December 8th, Question Points Score Total: 100 CS 1110 Final, December 8th, 2016 This 150-minute exam has 8 questions worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need more

More information

Algorithms Exam TIN093/DIT600

Algorithms Exam TIN093/DIT600 Algorithms Exam TIN093/DIT600 Course: Algorithms Course code: TIN 093 (CTH), DIT 600 (GU) Date, time: 24th October 2015, 14:00 18:00 Building: M Responsible teacher: Peter Damaschke, Tel. 5405. Examiner:

More information

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

Complexity, General. Standard approach: count the number of primitive operations executed. Complexity, General Allmänt Find a function T(n), which behaves as the time it takes to execute the program for input of size n. Standard approach: count the number of primitive operations executed. Standard

More information

1.3 Conditionals and Loops

1.3 Conditionals and Loops 1.3 Conditionals and Loops Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 15/1/2013 22:16:24 A Foundation for Programming any program

More information

Lecture Notes on Tries

Lecture Notes on Tries Lecture Notes on Tries 15-122: Principles of Imperative Computation Thomas Cortina, Frank Pfenning, Rob Simmons, Penny Anderson Lecture 22 June 20, 2014 1 Introduction In the data structures implementing

More information

The Simplex Algorithm for LP, and an Open Problem

The Simplex Algorithm for LP, and an Open Problem The Simplex Algorithm for LP, and an Open Problem Linear Programming: General Formulation Inputs: real-valued m x n matrix A, and vectors c in R n and b in R m Output: n-dimensional vector x There is one

More information

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

Midterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011 Midterm I Exam 15-122 Principles of Imperative Computation Frank Pfenning February 17, 2011 Name: Sample Solution Andrew ID: fp Section: Instructions This exam is closed-book with one sheet of notes permitted.

More information

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

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays Prof. Amr Goneid, AUC 1 Arrays Prof. Amr Goneid, AUC 2 1-D Arrays Data Structures The Array Data Type How to Declare

More information

CSE 373 Final Exam 3/14/06 Sample Solution

CSE 373 Final Exam 3/14/06 Sample Solution Question 1. (6 points) A priority queue is a data structure that supports storing a set of values, each of which has an associated key. Each key-value pair is an entry in the priority queue. The basic

More information

'C' Programming Language

'C' Programming Language F.Y. Diploma : Sem. II [DE/EJ/ET/EN/EX] 'C' Programming Language Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1(a) Define pointer. Write syntax

More information

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

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:... Family Name:... Other Names:... Signature:... Student Number:... THE UNIVERSITY OF NEW SOUTH WALES SCHOOL OF COMPUTER SCIENCE AND ENGINEERING Sample Examination COMP1917 Computing 1 EXAM DURATION: 2 HOURS

More information

1 Dynamic Programming

1 Dynamic Programming CS161 Lecture 13 Dynamic Programming and Greedy Algorithms Scribe by: Eric Huang Date: May 13, 2015 1 Dynamic Programming The idea of dynamic programming is to have a table of solutions of subproblems

More information

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

Project 1. due date Sunday July 8, 2018, 12:00 noon Queens College, CUNY, Department of Computer Science Object-oriented programming in C++ CSCI 211 / 611 Summer 2018 Instructor: Dr. Sateesh Mane c Sateesh R. Mane 2018 Project 1 due date Sunday July 8,

More information

Hypercubes. (Chapter Nine)

Hypercubes. (Chapter Nine) Hypercubes (Chapter Nine) Mesh Shortcomings: Due to its simplicity and regular structure, the mesh is attractive, both theoretically and practically. A problem with the mesh is that movement of data is

More information

Department of Computer Science COMP The Programming Competency Test

Department of Computer Science COMP The Programming Competency Test 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

More information

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

Object-Oriented Design Lecture 16 CS 3500 Fall 2010 (Pucella) Friday, Nov 12, 2010 Object-Oriented Design Lecture 16 CS 3500 Fall 2010 (Pucella) Friday, Nov 12, 2010 16 Mutation We have been avoiding mutations until now. But they exist in most languages, including Java and Scala. Some

More information