1 / 9

Problem Session

Problem Session. Working in pairs of two, solve the following problem. Problem. Using OCD, design and implement a quadratic library that provides functions to compute the roots of a quadratic equation.

iapplegate
Download Presentation

Problem Session

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem Session Working in pairs of two, solve the following problem...

  2. Problem Using OCD, design and implement a quadratic library that provides functions to compute the roots of a quadratic equation. Your functions should check for likely errors (i.e., a equal to zero, negative b2-4ac, etc.)

  3. OCD: Behavior Our functions should receive a, b, and c from its caller. Each should check that a != 0, compute b2-4ac and check that it is positive, and display diagnostic messages and terminate if either is false. Function Root1() should return the root where addition is performed, while Root2() should return the root where subtraction is performed.

  4. OCD: Objects Description Type Kind Name a double varying a b double varying b c double varying c b2-4ac double varying temp diagnostic string constant --

  5. OCD: Operations Description Predefined? Library? Name receive doubles yes -- -- check preconds yes cassert assert() compute root no -- -- - multiply doubles yes built-in * - add doubles yes built-in + - subtract doubles yes built-in - - divide doubles yes built-in / - square root yes cmath sqrt() return a double yes built-in return

  6. OCD: Algorithm 0. Receive a, b, c. 1. Compute temp = b2-4ac. 2. Assert preconditions are true. 3. Root1: return (-b + sqrt(temp))/2a. Root2: return (-b - sqrt(temp))/2a.

  7. Coding: quadratic.h /* quadratic.h * Author: J. Adams. * Date: january 1998. * Purpose: Prototypes of functions to compute * quadratic roots. */ double Root1(double a, double b, double c); double Root2(double a, double b, double c);

  8. Coding: quadratic.cpp /* quadratic.cpp * Author: J. Adams. * Date: january 1998. * Purpose: Definitions of functions for quadratic roots. */ #include “quadratic.h” // prototypes #include <cmath> // sqrt() #include <cassert> // assert() using namespace std; double Root1(double a, double b, double c) { double temp = b*b - 4*a*c; assert(a != 0 && temp > 0); return (-b + sqrt(temp))/2*a; } // ... Define Root2() here in a similar fashion

  9. Coding: quadratic.doc /* quadratic.doc * Author: J. Adams. * Date: january 1998. * Purpose: Documentation of functions to compute * quadratic roots. */ /****************************************************** * Compute the roots of a quadratic. * * Receive: a, b, c, the coefficients of a quadratic. * * Precondition: a != 0 && b^2-4ac > 0. * * Return: Root1: the “-b + sqrt...” root. * * Root2: the “-b - sqrt...” root. * ******************************************************/ double Root1(double a, double b, double c); double Root2(double a, double b, double c);

More Related