Useful Classes and Functions - III


The following topics are covered in this section:


Some compilers may not permit String Objects

The string objects that we discussed above can be created only in new C++ compilers. In older compilers you cannot create an object of type string (this is because the string class is not defined). In such cases you have to use character arrays terminated by a null character for strings. But there are many functions provided for operating on such strings as well. A few of these are discussed below.

These functions were part of the standard C library and it has been incorporated into the C++ library as well. To use these functions, if you are using an old C++ compiler, you should include the header file string.h. If you have a new C++ compiler that supports namespaces, then you should use the header <cstring>

Copying strings and concatenating strings:

For copying we have a strcpy( ) function and for concatenation we have the strcat( ) function.

int main( )
{
char s1[20];
char s2[20];
strcpy(s1,"hi");
strcpy(s2,"bye");
strcat(s1,s2);
cout<<s1;
return 0;
}

The output is:

hibye

The function strcpy ( ) will act only on character arrays. Similarly the concatenation function strcat ( ) will also act only on two character arrays. You cannot create a string object and then use strcpy ( ).

string s1;
strcpy(s1,"hi");

will lead to an error (because s1 is not declared as a character array).

String length: This function will give you the length of the character array.

    strlen(character-array)

String Comparison:

The function will return an integer. The syntax for the function is:

    int strcmp(char-array s1, char-array s2);

If s1>s2, then the result will be 1. If s1<s2, then the integer returned will be negative. If s1=s2, then the result will be 0.

There are many other functions that you might find useful but make sure as to whether you can create a string object or whether you have to work with character arrays. Depending on what you use, you will have different functions available.

Remember: New C++ compilers that permit the creation of string objects will also support all the old functions available for character arrays. You should be careful that you don’t use those functions on string objects.

Character conversion functions:

There are two functions for converting a character into uppercase or into lowercase. The two functions are:

    int toupper (int ch);
    int tolower (int ch);

These functions are defined in the header file <ctype.h>

char letter = 'a';
letter = toupper(letter);
cout<<letter; // ‘letter’ is now ‘A’


Mathematical Functions

In many of your program you might want to perform some special mathematical operations (other than the basic ones). For this purpose it will be helpful if you know about the existing mathematical functions provided in C++. In older compilers you have to make use of the math.h header file. If you are using a new compiler and adding headers through namespaces, then you should include the header <cmath> 

Descriptive Name

Purpose

Syntax

Similar functions

Remarks

Trignormetric functions (and hyperbolic)

calculate the cosine of a given angle

double cos(double angle)

 

sin, tan, cosh, sinh, tanh

Remember to give the angle in radians (and not in degrees). Radians are in terms of the constant ‘pi’ (one radian equal 180 degrees).

Inverse trigonometric functions

returns the value of the angle in radians

double acos(double value)

 

asin, atan

It is the opposite of the cos, sin and tan functions. Value should be between –1 and 1 when using acos ( ) or asin ( ) functions.

Hypotenuse

Pass two arguments, this function will find the hypotenuse.

double hypot (double val1, double val2)

 

 

For example: if we code:

cout<<hypot(3,4);

the output will be 5.

Square root

Returns the square root of the given number.

double sqrt (double val)

 

 

 

Logarithm

Functions to calculate natural logarithm and also base 10 logarithms.

double log (double val)

To calculate natural logarithm of ‘val’.

double log10 (double val)

To calculate logarithm of ‘val’ to the base 10.

log10(10) = 1

log(10) = 2.3

 

Absolute value

Two functions to obtain the absolute value of a number. Absolute value means only positive numbers.

int abs (int val)

The abs( ) function will operate on integers and return integers.

double fabs (double val)

To retain the decimal places use the fabs( ) function.

 

fabs(2.1) = 2.1

fabs(-2.1) = 2.1

abs (2.1) = 2.

abs (-2.1) = 2

abs( ) will ignore decimal places.

Raising to power

Raise a base to the power of an exponent. If we say, 23, then 2 is the base and 3 is the exponent.

double pow(double base, double exponent)

 

pow(2,3) is equal to 8.

Exponent

This function will raise the natural logarithm base ‘e’ to the power of the argument provided. The value of e is 2.718.

double exp(double power)

 

 

cout<<exp(1);

The result will be: 2.718

 

 Rounding Off values:

ceil:

The ceil ( ) function is used for rounding up a number to an integer. But it doesn’t exactly do rounding up. It will return the lowest integer that is greater than the number you gave as argument.

double ceil (double val)

If val=2.1, then the returned value will be 3. If val= -2.1 then the returned value will be –2.

Floor:

This is again similar to ceil ( ) function except that this will return the highest integer that is less than the value you provided.

double floor (double val)

Suppose that val=2.1, after the floor ( ) operation you will get a result of 2.

If val = -2.1 then the result will be –3.

Random Numbers:

The rand ( ) function can be used to generate random numbers. The srand( ) function can be used to initialize the random number generator. srand ( ) will not return any value while the rand ( ) will return a random integer.

The example below should clarify your doubts about srand ( ).

int main( )
{
srand(10);
cout<<rand()<<endl;
cout<<rand()<<endl;
srand(10);
cout<<rand( );
return 0;
}

The output will be:

71
16899
71

Once initialized to the same number, the random number generator will generate the same random number. In the above case, srand (10) initializes the random number generator to 71.


Go back to the Contents Page 2


Copyright © 2005 Sethu Subramanian All rights reserved. Sign my guestbook.