Programming Project #2: Solving Quadratic Equations Date Due: Monday 25 September 2017

Similar documents
MA 1128: Lecture 02 1/22/2018

Floating-point numbers. Phys 420/580 Lecture 6

Numerical Computing: An Introduction

MAT 003 Brian Killough s Instructor Notes Saint Leo University

CHAPTER 2 SENSITIVITY OF LINEAR SYSTEMS; EFFECTS OF ROUNDOFF ERRORS

Section 1.1 Definitions and Properties

MAT128A: Numerical Analysis Lecture Two: Finite Precision Arithmetic

Summer Math Assignments for Students Entering Algebra II

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

Summer Math Assignments for Students Entering Integrated Math

Algebra 2 Common Core Summer Skills Packet

Floating-Point Numbers in Digital Computers

Math 1: Solutions to Written Homework 1 Due Friday, October 3, 2008

Divisibility Rules and Their Explanations

GSE Algebra 1 Name Date Block. Unit 3b Remediation Ticket

Python Programming: An Introduction to Computer Science

Project 1: Part 1. Project 1 will be to calculate orthogonal polynomials. It will have several parts. x = b± b 2 4ac., x 2 = b b 2 4ac.

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

Floating-Point Numbers in Digital Computers

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

What Every Programmer Should Know About Floating-Point Arithmetic

Project 1: Part 1. x = b± b 2 4ac., x 2 = b b 2 4ac.

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology

CSE100 Principles of Programming with C++

Scientific Computing: An Introductory Survey

UNIT 8: SOLVING AND GRAPHING QUADRATICS. 8-1 Factoring to Solve Quadratic Equations. Solve each equation:

Exploring Fractals through Geometry and Algebra. Kelly Deckelman Ben Eggleston Laura Mckenzie Patricia Parker-Davis Deanna Voss

HW DUE Floating point

Section 0.3 The Order of Operations

!"!!!"!!"!! = 10!!!!!(!!) = 10! = 1,000,000

College Algebra Extra Credit Worksheet

3x - 5 = 22 4x - 12 = 2x - 9

Polynomial and Rational Functions

3x 2 + 7x + 2. A 8-6 Factor. Step 1. Step 3 Step 4. Step 2. Step 1 Step 2 Step 3 Step 4

Trombone players produce different pitches partly by varying the length of a tube.

Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CITS 1401 Problem Solving & Programming

, has the form T i1i 2 i m. = κ i1i 2 i m. x i1. 1 xi2 2 xim m (2)

2 Computation with Floating-Point Numbers

2 Computation with Floating-Point Numbers

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

6.01, Spring Semester, 2008 Assignment 3, Issued: Tuesday, February 19 1

Binary, Hexadecimal and Octal number system

+ bx + c = 0, you can solve for x by using The Quadratic Formula. x

Algebra 1. Standard 11 Operations of Expressions. Categories Combining Expressions Multiply Expressions Multiple Operations Function Knowledge

Math 135: Intermediate Algebra Homework 10 Solutions December 18, 2007

Properties and Definitions

Introduction to Computer Programming for Non-Majors

UNIT 5 QUADRATIC FUNCTIONS Lesson 1: Interpreting Structure in Expressions Instruction

Skill 1: Multiplying Polynomials

Operator overloading

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

AP Computer Science Java Mr. Clausen Program 6A, 6B

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.

Review Questions 26 CHAPTER 1. SCIENTIFIC COMPUTING

Mark Important Points in Margin. Significant Figures. Determine which digits in a number are significant.

More Ways to Solve & Graph Quadratics The Square Root Property If x 2 = a and a R, then x = ± a

