CSCI 135 Software Design and Analysis, C++ Homework 1 Solution

Similar documents
CSCI 135 Software Design and Analysis, C++ Homework 5 Solution

CSCI 135 Software Design and Analysis, C++ Homework 2 Due 2/21/2014

CSCI 135 Software Design and Analysis, C++ Homework 8 Solution

do { statements } while (condition);

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

CSci 1113, Spring 2018 Lab Exercise 3 (Week 4): Repeat, Again and Again

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

CSE100 Principles of Programming with C++

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

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

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions)

CSI33 Data Structures

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

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

EP578 Computing for Physicists

Fortran 90 Two Commonly Used Statements

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

REPETITION CONTROL STRUCTURE LOGO

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

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Programming Language. Functions. Eng. Anis Nazer First Semester

A SHORT COURSE ON C++

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

Homework 2 Solutions Group B 1- Write a C++ program to read a students score in Statistics and print if he is successful or failing.

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

Exercises C-Programming

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Why Is Repetition Needed?

Introduction to Programming I COS1511 School of Computing Revision Notes

Fondamenti di Informatica

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Lab Instructor : Jean Lai

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Simplifying Square Root Expressions[In Class Version][Algebra 1 Honors].notebook August 26, Homework Assignment. Example 5 Example 6.

CSCI 111 Midterm 1 Exam Spring Solutions 09.00am 09.50am, Monday, March 21, 2016

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

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

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

CS242 COMPUTER PROGRAMMING

CS 101 Computer Programming and Utilization. Lecture 5, More Numerical computing (Slides courtesy Prof Ranade)

C++ Programming: From Problem Analysis to Program Design, Third Edition

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

CS 215 Fundamentals of Programming II Spring 2011 Project 2

Functions and Recursion

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Intro to Computational Programming in C Engineering For Kids!

Discrete Mathematics Introduction

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Warm Up. Factor the following numbers and expressions. Multiply the following factors using either FOIL or Box Method

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Name SECTION: 12:45 2:20. True or False (12 Points)

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

1.1 - Introduction to Sets

Introduction to Algorithms and Programming (COMP151)

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

Chapter 7. Iteration. 7.1 Multiple assignment

Operators and Expressions:

A Freshman C++ Programming Course

Functional Programming in Haskell Part I : Basics

PIC 10B Lecture 1 Winter 2014 Homework Assignment #2

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

To become familiar with array manipulation, searching, and sorting.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions

True or False (12 Points)

Lecture 22. While Loops

After an "Hour of Code" now what?

CSCI 111 Second Midterm Exam Spring Solutions 09.05am 09.55am, Monday, May 07, 2018

The following expression causes a divide by zero error:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Unit 7. 'while' Loops

Problem 1 Write the best title lines for the functions that are called by the following main program. Do not supply blocks for the functions.

CSCI Wednesdays: 1:25-2:15 Keller Thursdays: 4:00-4:50 Akerman 211

Review Questions I Spring 2010

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

CSCE Practice Midterm. Data Types

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Chapter 3 - Functions

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Something we should know

Chapter 1. Fundamentals of Higher Order Programming

CH 21 CONSECUTIVE INTEGERS

Expressions & interactions Conditionals Solving a problem. binary representation (8 bits)

CS 007A Midterm 1 Practice Chapters 1 5

UNC Charlotte 2010 Comprehensive

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Transcription:

