One of the objects of good programming style should be to minimize the amount of repeated coding that must done. By this is meant that the programmer should try to use functions that are already defined. The language C++ has many different functions already built into libraries. These functions are called **predefined functions**. To use them all that needs to be done is to attach their header file to the program with a **#include**. For example Standard C++ has a library that is called **cmath**. (In Classical C++ the header was called **math.h**). Some of the predefined functions in the library **cmath** are:

| |
---|---|

| Returns the smallest integer greater that |

| Returns the double that is the cosine of |

| Returns the double that is the sine of |

| Returns the absolute value of |

| Returns the largest integer less than |

| Returns |

| Returns the double that is |

| Returns the double that is log base e of |

| Returns the double that is the square root of |

Suppose that a program was needed that would output the squares and cubes of all of the numbers from **1** up to a value specified by the user. This could be done by using the following calculations: **theNumber * theNumber** and **theNumber * theNumber * theNumber** to output. But using **pow()** from the **cmath** library, these expressions could be written as: **pow(theNumber,2)** and **pow(theNumber,3)**. See ** powers.cpp**.

Two other C++ built in libraries that you may find useful are: **cstdlib** and **cctype**. (These files were **stdlib.h** and **ctype.h** in Classical C++) Some examples of predefined functions in these libraries are the following:

| |
---|---|

| Returns the absolute value of |

| Returns a number between |

Suppose that you wanted to obtain a collection of random numbers that are between two values like **1** and **25**. The predefined function **rand( )** from the **cstdlib** library could be used. However since **rand( )** returns random numbers between **0** and **1**, the function needs the following modification to get these values: **rand() % 25 + 1**. (The **1** was needed because you wanted to start at **1** and go to **25**.) For example see ** random.cpp**. Compile and run the program. Did the outcome appear as you expected? Run the program several times. Observe the first and last number each time. Were these values the same each time the program is run? This is one of the problems with

| |
---|---|

| Returns the lower case value of |

| Returns the upper case value of |

| Returns |

| Returns |

| Returns true when |

Suppose that you wanted to ensure that the user was entering a number and * not* some non digit character. For this, you could use the predefined function:

In general, when you use a predefined function in a program, you do * not* know what its algorithms are. As a result the pseudo code of a program that contains a predefined function would

There are additional C++ built-in libraries but they will * not* be discussed in this part of the lectures.

Intermediate Business Programming with C++

ISBN: 738453099

EAN: N/A

EAN: N/A

Year: 2007

Pages: 142

Pages: 142

flylib.com © 2008-2017.

If you may any questions please contact us: flylib@qtcs.net

If you may any questions please contact us: flylib@qtcs.net