CHAPTER 5—FUNCTIONS code;


Code:
/**1.  Refer to the CIRCAREA program in Chapter 2, “C++ Programming Basics.” Write a function
  called circarea() that finds the area of a circle in a similar way. It should take an argument of type float
  and return an argument of the same type. Write a main() function that gets a radius value from the
  user, calls circarea(), and displays the result.*/
#include<iostream>
#include<conio.h>
using namespace std;

float circarea(float radius);

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// circarea.cpp
// demonstrates floating point variables
#include <iostream>                     //for cout, etc.
using namespace std;

int main()
   {
   float rad;                           //variable of type float
   const float PI = 3.14159F;           //type const float

   cout << "Enter radius of circle: ";  //prompt
   cin >> rad;                          //get radius
   float area = PI * rad * rad;         //find area
   cout << "Area is " << area << endl;  //display answer
   return 0;
   }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void main(void)
{
	cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
	cout<<"-------------------------------------------------------------------------------\n"<<endl;
	
	float rad;
	
	do{
	cout<<"Enter radius of circle: "; cin>> rad;
	cout <<"Area is "<<circarea(rad);
	cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
	}while(getch()=='c');
}

float circarea(float radius) { return 3.14159F*radius*radius;}
Code:
/**2.  Raising a number n to a power p is the same as multiplying n by itself p times. Write a function
  called power() that takes a double value for n and an int value for p, and returns the result as a double
  value. Use a default argument of 2 for p, so that if this argument is omitted, the number n will be
  squared. Write a main() function that gets values from the user to test this function.*/
#include<iostream>
#include<conio.h>
using namespace std;

double power(double n, int p=2);

void main(void)
{
	cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
	cout<<"-------------------------------------------------------------------------------\n"<<endl;
	
	double n; int p=2;
	
	do{
	cout<<"Enter n: "; cin>> n;
	cout<<"Enter p: "; cin>> p;
	cout <<"The power is "<<power(n, p);
	cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
	}while(getch()=='c');
}

double power(double n, int p){ for(int ret=1; p>0; p--) ret*=n; return ret;}
Code:
/**3.  Write a function called zeroSmaller() that is passed two int arguments by reference and then sets
  the smaller of the two numbers to 0. Write a main() program to exercise this function.*/
#include<iostream>
#include<conio.h>
using namespace std;

bool zeroSmaller(int& n1, int& n2);

void main(void)
{
	cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
	cout<<"-------------------------------------------------------------------------------\n"<<endl;
	
	int n1, n2;
	
	do{
	cout<<"Enter n1: "; cin>> n1;
	cout<<"Enter n2: "; cin>> n2;
	cout <<"The number assigned to zero is ";
	if(zeroSmaller(n1, n2)) cout<<"n1"; else cout<<"n2";
	cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
	}while(getch()=='c');
}

bool zeroSmaller(int& n1, int& n2){ 
	if(n1<n2) {n1=0; return true;}
	else {n2=0;return false;}}
Code:
/**4.  Write a function that takes two Distance values as arguments and returns the larger one. Include
  a main() program that accepts two Distance values from the user, compares them, and displays the
  larger. (See the retstrc program for hints.).*/
#include<iostream>
#include<conio.h>
using namespace std;

int distWatch(int d1, int d2);

void main(void)
{
	cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
	cout<<"-------------------------------------------------------------------------------\n"<<endl;
	
	int d1, d2;
	
	do{
	cout<<"Enter d1: "; cin>> d1;
	cout<<"Enter d2: "; cin>> d2;
	cout <<"The largest distance is "<<distWatch(d1, d2);
	cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
	}while(getch()=='c');
}

int distWatch(int d1, int d2){ if(d1<d2) return d2; else return d1;}
Code:
/*5.  Write a function called hms_to_secs() that takes three int values—for hours, minutes, and
  seconds—as arguments, and returns the equivalent time in seconds (type long). Create a program
  that exercises this function by repeatedly obtaining a time value in hours, minutes, and seconds from
  the user (format 12:59:59), calling the function, and displaying the value of seconds it returns.*/
#include<iostream>
#include<conio.h>
using namespace std;

long hms_to_secs(int hours, int minutes, int seconds);

void main(void)
{
	cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
	cout<<"-------------------------------------------------------------------------------\n"<<endl;
	
	int h, m, s; char sep;
	
	do{
	cout<<"Enter the time in format hh:mm:ss : "; cin>>h>>sep>>m>>sep>>s;
	cout <<"The equivalent time in seconds is : "<<hms_to_secs(h, m, s);
	cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
	}while(getch()=='c');
}

long hms_to_secs(int hours, int minutes, int seconds){ return 
seconds+minutes*60+hours*3600;}