Algebra 2 Semester 1 (#2221)

Structures of Expressions

i W E I R D U T O P I A i

Introduction to Computer Programming for Non-Majors

(Refer Slide Time: 02:59)

Introduction to Computer Programming for Non-Majors

Introduction to Scientific Computing Lecture 1

A-SSE.1.1, A-SSE.1.2-

How to approach a computational problem

CS 542G: Preliminaries, Floating-Point, Errors

School Year:

C++ for Java Programmers

1. Answer: x or x. Explanation Set up the two equations, then solve each equation. x. Check

Introduction to Computer Engineering (E114)

1.7 Limit of a Function

9 R1 Get another piece of paper. We re going to have fun keeping track of (inaudible). Um How much time do you have? Are you getting tired?

Question 1. [5 points] Circle and briefly explain the error(s) in the following code:

Unit 4: Multiplication

Learning Packet. Lesson 6 Exponents and Rational Functions THIS BOX FOR INSTRUCTOR GRADING USE ONLY

With the cursor flashing on the word FUNCTION, press the. Section 1. <MODE> key

Computer Systems C S Cynthia Lee

CS321 Introduction To Numerical Methods

hp calculators HP 9s Basic Arithmetic Practice Doing Arithmetic

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

CS 1044 Project 1 Fall 2011

CCNY Math Review Chapter 2: Functions

Course of study- Algebra Introduction: Algebra 1-2 is a course offered in the Mathematics Department. The course will be primarily taken by

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Exponents. Common Powers

x y

Chapter 1. Numeric Artifacts. 1.1 Introduction

Section 1.5 Transformation of Functions

Basics of Computational Geometry

Rev Name Date. . Round-off error is the answer to the question How wrong is the rounded answer?

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

Expression and Equations

Section 1.5 Transformation of Functions

YEAH 2: Simple Java! Avery Wang Jared Bitz 7/6/2018

Python Programming Exercises 1

CPS122 Lecture: From Python to Java

Transcription:

CISC 5300 Programming in C++ Fall, 2017 Programming Project #2: Solving Quadratic Equations Date Due: Monday 25 September 2017 Write a program that prompts the user to enter the coefficients of a quadratic polynomial ax 2 + bx + c. It should then solve the polynomial equation ax 2 + bx + c = 0. You should use the quadratic formula to solve this equation; if you don t remember it, you ll find it later on in this handout. In what most people consider to be the normal case, there are two real roots, as in the case x 2 3x+2 = 0. However, there is the possibility of only one real root, 1 as in the case x 2 2x + 1 = 0, and no real roots, as with x 2 + 1 = 0. Since you can t control the input your users are giving you, think about what to do if a = 0. (Note that the equation is now a linear equation, provided that b 0.) A few considerations: 1. You are to do this using only the techniques found in Chapters 1 through 4 of the text. 2. You should do this project, in a subdirectory of your /private/programming-c++ directory. See the Project 1 handout for further discussion, and adjust accordingly. 3. I am providing you with the following goodies, to help you get started. An executable version of this program for you to try out, named -agw. An executable for a stable version of this program, named proj1-agw-stable. (I ll explain this later on.) If you d like to earn 10 points in extra credit, you can implement a stable quadratic solver. You should do this after you ve done the basic version of this assignment. A stub version of the C++-code, named -stub.cc. I ll also explain this further on. You should copy these files into your working directory, via the cp command. Assuming that you re logged into one of our machines (i.e., on erdos or a lab machine) and that the working directory described in (2) above is your current directory, the command cp agw/class/programming-c++/share//. will do this. The dot is part of the command; it means current working directory. Once you ve copied it over, please try it out, so you can see what I consider to be good input/output behavior, not to mention how I handle various strange data values (as mentioned above). 1 More precisely, it s a repeated real root of multiplicity two.

4. Of course, you ll need to compute the square root of the discriminant. The standard library has a function sqrt() that will be pretty helpful here. Its prototype (which you get for free) is double sqrt(double x); The return value of sqrt(blah) is (not surprisingly) the square root of blah, assuing that blah is non-negative. 2 5. As you know by now, it s a good idea to let a function carry out a well-encapsulated subtask. When done carefully, this can shorten the main() function; my main() is only 21 lines long. I used two subsidiary functions: The function having prototype void solve_linear(double b, double c); solves a linear equation bx + c = 0, distinguishing between the cases b 0 and b = 0. My version was 14 lines long. The function having prototype void solve_quadratic(double a, double b, double c); solves a genuine quadratic equation ax 2 + bx + c = 0, genuine meaning that a 0. Mine was 21 lines long. The main reason for using functions here (other than simply getting practice) is that each function (including the main() function) is small enough to understand easily; in particular, each function takes up less than a screenful of space. At this point, we can talk about -stub.cc. This file takes care of the overall logic flow, declares the two functions mentioned above, and gives stub implementations of these functions, i.e., minimal implementations for the program to compile. Your first step will be to make a copy of -stub.cc, which you ll call.cc. For fun, you should compile this file as is, and see if you understand why it works the way it does, in its limited way. If you now fill in the blanks (which means implementing the input section and completing the implementation of the two functions mentioned above), you ll now have a working program. Big hint: Do one thing at a time. First, take care of the input phase (this should be pretty easy). Run the program, to make sure that this piece works right. Now, implement one of the two functions, after which you should run the program and test that the program works right so far. Finally, implement the other function, and do yet more testing. 6. Here is a good collection of sample data to use. 2 More honestly, the sqrt() function doesn t really return the exact square root of its argument, but an approximation to same. But it s a very good approximation. 2

a b c Equation Explanation 1-3 2 x 2 3x + 2 = 0 Two real roots (x = 1 and x = 2) 2-6 4 2x 2 6x + 4 = 0 Two real roots (x = 1 and x = 2) 1-2 1 x 2 2x + 1 = 0 One double root (x = 1) 1-2 4 x 2 2x + 4 = 0 No real roots 0 2 4 2x + 4 = 0 Linear equation, root x = 2 0 0 0 0 = 0 An identity 0 0 1 1 = 0 A contradiction 7. Use the photo program so you can have something to turn in. Don t do this until the program is working! The commands you should issue are as follows: photo cat.cc g++ -std=c++17 -o.cc exit You ll be running once for each test dataset. Once again, see the Project 1 handout if you re still unclear about photo. Good luck! For 10 points extra credit: The classical quadratic formula is fine if one is doing exact arithmetic of real numbers. For our discussion, we ll think of a real number as being a decimal number an infinite number of places beyond the decimal point. Computers cannot represent such numbers with exact precision; they can only approximate them. The common standard is to approximate real numbers by floating-point numbers. For the purpose of our discussion, we can think of a floating-point number as being a number written in scientific notation (such as 0.6626068 10 34 or 0.6022142 10 24, with a fixed number of digits after the decimal point (in these examples, six places) and a limited range for the exponent (say, from -44 to 38). You ll also notice that there s a zero before the decimal point and that the first place after the decimal point is nonzero; such floating-point numbers are said to be normalized. The common standard is known as ieee 754, which specifies both single-precision and double-precision floating-point arithmetic (which might be implemented via float and double, respectively, by a C++ compiler). What this all means is that floating-point arithmetic is inaccurate, compared to real arithmetic. For example, whereas the commutative laws (for addition and multiplication) hold in floating point, the associative law does not hold; neither does the distributive law. The inaccuracy is generally negligible, but it occasionally can cause bad things to happen. 3

One such example is catastrophic cancellation, which occurs when subtracting nearly equal quantities. Suppose we are using floating-point arithmetic having ten decimal digits worth of accuracy. Let s look at an example, taken from http://www.zipcon.net/ swhite/docs/math/loss of significance.html Let x = 0.1234567891234567890 0.1234567890, and suppose that we approximate x on a machine using floating-point arithmetic with 10-digit accuracy. Our best approximation would be x = 0.1234567891 0.1234567890 = 0.0000000001 = 0.100000000 10 9. But the real answer is 0.0000000001234567890, which is 0.1234567890 10 9 when represented in floating point. So we only have one digit of accuracy in our answer, even though the answer can be represented in floating point with ten digits worth of accuracy. Putting this another way, the relative error of our approximation is x x x 0.18999, which is about 19%. Now how does all this apply to our quadratic equation? sobolev@dsm:$ Enter the coefficients of a quadratic polynomial ax2 + bx +c: a? 1 b? -20000 c? 1.5e-9 Trying to solve the quadratic equation 1xx + -20000x + 1.5e-09 == 0 Two roots, x = 20000 and x = 0 Let s double-check this answer, using Mathematica R : sobolev@dsm:$ math Mathematica 9.0 for Linux x86 (64-bit) Copyright 1988-2013 Wolfram Research, Inc. In[1]:= xˆ2-20000 x + 1.510ˆ-9-9 2 Out[1]= 1.5 10-20000 x + x In[2]:= Solve[%==0, x] -14 Out[2]= {{x -> 7.5 10 }, {x -> 20000.}} In[3]:= Quit So one computed solution (x = 20000) is correct, whereas the relative error in the other computed solution (x = 0) has a relative error of 100%!! 4

The problem here is that the values of a, b, and c were carefully chosen so that although b 2 b 2 4ac in real arithmetic, the floating-point values of b 2 and b 2 4ac are the same (you should check this yourself). Hence the discriminant of this polynomial computes out as being b 2, and so x + = b + b 2 4ac 2a in floating point. What to do? Note that in this case, the other root b + b 2 2a = 0 2 1 = 0 x = b b 2 4ac 2a does not cause a cancellation error (since both b and b 2 4ac are negative). We can calculate the other root by using the fact that the product of the roots must equal c/a, which follows from some simple algebra. 3 Thus we can compute x + = c ax Of course, if b < 0, things will work in the opposite direction. So (finally!) for ten points worth of extra credit, implement this stable version of solve_quadratic(). Sample runs of the program look like the following: sobolev@dsm:$ -stable Enter the coefficients of a quadratic polynomial ax2 + bx +c: a? 1 b? -20000 c? 1.5e-9 Trying to solve the quadratic equation 1xx + -20000x + 1.5e-09 == 0 Using classical formula: Two roots, x = 20000 and x = 0 Using stable formula: Two roots, x = 20000 and x = 7.5e-14 sobolev@dsm:$ -stable Enter the coefficients of a quadratic polynomial ax2 + bx +c: a? 1 b? 20000 c? 1.5e-9 Trying to solve the quadratic equation 1xx + 20000x + 1.5e-09 == 0 Using classical formula: Two roots, x = 0 and x = -20000 Using stable formula: Two roots, x = -7.5e-14 and x = -20000 For your test data, run your program on the test data given above, along with these two additional data sets. Note: If you re going to do a stable version, you should wait until you ve finished the basic version. Having done so, you should call your C++ file -stable.cc, and it should start out as a copy of of.cc. 3 Indeed, since the two roots are x + and x (these are numbers, and not variables!), we have ax 2 + bx + c = a(x x + )(x x ) = a ( x 2 (x + + x )x + (x + x ) ) = ax 2 a(x + + x )x + (ax + x ). Now compare the constant terms to see that c = ax + x, and so x + x = c/a. 5

Here is a listing of -stub.cc: / Project 2: Quadratic Equation Solver Say something here. Author: Harry Q. Bovik, PhD Date: 14 September 2017 / #include <bjarne/std_lib_facilities.h> // function declarations void solve_linear(double b, double c); void solve_quadratic(double a, double b, double c); int main() { // input the coefficients of the polynomial double a, b, c; // coefficients of the polynomial // figure out the rest yourself!! } // handle degenerate case (linear equation) and quit if (a == 0) // linear equation, not quadratic solve_linear(b, c); else // genuine quadratic equation... forge ahead solve_quadratic(a, b, c); // solve the linear equation bx + c == 0 void solve_linear(double b, double c) { cout << "Trying to solve linear equation " << b << "x + " << c << " == 0\n"; // figure out the rest yourself! } // use classical quadratic formula to solve a genuine quadratic equation // axˆ2 + bx + c ==0, with a!= 0 void solve_quadratic(double a, double b, double c) { cout << "Trying to solve the quadratic equation " << a << "xx + " << b << "x + " << c << " == 0\n"; // figure out the rest yourself! } 6