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

Similar documents
Queens College, CUNY, Department of Computer Science Numerical Methods CSCI 361 / 761 Fall 2018 Instructor: Dr. Sateesh Mane

Queens College, CUNY, Department of Computer Science Computational Finance CSCI 365 / 765 Fall 2018 Instructor: Dr. Sateesh Mane

2 Homework: Bond class, fair value, duration & yield

CS110D: PROGRAMMING LANGUAGE I

CSE100 Principles of Programming with C++

2. You are required to enter a password of up to 100 characters. The characters must be lower ASCII, printing characters.

Data Structures and Algorithms

CSC212. Data Structure. Lecture 12 Reasoning about Recursion. Instructor: George Wolberg Department of Computer Science City College of New York

WELCOME! (download slides and.py files and follow along!) LECTURE 1

CSCE 121 ENGR 112 List of Topics for Exam 1

Table of Contents. Course Minutiae. Course Overview Algorithm Design Strategies Algorithm Correctness Asymptotic Analysis 2 / 32

Queens College, CUNY, Department of Computer Science Numerical Methods CSCI 361 / 761 Spring 2018 Instructor: Dr. Sateesh Mane.

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

ECE264 Spring 2013 Final Exam, April 30, 2013

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

Subset sum problem and dynamic programming

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

CS 241 Data Organization using C

6.1 Skip List, Binary Search Tree

Practical 2: Ray Tracing

CS 2316 Individual Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 18th, before 11:55 PM Out of 100 points

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Unit 7 Number System and Bases. 7.1 Number System. 7.2 Binary Numbers. 7.3 Adding and Subtracting Binary Numbers. 7.4 Multiplying Binary Numbers

Functions. Lecture 6 COP 3014 Spring February 11, 2018

6. Control Statements II

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

University of Cincinnati Department of Electrical and Computer Engineering EECE180 Software Methods I

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

EDIT202 Spreadsheet Lab Prep Sheet

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

An introduction introduction to functional functional programming programming using usin Haskell

CS 051 Homework Laboratory #2

UNIVERSITY REGULATIONS

Programming Assignment #4 Arrays and Pointers

CSCE 2014 Final Exam Spring Version A

Computer Science II Spring 2018 Final Exam Instructor: Arup Guha Date: 5/1/2018. Name:

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

(Refer Slide Time: 01.26)

CS 112: Intro to Comp Prog

CS 1803 Pair Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 29th, before 6 PM Out of 100 points

Q1 Q2 Q3 Q4 Q5 Q6 Total

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

CS 101, Spring 2016 March 22nd Exam 2

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Fortran 90 Two Commonly Used Statements

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

CMPE012 Computer Engineering 12 (and Lab) Computing Systems and Assembly Language Programming. Summer 2009

gcc o driver std=c99 -Wall driver.c bigmesa.c

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CS 115 Exam 3, Spring 2010

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Lecture 10. Daily Puzzle

Algebra Homework: Chapter 1 (Homework is listed by date assigned; homework is due the following class period) Day Date In-Class Homework

Exam Informatik D-MATH/D-PHYS :00 17:00

Answer the following questions on the answer sheet that is provided. The computer must be switched off while you are busy with Section A.

Reasoning About Imperative Programs. COS 441 Slides 10

CS 415 Midterm Exam Fall 2003

C Programming for Engineers Structured Program

DEPARTMENT OF ACADEMIC UPGRADING

Syllabus. ICS103: Computer Programming in C 2017 / 2018 First Semester (Term 171) INSTRUCTOR Office Phone Address Office Hours

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

may be sent to:

