Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Problem Session Working in pairs of two, solve the following problem..."— 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 b 2 -4ac, etc.)

3 OCD: Behavior Our functions should receive a, b, and c from its caller. Each should check that a != 0, compute b 2 -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 b double varying b c double varying c b 2 -4ac double varying temp diagnostic string constant -- a double varying a

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 / return a double yes built-in return - square root yes cmath sqrt()

6 OCD: Algorithm 0. Receive a, b, c. 1. Compute temp = b 2 -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 // sqrt() #include // 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);


Download ppt "Problem Session Working in pairs of two, solve the following problem..."

Similar presentations


Ads by Google