Translate

Saturday 12 May 2012

Some frequently used library functions in C++


These functions are one word functions which can be used to get the output of trigonometric complex functions, some string functions etc. To incorporate these functions proper header files should be included. Some frequently used functions are written below.


Mathematical function

      1.       sqrt(x):- This function provides squareroot of ‘x’. ‘x’ should be greater than or equal to zero.
      2.       pow(a,m):- This function determines a power m i.e. am.


3.       floor(x):- This function provides an integer number less than or equal to x. Basically it is greatest integer function.

4.       ceil(x):- This function provides an integer number greater than or equal to x.

5.       log(x):- This function can be used to determine log x to the base e where x should be greater than 0.

      6.       exp(x):- This function can be used to determine ex where e=2.718

7.       sin(x):- This function determines sine of x. Here x should be in radians.

8.       cos(x):- This function determines cosine of x. Here x should be in radians.

9.       tan(x):- This function determines tangent of x. Here x should be in radians.

10.     atan(x):- This function determines arc tangent of x. Here output is in radians.

11.     abs(n):- It provides absolute value of integer n.

12.     fabs(n):- It provides absolute value of a floating value.

13.     log10(n):- It determines log n to the base 10 where n is greater than 0.

Header file of above functions is <math.h>



Character functions
   
1.       isalpha(c):- It verifies whether the character c is an alphabet or not. It returns ‘1’ if c is an alphabet otherwise ‘0’.

2.       isdigit(c):- It verifies whether the character c is a digit or not. It returns ‘1’ if c is a digit otherwise ‘0’.

3.       isalnum(c):- It verifies whether the character c is an alpha numeric charater or not i.e. a-z, A-Z or 0-9. It returns ‘1’ if c is a digit otherwise ‘0’.

4.       isupper(c):- If c is an uppercase alphabetic letter then it returns 1 otherwise it returns zero.

5.       islower(c):- If c is a lowercase alphabetic letter then it returns 1 otherwise it returns zero.

6.       toupper(c):- It converts lowercase letter into uppercase letter.

7.       tolower(c):- It converts uppercase letter into lowercase letter.

Header file of above functions is <ctype.h>



Other functions

      1.       rand():- This function generates random numbers starting from zero.

2.       srand(n):- This function determines the starting value of the random generations.

3.       exit(n):- This function can be used to terminate execution of a program.
                exit(0) indicates normal program termination.
                exit(1) indicates program termination due to some error.

4.       atoi(s):- This function converts a string into integer.
                atoi(‘45ab’); = 45

Header file of above function is <stdlib.h>


Lets have some example of programs having functions.

Q. Write a program (WAP) to read rectangular coordinate and display equivalent polar coordinates.

/*program to display polar coordinates*/
#include<iostream>
#include<conio.h>
#include<math.h>
void main()
{
                clrscr();                 //To clear screen
                const float pi=3.14;
                float x,y,r,th;
                cout<<“Enter rectangular coordinates”;
                cin>>x>>y;
                r=sqrt(x*x+y*y);
                th=atan(y/x)*180/pi;
                cout<< “Polar coordinates are\n” << r << “(cos”<< th << “+isin” << th << “)”;
                getch()
}

Q. Write a program to read a +ve integer and check whether it is divisible by 4 and 9.

/*program to check divisibility*/
#include<iostream>
#include<conio.h>
void main()
{
                clrscr();
                int n;
                cout<< “Enter the value of n”;
                cin>>n;
                n%36==0 ? cout<< n << “is divisible by 4 and 9” : cout<< n << “is not divisible by 4 and 9.”;
                getch();
}
/*conditional operator is used here*/   
/* if n%36==0 then it will print n is divisible by 4 and 9 otherwise n is not divisible by 4 and 9.*/


Exercise
Q. Write a program to read input in polar coordinates and convert and display output in rectangular coordinates.
Q. Write a program to generate nth term of the sequence 1,4,7,10...
                                hint: an=a+(n-1)*d
Q. Write a program to generate nth term of the sequence 1,2,4,8...
Q. Find the output of following program segment.
1.
 void main()
{
                clrscr();
                int m=4, n=7;
                cout<<m++<<”;”<<++n<<endl;
                cout<<++m<<”;”<<n++<<endl;
                cout<<m++<<”;”<<++n<<endl;
                cout<<m++<<”;”<<n++<<endl;
}

Output
4;8
6;8
6;10
7;10

2.
void main()
{
                int y=10;
                cout<<y++<<endl
                    <<y++<<endl
                    <<y++<<endl
                    <<y++<<endl
                    <<y++;
}

Output
14
14
12
12
10


Q. Evaluate following program C++ statements.
    int p=-7, q=9, r=5, x, y;

    x=p < q && r+4 >= q || r-7 > p;
    y= p * (-12) % q + r / q * p;
    c= q / ( r + 8 – p + q % 2);

    x=1,y=3,c=0



No comments:

Post a Comment

Total Pageviews