#include #include #include using namespace std; #include "utility.h" //************************************************************************ // function to compare two double for "equality". Returns true if they // are within TOLERANCE of each other. //************************************************************************ bool dEqual( double x, double y) { return fabs(x-y) < TOLERANCE; } //************************************************************************ // function to exchange the values stored in two integer locations // // Parameter usage : // a,b -- import the numbers to be exchanged and // export the exchanged values // // Post-condition -- the values in the locations will be exchanged //************************************************************************ void swap(int& a, int& b) { int temp = a; a = b; b = temp; } //************************************************************************ // function to exchange the values stored in two floating point locations // // Parameter usage : // a,b -- import the numbers to be exchanged and // export the exchanged values // // Post-condition -- the values in the locations will be exchanged //************************************************************************ void swap(float& a, float& b) { float temp = a; a = b; b = temp; } //************************************************************************ // function to exchange the values stored in two double locations // // Parameter usage : // a,b -- import the numbers to be exchanged and // export the exchanged values // // Post-condition -- the values in the locations will be exchanged //************************************************************************ void swap(double& a, double& b) { double temp = a; a = b; b = temp; } //*********************************************************************** // utility function to generate a random integer between 1 and n inclusive // // Parameter usage : n -- imports the upper bound // // Pre-condition -- n is greater than 0 // Post-condition -- function returns a random integer between 1 and n //************************************************************************* int rand(int n) { return rand()%n + 1; } //*********************************************************************** // utility function to generate a random integer between a and b inclusive // // Parameter usage : // a,b -- import the upper and lower bounds // // Pre-condition -- a and b are greater than 0 // Post-condition -- function returns a random integer between a and b //************************************************************************* int rand(int a, int b) { if (a < b) // a is lower bound and b is upper bound return rand()%(b-a+1) + a; else // a >= b so b is lower bound and a is upper bound return rand()%(a-b+1) + b; } //************************************************************************** // Function to round a value to n decimals // // parameter usage : // value - imports the value to be rounded // n - imports the number of desired decimal places // // post-condition -- returns the rounded value //************************************************************************** double round(double value, int n) { //calculate 10^n to multiply and divide by double power = 1; // 10^0 = 1; for (int i = 1; i <= n; i++) power*=10.0; int temp = static_cast(value*power + .5); return temp/power; } //************************************************************************** // Function to return the distance between two points // // parameter usage : // x1, y1 -- import the first point // x2, y2 -- import the second point //************************************************************************** double distance(double x1, double y1, double x2, double y2) { return sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ); } //************************************************************************** // Function to return true is n is prime // // a number is prime if it is not evenly divisible by // any number less than it. In this version we only check // divisors up to the square root of n. //************************************************************************** bool prime(int n) { bool pri = true; // assume that it is prime and prove false if (n == 1) pri = false; // 1 is not prime int divisor = 2; while (divisor*divisor <= n && pri) { pri = n % divisor != 0; divisor++; } return pri; } //************************************************************************** // Function to return true if the character is a vowel // // parameter usage : // ch -- imports the character to check // // post-condition -- returns true if ch is a vowel and false otherwise //************************************************************************** bool isVowel(char ch) { ch = toupper(ch); // avoid case sensitivity return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'; }