Notes on the 2008 Exam

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Table : IEEE Single Format ± a a 2 a 3 :::a 8 b b 2 b 3 :::b 23 If exponent bitstring a :::a 8 is Then numerical value represented is ( ) 2 = (

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

During the first 2 weeks of class, all students in the course will take an in-lab programming exam. This is the Exam in Programming Proficiency.

CS 320 Midterm Exam Solution

بسم اهلل الرمحن الرحيم

Condition Controlled Loops. Introduction to Programming - Python

CS 231 Data Structures and Algorithms, Fall 2016

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Chapter 3 Structured Program Development

Midterm CSE 21 Spring 2012

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

CS403: Programming Languages. Assignment 1. Version 2a

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Writeup for first project of CMSC 420: Data Structures Section 0102, Summer Theme: Threaded AVL Trees

Midterm 2. Replace by cover page.

Loops / Repetition Statements

REVIEW. The C++ Programming Language. CS 151 Review #2

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Chapter 1. Numeric Artifacts. 1.1 Introduction

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy.

Chapter 8 Algorithms 1

Transcription:

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, 2018, 12:00 noon NOTE: It is the policy of the Computer Science Department to issue a failing grade to any student who either gives or receives help on any test. A student caught cheating on any question in an exam, project or quiz will fail the entire course. Students who form teams to collaborate on projects must inform the lecturer of the names of all team members ahead of time, else the submissions will be classified as cheating and will receive a failing grade. Any problem to which you give two or more (different) answers receives the grade of zero automatically. Please submit your solution via email, as a file attachment, to Sateesh.Mane@qc.cuny.edu. Please submit one zip archive with all your files in it. 1. The zip archive should have either of the names (CS211 or CS611): StudentId_first_last_CS211_project1_July2018.zip StudentId_first_last_CS611_project1_July2018.zip 2. The archive should contain one text file named Project1.[txt/docx/pdf] and one cpp file per question named Q1.cpp and Q2.cpp etc. 3. Note that not all questions may require a cpp file. In all questions where you are asked to submit programming code, programs which display any of the following behaviors will receive an automatic F: 1. Programs which do not compile successfully (non-fatal compiler warnings are excluded). 2. Array out of bounds, reading of uninitialized variables (including null pointers). 3. Operations which yield NAN or infinity, e.g. divide by zero, square root of negative number, etc. Infinite loops. 4. Programs which do NOT implement the public interface stated in the question. In addition, note the following: 1. All debugging statements (for your personal testing) should be commented out. 2. Program performance will be graded solely on the public interface stated in the questions. 1

Students who did not take Midterm 1 (in class) are not eligible for credit for this project. Students who took the makeup Midterm 1 (take home) are not eligible for credit for this project. 2

General information Ignore the statements below in this section if they are not relevant for the questions in this document. You are permitted to copy and use code supplied in the online lecture notes. 64 bit computers 1. Hardware architecture will not be taken into account in the questions below. 2. Solutions involving the writing of programs will not be judged if they work (or were written) on a 64 bit instead of a 32 bit computer. The following refers to questions involving mathematical calculations. 1. Value of π to machine precision on any computer. 2. Some compilers support the constant M PI for π, in which case you can write const double pi = M_PI; 3. If your compiler does not support M PI, the value of π can be computed via const double pi = 4.0*atan2(1.0,1.0); 3

1 Material to be used in later questions This is material for use in other questions. Nothing to calculate here. Form a set of eight digits (d 1,..., d 8 ) as follows. Take the 8 digits of your student id and define (d 1,..., d 8 ) as follows: d 1 = digit 1 of student id, d 2 = digit 2 of student id, d 3 = digit 3 of student id, d 4 = digit 4 of student id, d 5 = digit 5 of student id, d 6 = digit 6 of student id, d 7 = digit 7 of student id, d 8 = digit 8 of student id. (1.1) For example if your student id is 23054611, then d 1 = 2, d 2 = 3, d 3 = 0, d 4 = 5, d 5 = 4, d 6 = 6, d 7 = 1, d 8 = 1. (1.2) For some student ids, it is possible that some of the digits may be zero. It is also possible that some of the digits may be equal. Do not worry. For the student id 11111111, all the digits are equal. For the student id 33330000, four digits are zero and the other four are all equal to 3. 4

2 Question 2 You will receive zero (or reduced) credit for this question if you did not answer Question 3 in Midterm 1. Consult the definition of the bounding rectangle from Midterm 1. The definition is repeated below. Let (x j, y j ), j = 0,..., n 1 be an arbitrary set of n integer-valued coordinates. Hence the values of x j and y j are integers. In this question, we define the bounding rectangle as follows. 1. The rectangle has horizontal and vertical edges. 2. It is the smallest rectangle which encloses all the points (x j, y j ), j = 0,..., n 1. 3. Let the coordinates of the bounding rectangle be (u 0, v 0 ), (u 1, v 1 ), (u 2, v 2 ) and (u 3, v 3 ). (u 0, v 0 ) = bottom left corner (u 1, v 1 ) = bottom right corner (u 2, v 2 ) = top right corner (u 3, v 3 ) = top left corner Write a function to compute the bounding rectangle of the points (x j, y j ), j = 0,..., n 1. The function signature is given below. Note that the return type is int. int bounding_rect(int n, int x[], int y[], int u[], int v[]); The return value is the area of the bounding rectangle. The formula for the area A is as follows: A = (u 2 u 0 )(v 2 v 0 ). Submit your function code as your solution. 1. Write the function only. 2. Do not write a main program. 3. Do not worry about header files (for math, etc.). Assume they are all given to you. 5

3 Question 3 You are given an input array a of type int and length n. You are also given that the array has no duplicates, i.e. a i a j if i j. Write a function to sum the m largest elements in an array of length n. 1. The function signature is: bool sum_top(int a[], int n, int m, int & sum); 2. The function return type is bool. 3. Safeguard: if n 0 or m 0 or m > n then set sum = 0 and return false. 4. If the calculation is sucessful, then return true. 5. The value of the sum is contained in the output variable sum. There are many ways to implement sum top(...). All correct solutions will be accepted. If you know how to sort an array, you may sort the array a. 1. Then sum the m largest elements in the sorted array. 2. This solution will be accepted. 3. However you must write the sort function yourself. 4. You must also explain the algorithm of the sort function. 5. Calling a black box library function to perform the sort will receive a failing grade. See next page. 6

Here is another possible implementation, which you may use. It is not necessarily the simplest or the fastest. It also has the bad feature that it changes the input array a (it is a partial sort). 1. First implement the safeguard checks described above. 2. Do not calculate anything if the safeguard checks fail (set sum=0 and return false). 3. Find the index of the largest element in the array a, call it say imax. 4. Swap a[n-1] and a[imax]. Then a[n-1] contains the largest element of a. (a) Repeat the procedure as many times as required (b) Next find the index of the largest element in the first n 1 elements of a. (c) Swap a[n-2] and the element that you found. Then a[n-2] contains the second largest element of a. (d) Then find the index of the largest element in the first n 2 elements of a. (e) Swap a[n-3] and the element that you found. Then a[n-3] contains the third largest element of a. 5. Stop the procedure when the m largest elements have been found. 6. If you do your work correctly, the m largest elements of a will be swapped into the locations a[n-1], a[n-2],..., a[n-m]. 7. The required value is sum = a[n 1] + a[n 2] + + a[n m]. 8. Return true and exit the function. The above implementation works, but has the bad side effect that it changes the array a. The calling application may not like that. We were asked to sum the m largest elements of a, not to modify the contents of a. The calling application might wish to use the array for other calculations, and we have disturbed the array. Bonus: Write your implementation such that it does not change the array a. 7

4 Question 4 Write a function to calculate the power of a number x n for a double x and integer n, using the divide and conquer algorithm. double power(double x, int n); The divide and conquer algorithm is explained below. 1. Safeguard: if x == 0 then return 0. Don t waste time on technicalities with zero. 2. If n < 0 then return power(1.0/x, -n). 3. Next, if n == 0 then return 1.0. 4. Otherwise note the following (this is the divide and conquer ). 5. The power function (e.g. math library function pow(x,n) ) has the following property. pow(x, n) = pow(x, n/2) * pow(x, n/2) // n is even pow(x, n) = x * pow(x, n/2) * pow(x, n/2) // n is odd 6. Therefore the calculation of the powers of x can be implemented recursively. Write the function power(double x, int n) and implement the divide and conquer algorithm. Submit your function code as your answer. You need not submit a main program or header files (for math, etc.). 8

5 Question 5 This was the second last question in Midterm 1. It is possible that you ran out of time and did not answer the question in Midterm 1. You will receive credit for submitting an answer to this question even if you did not answer the corresponding question in Midterm 1. The Babylonian algorithm to calculate the square root of a positive number was described in class and is as follows. 1. Let the number be a (you are given that a > 0). 2. Let x 0 = 1. 3. Compute successive iterates x i, i = 1, 2,... via the following rule. x i = 1 2 ( x i 1 + a ). x i 1 4. The iteration clearly goes on forever, so we set a tolerance to stop the iteration. 5. Stop the iteration when x i x i 1 10 4. 6. Return the value of x i as the approximation to the square root a. Write a function to implement the Babylonian algorithm to calculate the square root of a positive number. double sqrt_babylonian(double a); The function return value is the approximation x i to the square root of a. Print the values of the iterates x i inside your function, starting with x 0 = 1. Submit your function code as your answer. You need not submit a main program or header files (for math, etc.). Use your function to compute the square root of your student id. Fill the table below with your list of iterates. State your values to 4 decimal places. i x i 0 1 1 4 d.p. 2 4 d.p. etc. 9

6 Question 6: Mandatory for graduate students (bonus for undergraduates) Write the body of a function with the following signature. bool divisible_9(int n, int &s); You are given an input positive integer n > 0. The decimal digits of n are x 1 x 2 x 3.... The sum of the digits s = x 1 + x 2 +.... If s 9 then return true if s == 9, else return false. If s > 9 then set n = s and call the function recursively until you obtain s 9. 1. For example if n = 999 then proceed recursively until you obtain s = 9 and return true. 2. For example if n = 9876 then proceed recursively until you obtain s = 3 and return false. If you do your work correctly, the function will return true if n is divisible by 9 (and the output will be s = 9) and false if not (and the output will be a value 1 s 8). Apply your function to your student id and state the output. 10

7 Question 7: Mandatory for graduate students (bonus for undergraduates) Write the body of a function with the following signature. bool divisible_11(int n, int &s); You are given an input positive integer n > 0. The decimal digits of n are x 1 x 2 x 3.... The sum of the digits in the odd locations is s 1 = x 1 + x 3 +.... The sum of the digits in the even locations is s 2 = x 2 + x 4 +.... Set s to the absolute difference s = s 1 s 2. If s == 0 then return true. Else if s < 11 return false. If s 11 then set n = s and call the function recursively until you obtain 0 s < 11. 1. For example if n = 65432 then s 1 = 6 + 4 + 2 = 12 and s 2 = 5 + 3 = 8. The absolute difference is s 1 s 2 = 12 8 = 4 hence return false. 2. For example if n = 7909 then s 1 = 7+0 = 7 and s 2 = 9+9 = 18. The absolute difference is s 1 s 2 = 7 18 = 11 hence proceed recursively. The new values are s 1 = 1 and s 2 = 1, hence s 1 s 2 = 1 1 = 0 hence return true. If you do your work correctly, the function will return true if n is divisible by 11 (and the output will be s = 0) and false if not (and the output will be a value 1 s 10). Apply your function to your student id and state the output. 11