Download presentation
Presentation is loading. Please wait.
Published byRuby Caldwell Modified over 8 years ago
1
Functions and Libraries
2
Reference parameters void setToZero(int var) { var = 0; } int main() { int var = 0; setToZero(var); cout << var; return 0; }
3
Reference parameters void setToZero(int & var) { var = 0; } int main() { int var = 0; setToZero(var); cout << var; return 0; }
4
/* * File: Quadratic.cpp * ------------------- * This program finds roots of the quadratic equation * * 2 * a x + b x + c = 0 * * If a is 0 or if the equation has no real roots, the * program prints an error message and exits. */ #include using namespace std;
5
/* Function prototypes */ void getCoefficients(double & a, double & b, double & c); void solveQuadratic(double a, double b, double c, double & x1, double & x2); void printRoots(double x1, double x2); void error(string msg);
6
/* Main program */ int main() { double a, b, c, r1, r2; getCoefficients(a, b, c); solveQuadratic(a, b, c, r1, r2); printRoots(r1, r2); return 0; }
7
void getCoefficients(double & a, double & b, double & c) { cout << "Enter coefficients :" << endl; cout << "a: "; cin >> a; cout << "b: "; cin >> b; cout << "c: "; cin >> c; }
8
void solveQuadratic(double a, double b, double c, double & x1, double & x2) { if (a == 0) error("The coefficient a must be nonzero."); double disc = b * b - 4 * a * c; if (disc < 0) error("This equation has no real roots."); double sqrtDisc = sqrt(disc); x1 = (-b + sqrtDisc) / (2 * a); x2 = (-b - sqrtDisc) / (2 * a); }
9
void printRoots(double x1, double x2) { if (x1 == x2) { cout << "There is a double root at " << x1 << endl; } else { cout << "The roots are " << x1 << " and " << x2 << endl; }
10
void error(string msg) { cerr << msg << endl; exit(EXIT_FAILURE); }
11
std::cerr Standard output stream for errors cerr is an object of class ostream that represents the standard error stream. It is associated with the cstdio streamstderr.cstdiostderr By default, most systems have their standard error output set to the console, where text messages are shown, although this can generally be redirected
12
void exit ( int status ); Terminate calling process If status is zero or EXIT_SUCCESS, a successful termination status is returned to the host environment.EXIT_SUCCESS If status is EXIT_FAILURE, an unsuccessful termination status is returned to the host environment.EXIT_FAILURE Otherwise, the status returned depends on the system and library implementation.
13
Define a library in C++ You must supply 2 parts 1.Define the interface : provide the information to clients. 2.Provide the implementation : the underlying details. The interface may export functions, types, or constants.
14
/* * File: error.h * ------------- * This file defines a simple function for reporting errors. */ #ifndef _error_h #define _error_h /* * Function: error * Usage: error(msg); * ------------------ * Writes the string msg to the cerr stream and then exits the program * with a standard status code indicating failure. The usual pattern for * using error is to enclose the call to error inside an if statement that * checks for a particular condition, which might look something like this: * * if (divisor == 0) error("Division by zero"); */ void error(std::string msg); #endif
15
/* * File: error.cpp * --------------- * This file implements the error.h interface. */ #include #include "error.h" using namespace std; /* * Implementation notes: error * --------------------------- * This function writes out the error message to the cerr stream and * then exits the program. The EXIT_FAILURE constant is defined in * to represent a standard failure code. */ void error(string msg) { cerr << msg << endl; exit(EXIT_FAILURE); }
16
/* * File: direction.h */ #ifndef _direction_h #define _direction_h #include enum Direction { NORTH, EAST, SOUTH, WEST }; Direction leftFrom(Direction dir); Direction rightFrom(Direction dir); std::string directionToString(Direction dir); #endif
17
/* * File: direction.cpp * ------------------- * This file implements the direction.h interface. */ #include #include "direction.h" using namespace std; Direction leftFrom(Direction dir) { return Direction((dir + 3) % 4); } Direction rightFrom(Direction dir) { return Direction((dir + 1) % 4); }
18
string directionToString(Direction dir) { switch (dir) { case NORTH: return "NORTH"; case EAST: return "EAST"; case SOUTH: return "SOUTH"; case WEST: return "WEST"; default: return "???"; }
19
/* * File: gmath.h * ------------- */ #ifndef _gmath_h #define _gmath_h extern const double PI; double sinDegrees(double angle); double cosDegrees(double angle); double toDegrees(double radians); double toRadians(double degrees); #endif
20
/* * File: gmath.cpp */ #include #include "gmath.h“ extern const double PI = 3.14159265358979323846; double sinDegrees(double angle) { return sin(toRadians(angle)); } double cosDegrees(double angle) { return cos(toRadians(angle)); } double toDegrees(double radians) { return radians * 180 / PI; } double toRadians(double degrees) { return degrees * PI / 180; }
21
Principles of interface design Unified – clear unifying theme Simple – hide the complexity from the client Sufficient – functionality to meet user needs General – meet the needs of many clients Stable – have the same structure and effect, even if the underlying implementation change
22
#include using namespace std; const int N_TRIALS = 10; int main() { cout << "On this computer, RAND_MAX is " << RAND_MAX<< endl; cout << "The first " << N_TRIALS << " calls to rand:" << endl; for (int i = 0; i < N_TRIALS; i++) { cout << setw(10) << rand() << endl; } return 0; }
23
On this computer, RAND_MAX is 2147483647 The first 10 calls to rand: 1103527590 377401575 662824084 1147902781 2035015474 368800899 1508029952 486256185 1062517886 267834847
24
Choosing the right set of functions The ability to select a random integer in a specified range. The ability to choose a random real number in a specified range. The ability to simulate a random event with a specific probability.
25
/* * File: random.h * -------------- * This interface exports functions for * generating pseudorandom numbers. */ #ifndef _random_h #define _random_h int randomInteger(int low, int high); double randomReal(double low, double high); bool randomChance(double p); void setRandomSeed(int seed); #endif
26
/* * File: random.cpp * ---------------- * This file implements the random.h interface. */ #include #include "random.h" using namespace std; /* Private function prototype */ void initRandomSeed();
27
int randomInteger(int low, int high) { initRandomSeed(); double d = rand() / (double(RAND_MAX) + 1); double s = d * (double(high) - low + 1); return int(floor(low + s)); } floor(x) the largest integral value that is not greater than x e.g. floor of 2.3 is 2.0, floor of 3.8 is 3.0, floor of -2.3 is -3.0, floor of -3.8 is -4.0 ceil(x) the smallest integral value that is not less than x e.g. ceil of 2.3 is 3.0, ceil of 3.8 is 4.0, ceil of -2.3 is -2.0, ceil of -3.8 is -3.0
28
double randomReal(double low, double high) { initRandomSeed(); double d = rand() / (double(RAND_MAX) + 1); double s = d * (high - low); return low + s; }
29
void setRandomSeed(int seed) { initRandomSeed(); srand(seed); } void initRandomSeed() { static bool initialized = false; if (!initialized) { srand(int(time(NULL))); initialized = true; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.