// File: solvers.cxx // Written by: Michael Main (main@colorado.edu) on Sep 16, 2011 // The purpose of this program is to show // how to create functions that solve equations //----------------------------------------------------------------------------- // Directives: #include #include #include #include #include using namespace std; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Function Prototypes. // The dist function finds the distance between two points (x1, y1) and // (x2, y2) with integer coordinates, for instance two pixels on the screen. double distance(double x1, double y1, double x2, double y2); // This function computes an approximation to e by adding the reciprocals // of the first n factorials. double e_approximation(int s); // The fahr_to_cell function converts Fahrenheit temperature f to Celsius. // Precondition: f >= -459.67 double fahr_to_cel (double f); // This function computes an approximation to pi using a n-random points // that are dropped on a square of size s by s. // Note: This is a different algorithm than the one you use in Homework 3! // Precondition: n > 0 double pi_approximation(int s, int n); // Use an iterative algorithm to solve x = a*x + b. Again, do not use this // in real life (it is too slow and it works only when 0 <= a < 1). // But it does illustrate iteration nicely. // Precondition: 0 <= a < 1. double solution(double a, double b); // Use an iterative algorithm along with the sqrt function to find the // cube root of x. // Precondition: 0 <= x double third_root(double z); //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- int main() { //double x; // Illustrate getting input from the console input: //cout << "Please enter a value: "; //cin >> x; //cout << "You chose " << x << endl; // Use that input as the argument to several functions: //cout << "Converted to fahrenheit: " << fahr_to_cel(x) << endl; //cout << "Sine of that value is: " << sin(x) << endl; //cout << "Absolute value is: " << abs(x) << endl; // Now just test the other functions with hard-coded arguments: cout << "distance: " << distance(50, 70, 250, 20) << endl; cout << "solution: " << solution(0.5, 4.0) << endl; cout << "cube root: " << third_root(1000) << endl; cout << "Pi estimate: " << pi_approximation(400, 20000) << endl; //cout << "Approximation of e: " // << e_approximation(400) << endl; return EXIT_SUCCESS; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- double distance(double x1, double y1, double x2, double y2) { return pow(pow(x1-x2, 2) + pow(y1-y2, 2), 0.5); } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- double e_approximation(int n) { double denominator = 1.0; double sum = 0.0; int many_terms = 0; while (many_terms < n) { sum += 1.0/denominator; ++many_terms; denominator = denominator*many_terms; } return sum; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- double fahr_to_cel (double f) { return 0; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- double pi_approximation(int s, int n) { int count = 0; //How many points we have looked at so far int count_red = 0; // How many red points have we seen int px, py; // Coordinates of a pixel initwindow(s, s, "PI!!!"); setfillstyle(SOLID_FILL, RED); setcolor(RED); fillellipse(s/2, s/2, s/2, s/2); while (count < n) { px = rand( ) % s; // Pick a random x pixel from 0 to s-1 py = rand( ) % s; // Pick a random y pixel if (getpixel(px, py) == RED) { ++count_red; } ++count; } closegraph(); return 4.0*count_red/count; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- double solution(double a, double b) { assert(a != 1); //return b / (1-a); double x = 42; double right = a*x + b; double diff = abs(right - x); while (diff > 0.0001) { x = right; right = a*x + b; diff = abs(right - x); } return x; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- double third_root(double z) { double x = 42; double right = sqrt(z/x); double diff = abs(right - x); while (diff > 0.0001) { x = right; right = sqrt(z/x); diff = abs(right - x); } return x; } //-----------------------------------------------------------------------------