CSCI 135 Software Design and Analysis, C++ Homework 1 Solution Saad Mneimneh Hunter College of CUNY PART I The purpose of PART I is to practice: input/output if statements and constructing the appropriate logic that is needed to solve the problem writing functions and passing values Problem 1: Intervals For this problem, assume all parameters are integers. An interval [a, b] represents the set of numbers between a and b inclusive. If a > b, we assume that the interval (set) is empty. (a) Write a function called intervalempty that takes a and b as parameters and returns true if [a,b] is empty and false otherwise. bool intervalempty(int a, int b) { return (a>b); (b) Write a function called intervalintersect that takes a, b, c, and d as parameters, and: outputs the intersection of intervals [a, b] and [c, d] as an interval. Use [1,0] to denote an empty intersection. returns the number of elements that belong to both intervals [a,b] and [c,d]

int intervalintersect(int a, int b, int c, int d) { int low; int high; //the larger of a and c if (a<=c) low=c; low=a; //the smaller of b and d if (b<=d) high=b; high=d; //check if intersection is empty if (intervalempty(low, high)) { low=1; high=0; cout<< [ <<low<<, <<high<< ] ; return high-low+1; (c) In the main function, write a program to prompt the user to input a, b, c, and d and output: whether [a,b] is empty or not whether [c,d] is empty or not the intersection of [a,b] and [c,d] and the number of integer elements in that intersection Example: If the two intervals are [1,0] and [2,3]: Interval [1,0] is empty Interval [2,3] is not empty The intersection of [1,0] and [2,3] is [1,0] with 0 integer elements Example: If the two intervals are [1,10] and [5,12]: Interval [1,10] is not empty Interval [5,12] is not empty The intersection of [1,10] and [5,12] is [5,10] with 6 integer elements Example: If the two intervals are [1,2] and [4,6]: Interval [1,2] is not empty Interval [4,6] is not empty The intersection of [1,2] and [4,6] is [1,0] with 0 integer elements

int main() { int a; int b; int c; int d; cout<<"input 4 integers to make two intervals [a,b] and [c,d]: "; cin>>a; cin>>b; cin>>c; cin>>d; cout<<"interval ["<<a<<, <<b<<"] is "; if (intervalempty(a,b)) cout<<"empty\n"; cout<<"not empty\n"; cout<<"interval ["<<c<<, <<d<<"] is "; if (intervalempty(c,d)) cout<<"empty\n"; cout<<"not empty\n"; cout<<"the intersection of ["<<a<<, <<b<<"] and ["<<c<<, <<d<<"] is "; int n=intervalintersect(a,b,c,d); cout<<" with "<<n<<" integer elements\n"; PART II The purpose of PART II is to practice: loops simple conditionals writing functions and passing values Problem 2: Fair and Square... (a) Write a function called square2 that takes an integer n as a parameter and returns the sum of the first n odd numbers starting from 1 to and ending in 2n 1. here are two possible solutions. int square2(int n) { int s=0; for (int i=1; i<=n; i=i+1) //loop n times s=s+2*i-1; //the i^th odd number is 2i-1 return s;

int square2(int n) { int s=0; int i=1; //start with the first odd number while (i<=2*n-1) { //as long as less of equal to 2n-1 s=s+i; i=i+2; //increment by 2 to get the next odd number return s; (b) Compare this function to the function square that we have seen in class. To do this, verify in main that both functions return the same value for all n = 0...100. One way is to print the values side by side in a loop. [optional] Try to find a better way using a loop and an if statement. here are two solutions. The first outputs the results side by side, the second uses if. int main() { for (int i=0; i<=100; i=i+1) { cout<<square(i)<< ; cout<<square2(i)<< \n ; int main() { bool agree=true; for (int i=0; i<=100; i=i+1) if (square(i)!=square2(i) agree=false; if (agree) cout<<"both functions agree on all inputs from 0 to 100\n"; cout<<"the two functions do not agree on all inputs from 0 to 100\n"; Where the square function is as seen in class: int square(int n) { return n*n;

Problem 3: Square root We have seen in class a function to compute the square root of a number x based on Newton s method: bool closeenough(float a, float b) { return (-0.001<=a-b && a-b<=0.001); float sqrt(float x, float guess) { while (!closeenough(guess*guess, x) { cout<<guess<< \n ; //not needed, but to see //how guess is changing guess = (guess + x/guess)/2; return guess; Implement a sqrt function based on the following idea: we bound the square root of x from the left and the right. Initially, the square root of x must satisfy: 0 x max(x,1) So if we initially let a = 0 and b = max(x,1), then the square root of x is in the interval [a,b]. To assign b, an if statement can compare x to 1. Now let m be the middle point of the interval [a,b] (we can use the average function to find it). While m 2 is not close enough to x we repeatedly perform the following (otherwise, we return m): if m 2 x, we assign a the value of m, i.e. the interval becomes [m,b] if m 2 x, we assign b the value of m, i.e. the interval becomes [a,m] update m to be the middle of the interval [a,b] Therefore, in addition to m, we need two variables to keep track of how the interval is changing. Note 1: We exit the loop when m 2 is close enough to x, say within 0.001. Note 2: The size of the bounding interval is halfed each time, but mathematically Newton s method converges faster. To check this, insert a cout statement as illustrated above to track the ietrations, and try both functions to compare the number of iterations (for the first version, you may start with x itself as the guess). Example: Here s how the interval and m change when computing the square root of x = 0.5.

[a,b] m m^2 x --------------------------------- ------------------- [0,1] 0.5 0.25 < 0.5 [0.5,1] 0.75 0.5625 > 0.5 [0.5,0.75] 0.625 0.390625 < 0.5 [0.625,0.75] 0.6875 0.472656 < 0.5 [0.6875,0.75] 0.71875 0.516602 > 0.5 [0.6875,0.71875] 0.703125 0.494385 < 0.5 [0.703125,0.71875] 0.710938 0.505432 > 0.5 [0.703125,0.710938] 0.707031 0.499893 < 0.5 float average(float x, float y) { return (x+y)/2; float sqrt(float x) { float a=0; float b; if (x>1) b=x; b=1; float m=average(a,b); //or simply (a+b)/2; while (!closeenough(m*m,x)) { if (m*m<=x) a=m; if (m*m>=x) b=m; m=average(a,b); //or simply (a+b)/2; return m; Instructions to submit homework Have a separate program for each problem. For each program, upload it to the following website: http://www.cs.hunter.cuny.edu/~saad/courses/c++/taxi.html If your program compiles successfully, you will receive a 5-digit TAXI code. Put this TAXI code as a comment in the beginning of the corresponding C code file. // TAXI code here #include <iostream> using... //the rest of the file... Submit the file through Blackboard. You will find an appropriate column to upload it in the Grade Center under the Assignments section.