Functions Exercise

Programming Exercise

Functions Programming Exercise P-3

Array Programming Exercise P-7

Program 22:
Write a program by defining a function “count” and declare a static variable inside the function. Further, initialize the static variable with value 1. When the function is called, it will display a message telling how many times the function has been called.

#include<iostream.h>
#include<conio.h>
main()
{
void count(void);
clrscr();
count();
count();
count();
getch();
}

// definition of count() function 
void count(void)
{
static st = 1;
cout<< "function is called " <<st << " time(s)" <<endl;
st++;
}

Program 23:
Write a program by defining an inline function “kg” to convert pounds into kilograms. Enter value in pounds and pass to the function. The function should calculate the value in kilograms and return to the calling function.
//(Hint, 1 pound = 0.453592 kg)

#include<iostream.h>
#include<conio.h>

inline float kg(float p)
{
return p*0.453592;
}

main()
{
float pounds;
clrscr();
cout<<"Enter weight in pounds ? ";
cin>>pounds;
cout<<pounds<<" pounds = "<<kg(pounds)<<" kilograms";
getch();
}

Program 24:
Write a program by defining an inline function “circle_area” that takes the radius of a circle and returns the calculated area of the circle.
//( Hint: area of circle = PI*R2 )

#include<iostream.h>
#include<conio.h>

inline float circle_area(float r)
{
return 3.1417*r*r;
}

main()
{
float radius;
clrscr();
cout<<"Enter radius of circle ? ";
cin>>radius;
cout<<"Area of circle = "<<circle_area(radius);
getch();
}

Program 25:
Write a program by defining an inline function “power” to find and return the exponential power, i.e. baseexponent. For example, if base =2 and exponent = 3, then the function should return 8.

#include<iostream.h>
#include<conio.h>

inline long power(int b, int p)
{
long i, res = 1;
for(i=1;i<=p;i++)
res =res*b;
return res;
}

main()
{
int base, exponent;
clrscr();
cout<<"Enter base value ? ";
cin>>base;
cout<<"Enter exponential value ? ";
cin>>exponent;
cout<<power(base, exponent);
getch();
}

Program 26:
Write a program by defining a function “time” that takes three default arguments of integer types for hours, minutes and seconds. The function should display the time in a standard format. The function should be called four times as:
// function call without using any argument.
// function call by passing value only of hours as argument.
// function call by passing values of hours and minutes as arguments.
// function call by passing values of hours, minutes and seconds as arguments.

#include<iostream.h>
#include<conio.h>
main()
{
int a, b;
void time(int =12, int =56, int =35);
clrscr();
time();
time(11);
time(10,33);
time(10,45,58);
getch();
}

// definition of time() function 
void time(int h, int m, int s)
{
cout<<h<<":"<<m<<":"<<s<<endl;
}

Program 27:
Write a program by defining functions overloading. The name of the overloaded function is “square”. The prototypes of overloaded functions is:
// void square(void); used to display a solid square of asterisks with side length of 4.
// void square(char); used to display a solid square of specified character with side length of 6.
// void square(char, int); used to display a solid square of specified character and with specified side length of ‘n’.

#include<iostream.h>
#include<conio.h>

void square(void)
{
for(int u=1;u<=4;u++)
{
for(int i=1;i<=4;i++)
cout<<"* ";
cout<<endl;
}
}
void square(char x)
{
for(int u=1;u<=6;u++)
{
for(int i=1;i<=6;i++)
cout<<x<<" ";
cout<<endl;
}
}

void square(char x, int n)
{
for(int u=1;u<=n;u++)
{
for(int i=1;i<=n;i++)
cout<<x<<" ";
cout<<endl;
}
}

main()
{
clrscr();
square();
cout<<endl;
square('y');
cout<<endl;
square('g',10);
getch();
}

Program 28:
Write a program to find greatest number of three numbers using template function.

#include<iostream.h>
#include<conio.h>

template < class N >
N max (N x, N y, N z )
{
N mx = x;
if (y > mx)
mx = y;
if (z > mx)
mx = z;
return mx;
}

main()
{
int a, b, c;
clrscr();
cout<<"Enter three integer values ? " <<endl;
cin>>a>>b>>c;
cout<<"Greatest integer value is: "<<max(a,b,c)<<endl;
double d1, d2, d3;
cout<<"Enter three real values?" <<endl;
cin>>d1>>d2>>d3;
cout<<"Greatest real value is:" <<max(d1,d2,d3)<<endl;
getch();
}

Program 29:
Write a program that inputs data of any type and then shows this data on screen.

#include<iostream.h>
#include<conio.h>

template < class T >
void show(T data )
{
cout<<data<<endl;
}

main()
{
clrscr();
show("Madina is the second-holiest site in Islam after Mecca");
show(2565);
show(15.20);
show('A');
getch();
}

 

Leave a Reply