//The following is a collection of function examples (most of
//which were presented in class) bundled together as if part of
//a single program. The "main" function contains sample calls
//to the functions. You might copy+paste part or all of this
//code into the compiler to watch it run or to use as a basis
//for your own functions. Or you might just print it out.
#include <iostream.h>
#include <stdlib.h>
//add to numbers
double Plus(double a, double b)
{
double sum;
sum=a+b;
return sum;
}
//cause internal speaker to beep
void Beep()
{
cout << "\a";
}
//return the value of PI
double PI()
{
return 3.14159265;
}
//make x in the range [min,max]
double clamp(double x, int min, int max)
{
if (x<min) return min;
if (x>max) return max;
return x;
}
//return maximum of a and b
double maximum(double a, double b)
{
if (a>=b) return a; else return b;
}
//simulate rolling a die with given number of sides
int RollDie(int sides)
{
return (rand()%sides)+1;
}
//returns an approximation to cos(a), if -1<a<1
double my_cos(double a)
{
if ((a<=-1)||(a>=1)) return 1000; //send back a nonsense value
return 1-a*a/2+a*a*a*a/24;
}
//displays day of week, using a fixed correspondence
void PrintDay(int day)
{
switch (day) {
case 0: cout << "Monday\n"; break;
case 1: cout << "Tuesday\n"; break;
case 2: cout << "Wednesday\n"; break;
case 3: cout << "Thursday\n"; break;
case 4: cout << "Friday\n"; break;
case 5: cout << "Saturday\n"; break;
case 6: cout << "Sunday\n"; break;
}
}
//return greatest integer less than or equal to x
int floor(double x)
{
int i=x; //type conversion causes numbers after decimal to be lost
if (x>=0) return i;
return i-1;
}
//return x rounded to nearest integer
int round(double x)
{
return floor(x+0.5);
}
//return x raised to the power of p
double power(double x, int p)
{
double answer=1;
int c=1;
while (c<=p) {
answer=answer*x;
++c;
}
return answer;
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// recursion
double recursive_power(double x, int p)
{
if (p==0) return 1; else return x*recursive_power(x,p-1);
}
double factorial(int n)
{
if (n<=1) return 1; else return n*factorial(n-1);
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// TRUE and FALSE
//C is designed so that 0 represents "false" (in IF statements, etc.)
//and nonzero represents "true"; can actually assign the names given
//below and use them to make programs clearer
const int FALSE=0;
const int TRUE=1; //anything nonzero
//does a equal b?
int equals(double a, double b)
{
if (a==b) return TRUE; else return FALSE;
}
//is ch a captial letter?
int IsCapital(char ch)
{
if ((ch>='A')&&(ch<='Z')) return TRUE; else return FALSE;
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// examples of calling the above functions
void main()
{
cout << Plus(4,2) << endl;
Beep();
cout << PI() << endl;
double y,x=-4.231;
x=clamp(x,1,10);
cout << maximum(x,0) << endl;
cout << RollDie(6) << endl;
cout << RollDie(6) << endl;
cout << RollDie(6) << endl;
y=my_cos(PI()/6);
cout << y << endl;
PrintDay(3);
cout << floor(6.782) << endl;
cout << round(3.513) << endl;
cout << power(2,5) << endl;
cout << recursive_power(2,5) << endl;
cout << factorial(5) << endl;
if (equals(3.14,3.14)) { //test succeeds because 3.14==3.14
cout << "The numbers are equal!\n";
}
if (IsCapital('M')) Beep(); //does beep
if (IsCapital('d')) Beep(); //does not beep
if (!IsCapital('d')) Beep(); //does beep, because "!" means "not"
}